1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.surefire.testng;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.surefire.api.testset.TestSetFailedException;
27  
28  import static org.apache.maven.surefire.testng.TestNGExecutor.run;
29  
30  
31  
32  
33  
34  
35  
36  final class TestNGXmlTestSuite extends TestSuite {
37      private final List<File> suiteFiles;
38  
39      private List<String> suiteFilePaths;
40  
41      private final String testSourceDirectory;
42  
43      private final Map<String, String> options;
44  
45      private final File reportsDirectory;
46  
47      private final int skipAfterFailureCount;
48  
49      
50  
51  
52  
53      TestNGXmlTestSuite(
54              List<File> suiteFiles,
55              String testSourceDirectory,
56              Map<String, String> confOptions,
57              File reportsDirectory,
58              int skipAfterFailureCount) {
59          this.suiteFiles = suiteFiles;
60          this.options = confOptions;
61          this.testSourceDirectory = testSourceDirectory;
62          this.reportsDirectory = reportsDirectory;
63          this.skipAfterFailureCount = skipAfterFailureCount;
64      }
65  
66      void execute(TestNGReporter testNGReporter) throws TestSetFailedException {
67          if (suiteFilePaths == null) {
68              throw new IllegalStateException("You must call locateTestSets before calling execute");
69          }
70          startTestSuite(testNGReporter.getRunListener());
71          run(suiteFilePaths, testSourceDirectory, options, testNGReporter, reportsDirectory, skipAfterFailureCount);
72          finishTestSuite(testNGReporter.getRunListener());
73      }
74  
75      Iterable locateTestSets() throws TestSetFailedException {
76          if (suiteFilePaths != null) {
77              throw new IllegalStateException("You can't call locateTestSets twice");
78          }
79  
80          if (suiteFiles.isEmpty()) {
81              throw new IllegalStateException("No suite files were specified");
82          }
83  
84          suiteFilePaths = new ArrayList<>(suiteFiles.size());
85          ArrayList<File> testSets = new ArrayList<>(suiteFiles.size());
86  
87          for (File suiteFile : suiteFiles) {
88              if (!suiteFile.isFile()) {
89                  throw new TestSetFailedException("Suite file " + suiteFile + " is not a valid file");
90              }
91              testSets.add(suiteFile);
92              suiteFilePaths.add(suiteFile.getAbsolutePath());
93          }
94          return testSets;
95      }
96  
97      @Override
98      Map<String, String> getOptions() {
99          return options;
100     }
101 }