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