1 package test.sanitycheck;
2 
3 import org.testng.Assert;
4 import org.testng.TestListenerAdapter;
5 import org.testng.TestNG;
6 import org.testng.annotations.Test;
7 import org.testng.xml.Parser;
8 import org.testng.xml.XmlClass;
9 import org.testng.xml.XmlSuite;
10 import org.testng.xml.XmlTest;
11 import org.xml.sax.SAXException;
12 import test.SimpleBaseTest;
13 import java.io.IOException;
14 import java.util.Arrays;
15 import javax.xml.parsers.ParserConfigurationException;
16 
17 public class CheckSuiteNamesTest extends SimpleBaseTest {
18 
19   /**
20    * Child suites have different names
21    */
22   @Test
checkChildSuites()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
checkChildSuitesFails()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
checkProgrammaticSuitesFails()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
checkXmlSuiteAddition()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