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.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  import org.apache.maven.surefire.report.ConsoleOutputCapture;
29  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
30  import org.apache.maven.surefire.report.ReporterFactory;
31  import org.apache.maven.surefire.report.RunListener;
32  import org.apache.maven.surefire.testset.TestSetFailedException;
33  
34  /**
35   * Handles suite xml file definitions for TestNG.
36   *
37   * @author jkuhnert
38   * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
39   */
40  public class TestNGXmlTestSuite
41      implements TestNgTestSuite
42  {
43      private final List suiteFiles;
44  
45      private List<String> suiteFilePaths;
46  
47      private final String testSourceDirectory;
48  
49      private final Map options;
50  
51      private final File reportsDirectory;
52  
53      // Not really used
54      private Map<File, String> testSets;
55  
56      /**
57       * Creates a testng testset to be configured by the specified
58       * xml file(s). The XML files are suite definitions files according to TestNG DTD.
59       */
60      public TestNGXmlTestSuite( List<File> suiteFiles, String testSourceDirectory, Properties confOptions,
61                                 File reportsDirectory )
62      {
63          this.suiteFiles = suiteFiles;
64  
65          this.options = confOptions;
66  
67          this.testSourceDirectory = testSourceDirectory;
68  
69          this.reportsDirectory = reportsDirectory;
70      }
71  
72      public void execute( ReporterFactory reporterManagerFactory )
73          throws TestSetFailedException
74      {
75          if ( testSets == null )
76          {
77              throw new IllegalStateException( "You must call locateTestSets before calling execute" );
78          }
79  //        RunListener reporter = new SynchronizedReporterManager( reporterManagerFactory.createReporter() );
80          RunListener reporter = reporterManagerFactory.createReporter();
81          ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
82  
83          TestNGDirectoryTestSuite.startTestSuite( reporter, this );
84          TestNGExecutor.run( this.suiteFilePaths, this.testSourceDirectory, this.options, reporter, this,
85                              reportsDirectory );
86          TestNGDirectoryTestSuite.finishTestSuite( reporter, this );
87      }
88  
89      public void execute( String testSetName, ReporterFactory reporterManagerFactory )
90          throws TestSetFailedException
91      {
92          throw new TestSetFailedException( "Cannot run individual test when suite files are specified" );
93      }
94  
95      public Map locateTestSets( ClassLoader classLoader )
96          throws TestSetFailedException
97      {
98          if ( testSets != null )
99          {
100             throw new IllegalStateException( "You can't call locateTestSets twice" );
101         }
102 
103         if ( this.suiteFiles == null )
104         {
105             throw new IllegalStateException( "No suite files were specified" );
106         }
107 
108         this.testSets = new HashMap<File, String>();
109         this.suiteFilePaths = new ArrayList<String>();
110 
111         for ( Object suiteFile : suiteFiles )
112         {
113             File file = (File) suiteFile;
114             if ( !file.exists() || !file.isFile() )
115             {
116                 throw new TestSetFailedException( "Suite file " + file + " is not a valid file" );
117             }
118             this.testSets.put( file, file.getAbsolutePath() );
119             this.suiteFilePaths.add( file.getAbsolutePath() );
120         }
121 
122         return this.testSets;
123     }
124 
125     public String getSuiteName()
126     {
127         String result = (String) options.get( "suitename" );
128         if ( result == null )
129         {
130             result = "TestSuite";
131         }
132         return result;
133     }
134 }