1 | |
package org.apache.maven.surefire.junit; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.Iterator; |
23 | |
import org.apache.maven.surefire.common.junit3.JUnit3Reflector; |
24 | |
import org.apache.maven.surefire.common.junit3.JUnit3TestChecker; |
25 | |
import org.apache.maven.surefire.providerapi.AbstractProvider; |
26 | |
import org.apache.maven.surefire.providerapi.ProviderParameters; |
27 | |
import org.apache.maven.surefire.report.ConsoleOutputCapture; |
28 | |
import org.apache.maven.surefire.report.ConsoleOutputReceiver; |
29 | |
import org.apache.maven.surefire.report.ReportEntry; |
30 | |
import org.apache.maven.surefire.report.ReporterException; |
31 | |
import org.apache.maven.surefire.report.ReporterFactory; |
32 | |
import org.apache.maven.surefire.report.RunListener; |
33 | |
import org.apache.maven.surefire.report.SimpleReportEntry; |
34 | |
import org.apache.maven.surefire.suite.RunResult; |
35 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
36 | |
import org.apache.maven.surefire.util.DirectoryScanner; |
37 | |
import org.apache.maven.surefire.util.ReflectionUtils; |
38 | |
import org.apache.maven.surefire.util.RunOrderCalculator; |
39 | |
import org.apache.maven.surefire.util.TestsToRun; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class JUnit3Provider |
45 | |
extends AbstractProvider |
46 | |
{ |
47 | |
private final ClassLoader testClassLoader; |
48 | |
|
49 | |
private final DirectoryScanner directoryScanner; |
50 | |
|
51 | |
private final PojoAndJUnit3Checker testChecker; |
52 | |
|
53 | |
private final JUnit3TestChecker jUnit3TestChecker; |
54 | |
|
55 | |
private final JUnit3Reflector reflector; |
56 | |
|
57 | |
private final ProviderParameters providerParameters; |
58 | |
|
59 | |
private final RunOrderCalculator runOrderCalculator; |
60 | |
|
61 | |
private TestsToRun testsToRun; |
62 | |
|
63 | |
public JUnit3Provider( ProviderParameters booterParameters ) |
64 | 0 | { |
65 | 0 | this.providerParameters = booterParameters; |
66 | 0 | this.testClassLoader = booterParameters.getTestClassLoader(); |
67 | 0 | this.directoryScanner = booterParameters.getDirectoryScanner(); |
68 | 0 | this.runOrderCalculator = booterParameters.getRunOrderCalculator(); |
69 | 0 | this.reflector = new JUnit3Reflector( testClassLoader ); |
70 | 0 | jUnit3TestChecker = new JUnit3TestChecker( testClassLoader ); |
71 | 0 | this.testChecker = new PojoAndJUnit3Checker( jUnit3TestChecker ); |
72 | 0 | } |
73 | |
|
74 | |
public RunResult invoke( Object forkTestSet ) |
75 | |
throws TestSetFailedException, ReporterException |
76 | |
{ |
77 | 0 | if ( testsToRun == null ) |
78 | |
{ |
79 | 0 | testsToRun = forkTestSet == null ? scanClassPath() : TestsToRun.fromClass( (Class) forkTestSet ); |
80 | |
} |
81 | |
|
82 | 0 | ReporterFactory reporterFactory = providerParameters.getReporterFactory(); |
83 | 0 | final RunListener reporter = reporterFactory.createReporter(); |
84 | 0 | ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter ); |
85 | |
|
86 | 0 | final String smClassName = System.getProperty( "surefire.security.manager" ); |
87 | 0 | if ( smClassName != null ) |
88 | |
{ |
89 | 0 | SecurityManager securityManager = |
90 | |
(SecurityManager) ReflectionUtils.instantiate( this.getClass().getClassLoader(), smClassName ); |
91 | 0 | System.setSecurityManager( securityManager ); |
92 | |
} |
93 | |
|
94 | 0 | for ( Iterator iter = testsToRun.iterator(); iter.hasNext(); ) |
95 | |
{ |
96 | 0 | Class clazz = (Class) iter.next(); |
97 | 0 | SurefireTestSet surefireTestSet = createTestSet( clazz ); |
98 | 0 | executeTestSet( surefireTestSet, reporter, testClassLoader ); |
99 | 0 | } |
100 | |
|
101 | 0 | return reporterFactory.close(); |
102 | |
} |
103 | |
|
104 | |
private SurefireTestSet createTestSet( Class clazz ) |
105 | |
throws TestSetFailedException |
106 | |
{ |
107 | 0 | return reflector.isJUnit3Available() && jUnit3TestChecker.accept( clazz ) |
108 | |
? new JUnitTestSet( clazz, reflector ) |
109 | |
: (SurefireTestSet) new PojoTestSet( clazz ); |
110 | |
|
111 | |
} |
112 | |
|
113 | |
private void executeTestSet( SurefireTestSet testSet, RunListener reporter, ClassLoader classLoader ) |
114 | |
throws ReporterException, TestSetFailedException |
115 | |
{ |
116 | |
|
117 | 0 | ReportEntry report = new SimpleReportEntry( this.getClass().getName(), testSet.getName() ); |
118 | |
|
119 | 0 | reporter.testSetStarting( report ); |
120 | |
|
121 | 0 | testSet.execute( reporter, classLoader ); |
122 | |
|
123 | 0 | reporter.testSetCompleted( report ); |
124 | 0 | } |
125 | |
|
126 | |
private TestsToRun scanClassPath() |
127 | |
{ |
128 | 0 | final TestsToRun scanResult = directoryScanner.locateTestClasses( testClassLoader, testChecker ); |
129 | 0 | return runOrderCalculator.orderTestClasses( scanResult ); |
130 | |
} |
131 | |
|
132 | |
public Iterator getSuites() |
133 | |
{ |
134 | 0 | testsToRun = scanClassPath(); |
135 | 0 | return testsToRun.iterator(); |
136 | |
} |
137 | |
} |