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.surefire.providerapi.AbstractProvider;
23  import org.apache.maven.surefire.providerapi.ProviderParameters;
24  import org.apache.maven.surefire.report.ReporterConfiguration;
25  import org.apache.maven.surefire.report.ReporterException;
26  import org.apache.maven.surefire.report.ReporterFactory;
27  import org.apache.maven.surefire.report.ReporterManagerFactory;
28  import org.apache.maven.surefire.suite.RunResult;
29  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
30  import org.apache.maven.surefire.testset.TestArtifactInfo;
31  import org.apache.maven.surefire.testset.TestRequest;
32  import org.apache.maven.surefire.testset.TestSetFailedException;
33  import org.apache.maven.surefire.util.DirectoryScanner;
34  import org.apache.maven.surefire.util.NestedRuntimeException;
35  import org.apache.maven.surefire.util.TestsToRun;
36  
37  import java.io.File;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.Properties;
41  
42  /**
43   * @author Kristian Rosenvold
44   * @noinspection UnusedDeclaration
45   */
46  public class TestNGProvider
47      extends AbstractProvider
48  {
49      private final Properties providerProperties;
50  
51      private final TestArtifactInfo testArtifactInfo;
52  
53      private final ReporterConfiguration reporterConfiguration;
54  
55      private final ClassLoader testClassLoader;
56  
57      private final DirectoryScannerParameters directoryScannerParameters;
58  
59      private final DirectoryScanner directoryScanner;
60  
61      private final TestRequest testRequest;
62  
63      private final ProviderParameters providerParameters;
64  
65      private TestsToRun testsToRun;
66  
67      private final File basedir;
68  
69      public TestNGProvider( ProviderParameters booterParameters )
70      {
71          this.providerParameters = booterParameters;
72          this.testClassLoader = booterParameters.getTestClassLoader();
73          this.directoryScannerParameters = booterParameters.getDirectoryScannerParameters();
74          this.providerProperties = booterParameters.getProviderProperties();
75          this.testRequest = booterParameters.getTestRequest();
76          basedir = directoryScannerParameters != null ? directoryScannerParameters.getTestClassesDirectory() : null;
77          testArtifactInfo = booterParameters.getTestArtifactInfo();
78          reporterConfiguration = booterParameters.getReporterConfiguration();
79          this.directoryScanner = booterParameters.getDirectoryScanner();
80      }
81  
82      public Boolean isRunnable()
83      {
84          return Boolean.TRUE;
85      }
86  
87      public RunResult invoke( Object forkTestSet )
88          throws TestSetFailedException, ReporterException
89      {
90  
91          final ReporterFactory reporterFactory = providerParameters.getReporterFactory();
92  
93          if ( isTestNGXmlTestSuite( testRequest ) )
94          {
95              TestNGXmlTestSuite testNGXmlTestSuite = getXmlSuite();
96              testNGXmlTestSuite.locateTestSets( testClassLoader );
97              if ( forkTestSet != null && testRequest == null )
98              {
99                  testNGXmlTestSuite.execute( (String) forkTestSet, (ReporterManagerFactory) reporterFactory,
100                                             testClassLoader );
101             }
102             else
103             {
104                 testNGXmlTestSuite.execute( (ReporterManagerFactory) reporterFactory );
105             }
106         }
107         else
108         {
109             if ( testsToRun == null )
110             {
111                 testsToRun = forkTestSet == null ? scanClassPath() : TestsToRun.fromClass( (Class) forkTestSet );
112             }
113             TestNGDirectoryTestSuite suite = getDirectorySuite();
114             suite.execute( testsToRun, reporterFactory );
115         }
116 
117         return reporterFactory.close();
118     }
119 
120     boolean isTestNGXmlTestSuite( TestRequest testSuiteDefinition )
121     {
122         return testSuiteDefinition.getSuiteXmlFiles() != null && testSuiteDefinition.getSuiteXmlFiles().size() > 0 &&
123             testSuiteDefinition.getRequestedTest() == null;
124 
125     }
126 
127 
128     private TestNGDirectoryTestSuite getDirectorySuite()
129     {
130         return new TestNGDirectoryTestSuite( basedir, new ArrayList( directoryScannerParameters.getIncludes() ),
131                                              new ArrayList( directoryScannerParameters.getExcludes() ),
132                                              testRequest.getTestSourceDirectory().toString(),
133                                              testArtifactInfo.getVersion(), providerProperties,
134                                              reporterConfiguration.getReportsDirectory(),
135                                              testRequest.getRequestedTestMethod() );
136     }
137 
138     private TestNGXmlTestSuite getXmlSuite()
139     {
140         return new TestNGXmlTestSuite( testRequest.getSuiteXmlFiles(), testRequest.getTestSourceDirectory().toString(),
141                                        testArtifactInfo.getVersion(), providerProperties,
142                                        reporterConfiguration.getReportsDirectory() );
143     }
144 
145 
146     public Iterator getSuites()
147     {
148         if ( isTestNGXmlTestSuite( testRequest ) )
149         {
150             try
151             {
152                 return getXmlSuite().locateTestSets( testClassLoader ).keySet().iterator();
153             }
154             catch ( TestSetFailedException e )
155             {
156                 throw new NestedRuntimeException( e );
157             }
158         }
159         else
160         {
161             testsToRun = scanClassPath();
162             return testsToRun.iterator();
163         }
164     }
165 
166     private TestsToRun scanClassPath()
167     {
168         return directoryScanner.locateTestClasses( testClassLoader, null );
169     }
170 
171 
172 }