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