1 package test.timeout;
2 
3 import org.testng.annotations.Test;
4 import org.testng.xml.SuiteXmlParser;
5 import org.testng.xml.XmlClass;
6 import org.testng.xml.XmlInclude;
7 import org.testng.xml.XmlSuite;
8 import org.testng.xml.XmlTest;
9 
10 import test.BaseTest;
11 
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 
18 public class TimeOutFromXmlTest extends BaseTest {
19 
timeOutTest(boolean onSuite)20     private void timeOutTest(boolean onSuite) {
21         addClass(TestTimeOutSampleTest.class);
22         if (onSuite) {
23             setSuiteTimeOut(1_000);
24         } else {
25             setTestTimeOut(1_000);
26         }
27         run();
28 
29         verifyPassedTests();
30         verifyFailedTests("timeoutTest");
31     }
32 
33     @Test
timeOutOnSuiteTag()34     public void timeOutOnSuiteTag() {
35         timeOutTest(true /* on suite */);
36     }
37 
38     @Test
timeOutOnTestTag()39     public void timeOutOnTestTag() {
40         timeOutTest(false /* on test */);
41     }
42 
43     @Test
noTimeOut()44     public void noTimeOut() {
45       addClass(TestTimeOutSampleTest.class);
46       run();
47 
48       verifyPassedTests("timeoutTest");
49       verifyFailedTests();
50     }
51 
52     @Test
twoDifferentTests()53     public void twoDifferentTests() {
54       XmlSuite result = new XmlSuite();
55       result.setName("Suite");
56 
57       createXmlTest(result, "WithoutTimeOut");
58       createXmlTest(result, "WithTimeOut").setTimeOut(1_000);
59 
60       setSuite(result);
61       run();
62 
63       verifyPassedTests("timeoutTest");
64       verifyFailedTests("timeoutTest");
65     }
66 
createXmlTest(XmlSuite suite, String name)67     private XmlTest createXmlTest(XmlSuite suite, String name) {
68         XmlTest result = new XmlTest(suite);
69         result.setName(name);
70         List<XmlClass> classes = new ArrayList<>();
71         XmlClass cls = new XmlClass(TestTimeOutSampleTest.class);
72         cls.setIncludedMethods(
73             Collections.singletonList(new XmlInclude("timeoutTest")));
74         classes.add(cls);
75         result.setXmlClasses(classes);
76 
77         return result;
78     }
79 
80     @Test
timeOutInParallelTestsFromXml()81     public void timeOutInParallelTestsFromXml() throws IOException {
82       String file = "src/test/java/test/timeout/issue575.xml";
83       try (FileInputStream stream = new FileInputStream(file)) {
84         SuiteXmlParser suiteParser = new SuiteXmlParser();
85         XmlSuite suite = suiteParser.parse(file, stream, true);
86         setSuite(suite);
87         run();
88 
89         verifyPassedTests("timeoutShouldPass");
90         verifyFailedTests("timeoutShouldFailByException", "timeoutShouldFailByTimeOut");
91       }
92     }
93 }
94