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