1 package test.methodinterceptors;
2 
3 import org.testng.Assert;
4 import org.testng.ITestResult;
5 import org.testng.TestListenerAdapter;
6 import org.testng.TestNG;
7 import org.testng.annotations.Test;
8 import org.testng.xml.Parser;
9 import org.testng.xml.XmlSuite;
10 import org.xml.sax.SAXException;
11 
12 import test.SimpleBaseTest;
13 
14 import java.io.BufferedWriter;
15 import java.io.File;
16 import java.io.FileWriter;
17 import java.io.IOException;
18 import java.util.List;
19 
20 import javax.xml.parsers.ParserConfigurationException;
21 
22 public class MethodInterceptorTest extends SimpleBaseTest {
23 
24   private String XML =
25     "<!DOCTYPE suite SYSTEM \"http://beust.com/testng/testng-1.0.dtd\" >" +
26     "" +
27     "<suite name=\"Single\" verbose=\"0\">" +
28     "" +
29     "<listeners>" +
30     "  <listener class-name=\"test.methodinterceptors.NullMethodInterceptor\" />" +
31     "</listeners>" +
32     "" +
33     "  <test name=\"Single\" >" +
34     "    <classes>" +
35     "      <class name=\"test.methodinterceptors.FooTest\" />" +
36     "     </classes>" +
37     "  </test>" +
38     "" +
39     "</suite>";
40 
41   @Test
noMethodsShouldRun()42   public void noMethodsShouldRun() {
43     TestNG tng = create();
44     tng.setTestClasses(new Class[] { FooTest.class });
45     testNullInterceptor(tng);
46   }
47 
testNullInterceptor(TestNG tng)48   private void testNullInterceptor(TestNG tng) {
49     tng.setMethodInterceptor(new NullMethodInterceptor());
50     TestListenerAdapter tla = new TestListenerAdapter();
51     tng.addListener(tla);
52     tng.run();
53 
54     Assert.assertEquals(tla.getPassedTests().size(), 0);
55     Assert.assertEquals(tla.getFailedTests().size(), 0);
56     Assert.assertEquals(tla.getSkippedTests().size(), 0);
57   }
58 
testFast(boolean useInterceptor)59   private void testFast(boolean useInterceptor) {
60     TestNG tng = create();
61     tng.setTestClasses(new Class[] { FooTest.class });
62     if (useInterceptor) {
63       tng.setMethodInterceptor(new FastTestsFirstInterceptor());
64     }
65     TestListenerAdapter tla = new TestListenerAdapter();
66 //    tng.setParallel("methods");
67     tng.addListener(tla);
68     tng.run();
69 
70     Assert.assertEquals(tla.getPassedTests().size(), 3);
71     ITestResult first = tla.getPassedTests().get(0);
72 
73     String method = "zzzfast";
74     if (useInterceptor) {
75       Assert.assertEquals(first.getMethod().getMethodName(), method);
76     } else {
77       Assert.assertNotSame(first.getMethod().getMethodName(), method);
78     }
79   }
80 
81   @Test
fastShouldRunFirst()82   public void fastShouldRunFirst() {
83     testFast(true /* use interceptor */);
84   }
85 
86   @Test
fastShouldNotRunFirst()87   public void fastShouldNotRunFirst() {
88     testFast(false /* don't use interceptor */);
89   }
90 
91   @Test
nullMethodInterceptorWorksInTestngXml()92   public void nullMethodInterceptorWorksInTestngXml()
93       throws IOException, ParserConfigurationException, SAXException {
94 
95     File f = File.createTempFile("testng-tests-", "");
96     f.deleteOnExit();
97     BufferedWriter bw = new BufferedWriter(new FileWriter(f));
98     bw.write(XML);
99     bw.close();
100 
101     try {
102       List<XmlSuite> xmlSuites = new Parser(f.getAbsolutePath()).parseToList();
103 
104       TestNG tng = create();
105       tng.setXmlSuites(xmlSuites);
106       testNullInterceptor(tng);
107     }
108     catch(Exception ex) {
109       ex.printStackTrace();
110     }
111   }
112 
113   @Test(timeOut = 1000)
shouldNotLockUpWithInterceptorThatRemovesMethods()114   public void shouldNotLockUpWithInterceptorThatRemovesMethods() {
115     TestNG tng = create(LockUpInterceptorSampleTest.class);
116     tng.setParallel(XmlSuite.ParallelMode.METHODS);
117     tng.run();
118   }
119 }
120