• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test.preserveorder;
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.XmlClass;
8 import org.testng.xml.XmlSuite;
9 import org.testng.xml.XmlTest;
10 
11 import test.BaseLogTest;
12 import test.SimpleBaseTest;
13 
14 import java.util.Arrays;
15 import java.util.List;
16 
17 public class PreserveOrderTest extends SimpleBaseTest {
18 
19   @Test
preserveClassOrder()20   public void preserveClassOrder() {
21     String[][] tests = new String[][] {
22       new String[] { "AAA", "B", "C" },
23       new String[] { "AAA", "C", "B" },
24       new String[] { "B", "AAA", "C" },
25       new String[] { "B", "C", "AAA" },
26       new String[] { "C", "B", "AAA" },
27       new String[] { "C", "AAA", "B" },
28     };
29     for (String[] t : tests) {
30       TestNG tng = create();
31 //      tng.setVerbose(2);
32       XmlSuite s = createXmlSuite("Suite");
33       String[] fullTestNames = new String[t.length];
34       for (int i = 0; i < t.length; i++) {
35         fullTestNames[i] = "test.preserveorder." + t[i];
36       }
37       XmlTest xt = createXmlTest(s, "Test", fullTestNames);
38       xt.setPreserveOrder("true");
39 //      System.out.println(s.toXml());
40       tng.setXmlSuites(Arrays.asList(s));
41       tng.run();
42 
43       // 3 methods per class, 3 classes, so the log should contain 9 calls
44       List<String> log = BaseLogTest.getLog();
45       Assert.assertEquals(log.size(), 9);
46 
47       // Every slice of 3 logs should belong to the same class in the same
48       // order as in the specified string:  AAA.a1, AAA.a2, AAA.a3, B.a1, etc...
49       // Since we're only testing class ordering in this test, we only match
50       // against the class name
51       for (int i = 0; i < t.length; i++) {
52         for (int j = 0; j < 3; j++) {
53           Assert.assertTrue(log.get(j + (3 * i)).startsWith(t[i] + "."));
54         }
55       }
56     }
57   }
58 
59   @Test
preserveMethodOrder()60   public void preserveMethodOrder() {
61     String[][] methods = new String[][] {
62         new String[] { "a1", "a2", "a3" },
63         new String[] { "a1", "a3", "a2" },
64         new String[] { "a2", "a1", "a3" },
65         new String[] { "a2", "a3", "a1" },
66         new String[] { "a3", "a2", "a1" },
67         new String[] { "a3", "a1", "a2" },
68     };
69 
70     for (String[] m : methods) {
71       TestNG tng = create();
72   //      tng.setVerbose(2);
73       XmlSuite s = createXmlSuite("Suite");
74       String testName = "test.preserveorder.AAA";
75       XmlTest xt = createXmlTest(s, "Test", testName);
76       addMethods(xt.getXmlClasses().get(0), m);
77       xt.setPreserveOrder("true");
78   //      System.out.println(s.toXml());
79       tng.setXmlSuites(Arrays.asList(s));
80       tng.run();
81 
82       List<String> log = BaseLogTest.getLog();
83 //      System.out.println(log);
84       for (int i = 0; i < log.size(); i++) {
85         if (!log.get(i).endsWith(m[i])) {
86           throw new AssertionError("Expected " + Arrays.asList(m) + " but got " + log);
87         }
88       }
89     }
90   }
91 
92   @Test
orderShouldBePreservedWithDependencies()93   public void orderShouldBePreservedWithDependencies() {
94     TestNG tng = create();
95     XmlSuite s = createXmlSuite("PreserveOrder");
96     XmlTest t = new XmlTest(s);
97     t.getXmlClasses().add(new XmlClass("test.preserveorder.ChuckTest4"));
98     t.getXmlClasses().add(new XmlClass("test.preserveorder.ChuckTest3"));
99     t.setPreserveOrder("true");
100     tng.setXmlSuites(Arrays.asList(s));
101     TestListenerAdapter tla = new TestListenerAdapter();
102     tng.addListener(tla);
103     tng.run();
104 
105     verifyPassedTests(tla, "c4TestOne", "c4TestTwo", "c4TestThree",
106         "c3TestOne", "c3TestTwo", "c3TestThree");
107   }
108 
109   @Test(description = "preserve-order on a factory doesn't cause an NPE")
factoryPreserve()110   public void factoryPreserve() {
111     TestNG tng = create();
112     XmlSuite s = createXmlSuite("FactoryPreserve");
113     XmlTest t = new XmlTest(s);
114     t.getXmlClasses().add(new XmlClass(TestClassFactory.class.getName()));
115     t.setPreserveOrder("true");
116     tng.setXmlSuites(Arrays.asList(s));
117     tng.run();
118   }
119 }
120