View Javadoc
1   package org.apache.maven.surefire.testng;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.surefire.report.RunListener;
28  import org.apache.maven.surefire.testset.TestSetFailedException;
29  
30  import static org.apache.maven.surefire.testng.TestNGExecutor.run;
31  
32  /**
33   * Handles suite xml file definitions for TestNG.
34   *
35   * @author jkuhnert
36   * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
37   */
38  final class TestNGXmlTestSuite
39          extends TestSuite
40  {
41      private final List<File> suiteFiles;
42  
43      private List<String> suiteFilePaths;
44  
45      private final String testSourceDirectory;
46  
47      private final Map<String, String> options;
48  
49      private final File reportsDirectory;
50  
51      private final int skipAfterFailureCount;
52  
53      /**
54       * Creates a testng testset to be configured by the specified
55       * xml file(s). The XML files are suite definitions files according to TestNG DTD.
56       */
57      TestNGXmlTestSuite( List<File> suiteFiles, String testSourceDirectory, Map<String, String> confOptions,
58                          File reportsDirectory, int skipAfterFailureCount )
59      {
60          this.suiteFiles = suiteFiles;
61          this.options = confOptions;
62          this.testSourceDirectory = testSourceDirectory;
63          this.reportsDirectory = reportsDirectory;
64          this.skipAfterFailureCount = skipAfterFailureCount;
65      }
66  
67      void execute( RunListener reporter )
68          throws TestSetFailedException
69      {
70          if ( suiteFilePaths == null )
71          {
72              throw new IllegalStateException( "You must call locateTestSets before calling execute" );
73          }
74          startTestSuite( reporter );
75          run( suiteFilePaths, testSourceDirectory, options, reporter, reportsDirectory, skipAfterFailureCount );
76          finishTestSuite( reporter );
77      }
78  
79      Iterable locateTestSets()
80          throws TestSetFailedException
81      {
82          if ( suiteFilePaths != null )
83          {
84              throw new IllegalStateException( "You can't call locateTestSets twice" );
85          }
86  
87          if ( suiteFiles.isEmpty() )
88          {
89              throw new IllegalStateException( "No suite files were specified" );
90          }
91  
92          suiteFilePaths = new ArrayList<String>( suiteFiles.size() );
93          ArrayList<File> testSets = new ArrayList<File>( suiteFiles.size() );
94  
95          for ( File suiteFile : suiteFiles )
96          {
97              if ( !suiteFile.isFile() )
98              {
99                  throw new TestSetFailedException( "Suite file " + suiteFile + " is not a valid file" );
100             }
101             testSets.add( suiteFile );
102             suiteFilePaths.add( suiteFile.getAbsolutePath() );
103         }
104         return testSets;
105     }
106 
107     @Override
108     Map<String, String> getOptions()
109     {
110         return options;
111     }
112 }