View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Handles suite xml file definitions for TestNG.
32   *
33   * @author jkuhnert
34   * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
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       * Creates a testng testset to be configured by the specified
51       * xml file(s). The XML files are suite definitions files according to TestNG DTD.
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 }