1 package test.sanitycheck;
2 
3 import java.util.Arrays;
4 
5 import org.testng.Assert;
6 import org.testng.TestListenerAdapter;
7 import org.testng.TestNG;
8 import org.testng.TestNGException;
9 import org.testng.annotations.Test;
10 import org.testng.xml.XmlClass;
11 import org.testng.xml.XmlSuite;
12 import org.testng.xml.XmlTest;
13 
14 import test.SimpleBaseTest;
15 
16 public class CheckTestNamesTest extends SimpleBaseTest {
17 
18   /**
19    * Child suites and same suite has two tests with same name
20    */
21   @Test
checkWithChildSuites()22   public void checkWithChildSuites() {
23     runSuite("sanitycheck/test-a.xml");
24   }
25 
26   /**
27    * Simple suite with two tests with same name
28    */
29   @Test
checkWithoutChildSuites()30   public void checkWithoutChildSuites() {
31     runSuite("sanitycheck/test1.xml");
32   }
33 
runSuite(String suitePath)34   private void runSuite(String suitePath)
35   {
36     TestListenerAdapter tla = new TestListenerAdapter();
37     boolean exceptionRaised = false;
38     try {
39       TestNG tng = create();
40       String testngXmlPath = getPathToResource(suitePath);
41       tng.setTestSuites(Arrays.asList(testngXmlPath));
42       tng.addListener(tla);
43       tng.run();
44     } catch (TestNGException ex) {
45       exceptionRaised = true;
46       Assert.assertEquals(tla.getPassedTests().size(), 0);
47       Assert.assertEquals(tla.getFailedTests().size(), 0);
48     }
49     Assert.assertTrue(exceptionRaised);
50   }
51 
52   /**
53    * Simple suite with no two tests with same name
54    */
55   @Test
checkNoError()56   public void checkNoError() {
57     TestListenerAdapter tla = new TestListenerAdapter();
58     TestNG tng = create();
59     String testngXmlPath = getPathToResource("sanitycheck/test2.xml");
60     tng.setTestSuites(Arrays.asList(testngXmlPath));
61     tng.addListener(tla);
62     tng.run();
63     Assert.assertEquals(tla.getPassedTests().size(), 2);
64   }
65 
66   /**
67    * Child suites and tests within different suites have same names
68    */
69   @Test(enabled = false)
checkNoErrorWtihChildSuites()70   public void checkNoErrorWtihChildSuites() {
71     TestListenerAdapter tla = new TestListenerAdapter();
72     TestNG tng = create();
73     String testngXmlPath = getPathToResource("sanitycheck/test-b.xml");
74     tng.setTestSuites(Arrays.asList(testngXmlPath));
75     tng.addListener(tla);
76     tng.run();
77     Assert.assertEquals(tla.getPassedTests().size(), 4);
78   }
79 
80   /**
81    * Checks that suites created programmatically also run as expected
82    */
83   @Test
checkTestNamesForProgrammaticSuites()84   public void checkTestNamesForProgrammaticSuites() {
85     XmlSuite xmlSuite = new XmlSuite();
86     xmlSuite.setName("SanityCheckSuite");
87     XmlTest result = new XmlTest(xmlSuite);
88     result.getXmlClasses().add(new XmlClass(SampleTest1.class.getCanonicalName()));
89     result = new XmlTest(xmlSuite);
90     result.getXmlClasses().add(new XmlClass(SampleTest2.class.getCanonicalName()));
91 
92     TestNG tng = new TestNG();
93     tng.setVerbose(0);
94     tng.setXmlSuites(Arrays.asList(new XmlSuite[] { xmlSuite }));
95     tng.run();
96   }
97 }
98