1 | |
package org.apache.maven.surefire.testng; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.lang.annotation.Annotation; |
24 | |
import java.lang.reflect.Method; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collections; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
import java.util.Properties; |
31 | |
import java.util.SortedMap; |
32 | |
import java.util.TreeMap; |
33 | |
|
34 | |
import org.apache.maven.surefire.NonAbstractClassFilter; |
35 | |
import org.apache.maven.surefire.report.ConsoleOutputCapture; |
36 | |
import org.apache.maven.surefire.report.ConsoleOutputReceiver; |
37 | |
import org.apache.maven.surefire.report.ReportEntry; |
38 | |
import org.apache.maven.surefire.report.ReporterException; |
39 | |
import org.apache.maven.surefire.report.ReporterFactory; |
40 | |
import org.apache.maven.surefire.report.RunListener; |
41 | |
import org.apache.maven.surefire.report.SimpleReportEntry; |
42 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
43 | |
import org.apache.maven.surefire.util.RunOrderCalculator; |
44 | |
import org.apache.maven.surefire.util.ScanResult; |
45 | |
import org.apache.maven.surefire.util.TestsToRun; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class TestNGDirectoryTestSuite |
54 | |
implements TestNgTestSuite |
55 | |
{ |
56 | |
|
57 | |
private final Map options; |
58 | |
|
59 | |
private final Map junitOptions; |
60 | |
|
61 | |
private final String testSourceDirectory; |
62 | |
|
63 | |
private final File reportsDirectory; |
64 | |
|
65 | |
private SortedMap<String, TestNGTestSet> testSets; |
66 | |
|
67 | |
private final ScanResult scanResult; |
68 | |
|
69 | |
private final String testMethodPattern; |
70 | |
|
71 | |
private final RunOrderCalculator runOrderCalculator; |
72 | |
|
73 | |
private final Class junitTestClass; |
74 | |
|
75 | |
private Class<? extends Annotation> junitRunWithAnnotation; |
76 | |
|
77 | |
private Class<? extends Annotation> junitTestAnnotation; |
78 | |
|
79 | |
public TestNGDirectoryTestSuite( String testSourceDirectory, Properties confOptions, File reportsDirectory, String testMethodPattern, |
80 | |
RunOrderCalculator runOrderCalculator, ScanResult scanResult ) |
81 | 0 | { |
82 | |
|
83 | 0 | this.runOrderCalculator = runOrderCalculator; |
84 | |
|
85 | 0 | this.options = confOptions; |
86 | |
|
87 | 0 | this.testSourceDirectory = testSourceDirectory; |
88 | 0 | this.reportsDirectory = reportsDirectory; |
89 | 0 | this.scanResult = scanResult; |
90 | 0 | this.testMethodPattern = testMethodPattern; |
91 | 0 | this.junitTestClass = findJUnitTestClass(); |
92 | 0 | this.junitRunWithAnnotation = findJUnitRunWithAnnotation(); |
93 | 0 | this.junitTestAnnotation = findJUnitTestAnnotation(); |
94 | 0 | this.junitOptions = createJUnitOptions(); |
95 | 0 | } |
96 | |
|
97 | |
public void execute( TestsToRun testsToRun, ReporterFactory reporterManagerFactory ) |
98 | |
throws ReporterException, TestSetFailedException |
99 | |
{ |
100 | |
|
101 | 0 | if ( !testsToRun.allowEagerReading() ) |
102 | |
{ |
103 | 0 | executeLazy( testsToRun, reporterManagerFactory ); |
104 | |
} |
105 | 0 | else if ( testsToRun.containsAtLeast( 2 ) ) |
106 | |
{ |
107 | 0 | executeMulti( testsToRun, reporterManagerFactory ); |
108 | |
} |
109 | 0 | else if ( testsToRun.containsAtLeast( 1 ) ) |
110 | |
{ |
111 | 0 | Class testClass = testsToRun.iterator().next(); |
112 | 0 | executeSingleClass( reporterManagerFactory, testClass ); |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
private void executeSingleClass( ReporterFactory reporterManagerFactory, Class testClass ) |
117 | |
throws TestSetFailedException |
118 | |
{ |
119 | 0 | this.options.put( "suitename", testClass.getName() ); |
120 | |
|
121 | 0 | RunListener reporter = reporterManagerFactory.createReporter(); |
122 | 0 | ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter ); |
123 | |
|
124 | 0 | startTestSuite( reporter, this ); |
125 | |
|
126 | 0 | final Map optionsToUse = isJUnitTest( testClass ) ? junitOptions : options; |
127 | |
|
128 | 0 | TestNGExecutor.run( new Class[]{ testClass }, testSourceDirectory, optionsToUse, reporter, this, |
129 | |
reportsDirectory, testMethodPattern ); |
130 | |
|
131 | 0 | finishTestSuite( reporter, this ); |
132 | 0 | } |
133 | |
|
134 | |
public void executeLazy( TestsToRun testsToRun, ReporterFactory reporterFactory ) |
135 | |
throws ReporterException, TestSetFailedException |
136 | |
{ |
137 | |
|
138 | 0 | for ( Class c : testsToRun ) |
139 | |
{ |
140 | 0 | executeSingleClass( reporterFactory, c ); |
141 | 0 | } |
142 | 0 | } |
143 | |
|
144 | |
private Class findJUnitTestClass() |
145 | |
{ |
146 | 0 | return lookupClass( "junit.framework.Test" ); |
147 | |
} |
148 | |
|
149 | |
private Class findJUnitRunWithAnnotation() |
150 | |
{ |
151 | 0 | return lookupClass( "org.junit.runner.RunWith" ); |
152 | |
} |
153 | |
|
154 | |
private Class findJUnitTestAnnotation() |
155 | |
{ |
156 | 0 | return lookupClass( "org.junit.Test" ); |
157 | |
} |
158 | |
|
159 | |
private Class lookupClass( String className ) |
160 | |
{ |
161 | |
Class junitClass; |
162 | |
try |
163 | |
{ |
164 | 0 | junitClass = Class.forName( className ); |
165 | |
} |
166 | 0 | catch ( ClassNotFoundException e ) |
167 | |
{ |
168 | 0 | junitClass = null; |
169 | 0 | } |
170 | 0 | return junitClass; |
171 | |
} |
172 | |
|
173 | |
public void executeMulti( TestsToRun testsToRun, ReporterFactory reporterFactory ) |
174 | |
throws ReporterException, TestSetFailedException |
175 | |
{ |
176 | 0 | List<Class> testNgTestClasses = new ArrayList<Class>(); |
177 | 0 | List<Class> junitTestClasses = new ArrayList<Class>(); |
178 | 0 | for ( Class c : testsToRun ) |
179 | |
{ |
180 | 0 | if ( isJUnitTest( c ) ) |
181 | |
{ |
182 | 0 | junitTestClasses.add( c ); |
183 | |
} |
184 | |
else |
185 | |
{ |
186 | 0 | testNgTestClasses.add( c ); |
187 | |
} |
188 | 0 | } |
189 | |
|
190 | 0 | File testNgReportsDirectory = reportsDirectory, junitReportsDirectory = reportsDirectory; |
191 | |
|
192 | 0 | if ( junitTestClasses.size() > 0 && testNgTestClasses.size() > 0 ) |
193 | |
{ |
194 | 0 | testNgReportsDirectory = new File( reportsDirectory, "testng-native-results" ); |
195 | 0 | junitReportsDirectory = new File( reportsDirectory, "testng-junit-results" ); |
196 | |
} |
197 | |
|
198 | 0 | RunListener reporterManager = reporterFactory.createReporter(); |
199 | 0 | ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporterManager ); |
200 | 0 | startTestSuite( reporterManager, this ); |
201 | |
|
202 | 0 | Class[] testClasses = testNgTestClasses.toArray( new Class[testNgTestClasses.size()] ); |
203 | |
|
204 | 0 | TestNGExecutor.run( testClasses, this.testSourceDirectory, options, reporterManager, this, |
205 | |
testNgReportsDirectory, testMethodPattern ); |
206 | |
|
207 | 0 | if ( junitTestClasses.size() > 0 ) |
208 | |
{ |
209 | 0 | testClasses = junitTestClasses.toArray( new Class[junitTestClasses.size()] ); |
210 | |
|
211 | 0 | TestNGExecutor.run( testClasses, testSourceDirectory, junitOptions, reporterManager, this, |
212 | |
junitReportsDirectory, testMethodPattern ); |
213 | |
} |
214 | |
|
215 | 0 | finishTestSuite( reporterManager, this ); |
216 | 0 | } |
217 | |
|
218 | |
private boolean isJUnitTest( Class c ) |
219 | |
{ |
220 | 0 | return isJunit3Test( c ) || isJunit4Test( c ); |
221 | |
} |
222 | |
|
223 | |
private boolean isJunit4Test( Class c ) |
224 | |
{ |
225 | 0 | return hasJunit4RunWithAnnotation( c ) || hasJunit4TestAnnotation( c ); |
226 | |
} |
227 | |
|
228 | |
private boolean hasJunit4RunWithAnnotation( Class c ) |
229 | |
{ |
230 | 0 | return junitRunWithAnnotation != null && c.getAnnotation( junitRunWithAnnotation ) != null; |
231 | |
} |
232 | |
|
233 | |
private boolean hasJunit4TestAnnotation( Class c ) |
234 | |
{ |
235 | 0 | if ( junitTestAnnotation != null ) |
236 | |
{ |
237 | 0 | for ( Method m : c.getMethods() ) |
238 | |
{ |
239 | 0 | if ( m.getAnnotation( junitTestAnnotation ) != null ) |
240 | |
{ |
241 | 0 | return true; |
242 | |
} |
243 | |
} |
244 | |
} |
245 | |
|
246 | 0 | return false; |
247 | |
} |
248 | |
|
249 | |
private boolean isJunit3Test( Class c ) |
250 | |
{ |
251 | 0 | return junitTestClass != null && junitTestClass.isAssignableFrom( c ); |
252 | |
} |
253 | |
|
254 | |
private Map createJUnitOptions() |
255 | |
{ |
256 | 0 | Map junitOptions = new HashMap( this.options ); |
257 | 0 | junitOptions.put( "junit", Boolean.TRUE ); |
258 | 0 | return junitOptions; |
259 | |
} |
260 | |
|
261 | |
|
262 | |
public void execute( String testSetName, ReporterFactory reporterManagerFactory ) |
263 | |
throws ReporterException, TestSetFailedException |
264 | |
{ |
265 | 0 | if ( testSets == null ) |
266 | |
{ |
267 | 0 | throw new IllegalStateException( "You must call locateTestSets before calling execute" ); |
268 | |
} |
269 | 0 | TestNGTestSet testSet = testSets.get( testSetName ); |
270 | |
|
271 | 0 | if ( testSet == null ) |
272 | |
{ |
273 | 0 | throw new TestSetFailedException( "Unable to find test set '" + testSetName + "' in suite" ); |
274 | |
} |
275 | |
|
276 | 0 | RunListener reporter = reporterManagerFactory.createReporter(); |
277 | 0 | ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter ); |
278 | |
|
279 | 0 | startTestSuite( reporter, this ); |
280 | |
|
281 | 0 | TestNGExecutor.run( new Class[]{ testSet.getTestClass() }, this.testSourceDirectory, this.options, reporter, this, reportsDirectory, testMethodPattern ); |
282 | |
|
283 | 0 | finishTestSuite( reporter, this ); |
284 | 0 | } |
285 | |
|
286 | |
public static void startTestSuite( RunListener reporter, Object suite ) |
287 | |
{ |
288 | 0 | ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) ); |
289 | |
|
290 | |
try |
291 | |
{ |
292 | 0 | reporter.testSetStarting( report ); |
293 | |
} |
294 | 0 | catch ( ReporterException e ) |
295 | |
{ |
296 | |
|
297 | 0 | } |
298 | 0 | } |
299 | |
|
300 | |
public static void finishTestSuite( RunListener reporterManager, Object suite ) |
301 | |
throws ReporterException |
302 | |
{ |
303 | 0 | ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) ); |
304 | |
|
305 | 0 | reporterManager.testSetCompleted( report ); |
306 | 0 | } |
307 | |
|
308 | |
public String getSuiteName() |
309 | |
{ |
310 | 0 | String result = (String) options.get( "suitename" ); |
311 | 0 | if ( result == null ) |
312 | |
{ |
313 | 0 | result = "TestSuite"; |
314 | |
} |
315 | 0 | return result; |
316 | |
} |
317 | |
|
318 | |
private static String getSuiteName( Object suite ) |
319 | |
{ |
320 | |
String result; |
321 | 0 | if ( suite instanceof TestNGDirectoryTestSuite ) |
322 | |
{ |
323 | 0 | return ( (TestNGDirectoryTestSuite) suite ).getSuiteName(); |
324 | |
} |
325 | 0 | else if ( suite instanceof TestNGXmlTestSuite ) |
326 | |
{ |
327 | 0 | return ( (TestNGXmlTestSuite) suite ).getSuiteName(); |
328 | |
} |
329 | |
else |
330 | |
{ |
331 | 0 | result = "TestSuite"; |
332 | |
} |
333 | |
|
334 | 0 | return result; |
335 | |
} |
336 | |
|
337 | |
public Map locateTestSets( ClassLoader classLoader ) |
338 | |
throws TestSetFailedException |
339 | |
{ |
340 | 0 | if ( testSets != null ) |
341 | |
{ |
342 | 0 | throw new IllegalStateException( "You can't call locateTestSets twice" ); |
343 | |
} |
344 | 0 | testSets = new TreeMap<String, TestNGTestSet>(); |
345 | |
|
346 | 0 | final TestsToRun scanned = scanResult.applyFilter( new NonAbstractClassFilter(), classLoader ); |
347 | |
|
348 | 0 | final TestsToRun testsToRun = runOrderCalculator.orderTestClasses( scanned ); |
349 | |
|
350 | 0 | for ( Class testClass : testsToRun ) |
351 | |
{ |
352 | 0 | TestNGTestSet testSet = new TestNGTestSet( testClass ); |
353 | |
|
354 | 0 | if ( testSets.containsKey( testSet.getName() ) ) |
355 | |
{ |
356 | 0 | throw new TestSetFailedException( "Duplicate test set '" + testSet.getName() + "'" ); |
357 | |
} |
358 | 0 | testSets.put( testSet.getName(), testSet ); |
359 | |
|
360 | 0 | } |
361 | |
|
362 | 0 | return Collections.unmodifiableSortedMap( testSets ); |
363 | |
} |
364 | |
|
365 | |
} |