1package test.sanitycheck;
2
3import org.testng.Assert;
4import org.testng.TestListenerAdapter;
5import org.testng.TestNG;
6import org.testng.annotations.Test;
7import org.testng.xml.Parser;
8import org.testng.xml.XmlClass;
9import org.testng.xml.XmlSuite;
10import org.testng.xml.XmlTest;
11import org.xml.sax.SAXException;
12import test.SimpleBaseTest;
13import java.io.IOException;
14import java.util.Arrays;
15import javax.xml.parsers.ParserConfigurationException;
16
17public class CheckSuiteNamesTest extends SimpleBaseTest {
18
19  /**
20   * Child suites have different names
21   */
22  @Test
23  public void checkChildSuites() {
24    TestListenerAdapter tla = new TestListenerAdapter();
25    TestNG tng = create();
26    String testngXmlPath = getPathToResource("sanitycheck/test-s-b.xml");
27    tng.setTestSuites(Arrays.asList(testngXmlPath));
28    tng.addListener(tla);
29    tng.run();
30    Assert.assertEquals(tla.getPassedTests().size(), 4);
31  }
32
33  /**
34   * Child suites have same names
35   */
36  @Test
37  public void checkChildSuitesFails() {
38    TestListenerAdapter tla = new TestListenerAdapter();
39    TestNG tng = create();
40    String testngXmlPath = getPathToResource("sanitycheck/test-s-a.xml");
41    tng.setTestSuites(Arrays.asList(testngXmlPath));
42    tng.addListener(tla);
43    tng.run();
44    Assert.assertEquals(tla.getTestContexts().get(0).getSuite().getName(), "SanityCheck suites");
45    Assert.assertEquals(tla.getTestContexts().get(1).getSuite().getName(), "SanityCheck suites");
46    Assert.assertEquals(tla.getTestContexts().get(2).getSuite().getName(), "SanityCheck suites (0)");
47    Assert.assertEquals(tla.getTestContexts().get(3).getSuite().getName(), "SanityCheck suites (0)");
48  }
49
50  /**
51   * Checks that suites created programmatically also works as expected
52   */
53  @Test
54  public void checkProgrammaticSuitesFails() {
55    XmlSuite xmlSuite1 = new XmlSuite();
56    xmlSuite1.setName("SanityCheckSuite");
57    {
58      XmlTest result = new XmlTest(xmlSuite1);
59      result.getXmlClasses().add(new XmlClass(SampleTest1.class.getCanonicalName()));
60    }
61
62    XmlSuite xmlSuite2 = new XmlSuite();
63    xmlSuite2.setName("SanityCheckSuite");
64    {
65      XmlTest result = new XmlTest(xmlSuite2);
66      result.getXmlClasses().add(new XmlClass(SampleTest2.class.getCanonicalName()));
67    }
68
69    TestNG tng = create();
70    tng.setXmlSuites(Arrays.asList(xmlSuite1, xmlSuite2));
71    tng.run();
72    Assert.assertEquals(xmlSuite1.getName(), "SanityCheckSuite");
73    Assert.assertEquals(xmlSuite2.getName(), "SanityCheckSuite (0)");
74  }
75
76  @Test
77  public void checkXmlSuiteAddition() throws ParserConfigurationException, SAXException, IOException {
78    TestNG tng = create();
79    String testngXmlPath = getPathToResource("sanitycheck/test-s-b.xml");
80    Parser parser = new Parser(testngXmlPath);
81    tng.setXmlSuites(parser.parseToList());
82    tng.initializeSuitesAndJarFile();
83  }
84}
85