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