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