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