1 package test.parameters;
2 
3 import org.testng.TestListenerAdapter;
4 import org.testng.TestNG;
5 import org.testng.annotations.Test;
6 import org.testng.xml.XmlClass;
7 import org.testng.xml.XmlInclude;
8 import org.testng.xml.XmlSuite;
9 import org.testng.xml.XmlTest;
10 
11 import test.SimpleBaseTest;
12 
13 import java.util.Arrays;
14 
15 public class ParameterOverrideTest extends SimpleBaseTest {
16   enum S {
17     FAIL,
18     PASS_TEST,
19     PASS_CLASS,
20     PASS_INCLUDE,
21   };
22 
23   @Test
testOverrideSuite()24   public void testOverrideSuite() {
25     privateTestOverrideSuite(S.PASS_TEST);
26   }
27 
28   @Test(expectedExceptions = AssertionError.class)
testOverrideSuiteNegative()29   public void testOverrideSuiteNegative() {
30     privateTestOverrideSuite(S.FAIL);
31   }
32 
33   @Test
classOverrideSuite()34   public void classOverrideSuite() {
35     privateTestOverrideSuite(S.PASS_CLASS);
36   }
37 
38   @Test(expectedExceptions = AssertionError.class)
classOverrideSuiteNegative()39   public void classOverrideSuiteNegative() {
40     privateTestOverrideSuite(S.FAIL);
41   }
42 
43   @Test
includeOverrideClass()44   public void includeOverrideClass() {
45     privateTestOverrideSuite(S.PASS_INCLUDE);
46   }
47 
48   @Test(expectedExceptions = AssertionError.class)
includeOverrideClassNegative()49   public void includeOverrideClassNegative() {
50     privateTestOverrideSuite(S.FAIL);
51   }
52 
privateTestOverrideSuite(S status)53   public void privateTestOverrideSuite(S status) {
54     XmlSuite s = createXmlSuite("s");
55     s.getParameters().put("a", "Incorrect");
56     s.getParameters().put("InheritedFromSuite", "InheritedFromSuite");
57     XmlTest t = createXmlTest(s, "t");
58     t.getLocalParameters().put("InheritedFromTest", "InheritedFromTest");
59     if (status == S.PASS_TEST) {
60       t.getLocalParameters().put("a", "Correct");
61     }
62 
63     {
64       XmlClass c1 = new XmlClass(Override1Sample.class.getName());
65       c1.getLocalParameters().put("InheritedFromClass", "InheritedFromClass");
66       if (status == S.PASS_CLASS) {
67         c1.getLocalParameters().put("a", "Correct");
68       }
69       t.getXmlClasses().add(c1);
70 
71       for (String method : new String[] { "f", "g" }) {
72         XmlInclude include1 = new XmlInclude(method);
73         if (status == S.PASS_INCLUDE) {
74           include1.getLocalParameters().put("a", "Correct");
75         }
76         include1.setXmlClass(c1);
77         c1.getIncludedMethods().add(include1);
78       }
79 
80     }
81 
82     TestNG tng = create();
83     tng.setXmlSuites(Arrays.asList(s));
84     TestListenerAdapter tla = new TestListenerAdapter();
85     tng.addListener(tla);
86 //    System.out.println(s.toXml());
87 //    tng.setVerbose(10);
88     tng.run();
89 
90     assertTestResultsEqual(tla.getPassedTests(), Arrays.asList("f", "g"));
91   }
92 }
93