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.ReporterFactory;
31 import org.apache.maven.surefire.report.RunListener;
32 import org.apache.maven.surefire.report.SimpleReportEntry;
33 import org.apache.maven.surefire.suite.RunResult;
34 import org.apache.maven.surefire.testset.TestSetFailedException;
35 import org.apache.maven.surefire.util.ReflectionUtils;
36 import org.apache.maven.surefire.util.RunOrderCalculator;
37 import org.apache.maven.surefire.util.ScanResult;
38 import org.apache.maven.surefire.util.TestsToRun;
39
40
41
42
43 public class JUnit3Provider
44 extends AbstractProvider
45 {
46 private final ClassLoader testClassLoader;
47
48 private final PojoAndJUnit3Checker testChecker;
49
50 private final JUnit3TestChecker jUnit3TestChecker;
51
52 private final JUnit3Reflector reflector;
53
54 private final ProviderParameters providerParameters;
55
56 private final RunOrderCalculator runOrderCalculator;
57
58 private TestsToRun testsToRun;
59
60 private final ScanResult scanResult;
61
62 public JUnit3Provider( ProviderParameters booterParameters )
63 {
64 this.providerParameters = booterParameters;
65 this.testClassLoader = booterParameters.getTestClassLoader();
66 this.scanResult = booterParameters.getScanResult();
67 this.runOrderCalculator = booterParameters.getRunOrderCalculator();
68 this.reflector = new JUnit3Reflector( testClassLoader );
69 jUnit3TestChecker = new JUnit3TestChecker( testClassLoader );
70 this.testChecker = new PojoAndJUnit3Checker( jUnit3TestChecker );
71 }
72
73 public RunResult invoke( Object forkTestSet )
74 throws TestSetFailedException
75 {
76 if ( testsToRun == null )
77 {
78 if ( forkTestSet instanceof TestsToRun )
79 {
80 testsToRun = (TestsToRun) forkTestSet;
81 }
82 else if ( forkTestSet instanceof Class )
83 {
84 testsToRun = TestsToRun.fromClass( (Class<?>) forkTestSet );
85 }
86 else
87 {
88 testsToRun = scanClassPath();
89 }
90 }
91
92 ReporterFactory reporterFactory = providerParameters.getReporterFactory();
93 final RunListener reporter = reporterFactory.createReporter();
94 ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
95
96 final String smClassName = System.getProperty( "surefire.security.manager" );
97 if ( smClassName != null )
98 {
99 SecurityManager securityManager =
100 (SecurityManager) ReflectionUtils.instantiate( this.getClass().getClassLoader(), smClassName );
101 System.setSecurityManager( securityManager );
102 }
103
104 for ( Class clazz : testsToRun )
105 {
106 SurefireTestSet surefireTestSet = createTestSet( clazz );
107 executeTestSet( surefireTestSet, reporter, testClassLoader );
108 }
109
110 return reporterFactory.close();
111 }
112
113 private SurefireTestSet createTestSet( Class<?> clazz )
114 throws TestSetFailedException
115 {
116 return reflector.isJUnit3Available() && jUnit3TestChecker.accept( clazz )
117 ? new JUnitTestSet( clazz, reflector )
118 : new PojoTestSet( clazz );
119
120 }
121
122 private void executeTestSet( SurefireTestSet testSet, RunListener reporter, ClassLoader classLoader )
123 throws TestSetFailedException
124 {
125
126 ReportEntry report = new SimpleReportEntry( this.getClass().getName(), testSet.getName() );
127
128 reporter.testSetStarting( report );
129
130 testSet.execute( reporter, classLoader );
131
132 reporter.testSetCompleted( report );
133 }
134
135 private TestsToRun scanClassPath()
136 {
137 final TestsToRun testsToRun = scanResult.applyFilter( testChecker, testClassLoader );
138 return runOrderCalculator.orderTestClasses( testsToRun );
139 }
140
141 public Iterator getSuites()
142 {
143 testsToRun = scanClassPath();
144 return testsToRun.iterator();
145 }
146 }