1 package org.apache.maven.surefire.junitcore;
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 java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import org.apache.maven.surefire.common.junit4.JUnit4RunListenerFactory;
28 import org.apache.maven.surefire.common.junit48.FilterFactory;
29 import org.apache.maven.surefire.common.junit48.JUnit48Reflector;
30 import org.apache.maven.surefire.common.junit48.JUnit48TestChecker;
31 import org.apache.maven.surefire.providerapi.AbstractProvider;
32 import org.apache.maven.surefire.providerapi.ProviderParameters;
33 import org.apache.maven.surefire.report.ConsoleLogger;
34 import org.apache.maven.surefire.report.ConsoleOutputCapture;
35 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
36 import org.apache.maven.surefire.report.ReporterException;
37 import org.apache.maven.surefire.report.ReporterFactory;
38 import org.apache.maven.surefire.report.RunListener;
39 import org.apache.maven.surefire.suite.RunResult;
40 import org.apache.maven.surefire.testset.TestSetFailedException;
41 import org.apache.maven.surefire.util.RunOrderCalculator;
42 import org.apache.maven.surefire.util.ScanResult;
43 import org.apache.maven.surefire.util.ScannerFilter;
44 import org.apache.maven.surefire.util.TestsToRun;
45 import org.apache.maven.surefire.util.internal.StringUtils;
46 import org.junit.runner.manipulation.Filter;
47
48
49
50
51 @SuppressWarnings( { "UnusedDeclaration" } )
52 public class JUnitCoreProvider
53 extends AbstractProvider
54 {
55 private final ClassLoader testClassLoader;
56
57 private final JUnitCoreParameters jUnitCoreParameters;
58
59 private final ScannerFilter scannerFilter;
60
61 private final List<org.junit.runner.notification.RunListener> customRunListeners;
62
63 private final ProviderParameters providerParameters;
64
65 private TestsToRun testsToRun;
66
67 private JUnit48Reflector jUnit48Reflector;
68
69 private RunOrderCalculator runOrderCalculator;
70
71 private String requestedTestMethod;
72
73 private final ScanResult scanResult;
74
75 public JUnitCoreProvider( ProviderParameters providerParameters )
76 {
77 this.providerParameters = providerParameters;
78 this.testClassLoader = providerParameters.getTestClassLoader();
79 this.scanResult = providerParameters.getScanResult();
80 this.runOrderCalculator = providerParameters.getRunOrderCalculator();
81 this.jUnitCoreParameters = new JUnitCoreParameters( providerParameters.getProviderProperties() );
82 this.scannerFilter = new JUnit48TestChecker( testClassLoader );
83 this.requestedTestMethod = providerParameters.getTestRequest().getRequestedTestMethod();
84
85 customRunListeners =
86 JUnit4RunListenerFactory.createCustomListeners( providerParameters.getProviderProperties().getProperty( "listener" ) );
87 jUnit48Reflector = new JUnit48Reflector( testClassLoader );
88 }
89
90 public Boolean isRunnable()
91 {
92 return Boolean.TRUE;
93 }
94
95 public Iterator getSuites()
96 {
97 testsToRun = scanClassPath();
98 return testsToRun.iterator();
99 }
100
101 private boolean isSingleThreaded()
102 {
103 return jUnitCoreParameters.isNoThreading();
104 }
105
106 public RunResult invoke( Object forkTestSet )
107 throws TestSetFailedException, ReporterException
108 {
109 final ReporterFactory reporterFactory = providerParameters.getReporterFactory();
110
111 final ConsoleLogger consoleLogger = providerParameters.getConsoleLogger();
112
113 Filter filter = jUnit48Reflector.isJUnit48Available() ? createJUnit48Filter() : null;
114
115 if ( testsToRun == null )
116 {
117 if ( forkTestSet instanceof TestsToRun )
118 {
119 testsToRun = (TestsToRun) forkTestSet;
120 }
121 else if ( forkTestSet instanceof Class )
122 {
123 Class theClass = (Class) forkTestSet;
124 testsToRun = TestsToRun.fromClass( theClass );
125 }
126 else
127 {
128 testsToRun = scanClassPath();
129 }
130 }
131
132 org.junit.runner.notification.RunListener jUnit4RunListener = getRunListener( reporterFactory, consoleLogger );
133 customRunListeners.add( 0, jUnit4RunListener );
134 JUnitCoreWrapper.execute( testsToRun, jUnitCoreParameters, customRunListeners, filter );
135 return reporterFactory.close();
136 }
137
138 private org.junit.runner.notification.RunListener getRunListener( ReporterFactory reporterFactory,
139 ConsoleLogger consoleLogger )
140 throws TestSetFailedException
141 {
142 org.junit.runner.notification.RunListener jUnit4RunListener;
143 if ( isSingleThreaded() )
144 {
145 NonConcurrentRunListener rm = new NonConcurrentRunListener( reporterFactory.createReporter() );
146 ConsoleOutputCapture.startCapture( rm );
147 jUnit4RunListener = rm;
148 }
149 else
150 {
151 final Map<String, TestSet> testSetMap = new ConcurrentHashMap<String, TestSet>();
152
153 RunListener listener =
154 ConcurrentRunListener.createInstance( testSetMap, reporterFactory,
155 isParallelTypes(),
156 isParallelMethodsAndTypes(), consoleLogger );
157 ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) listener );
158
159 jUnit4RunListener = new JUnitCoreRunListener( listener, testSetMap );
160 }
161 return jUnit4RunListener;
162 }
163
164 private boolean isParallelMethodsAndTypes()
165 {
166 return jUnitCoreParameters.isParallelMethod() && isParallelTypes();
167 }
168
169 private boolean isParallelTypes()
170 {
171 return jUnitCoreParameters.isParallelClasses() || jUnitCoreParameters.isParallelSuites();
172 }
173
174 private Filter createJUnit48Filter()
175 {
176 final FilterFactory filterFactory = new FilterFactory( testClassLoader );
177 Filter groupFilter = filterFactory.createGroupFilter( providerParameters.getProviderProperties() );
178 return isMethodFilterSpecified() ? filterFactory.and( groupFilter,
179 filterFactory.createMethodFilter( requestedTestMethod ) )
180 : groupFilter;
181 }
182
183 private TestsToRun scanClassPath()
184 {
185 final TestsToRun scanned = scanResult.applyFilter( scannerFilter, testClassLoader );
186 return runOrderCalculator.orderTestClasses( scanned );
187 }
188
189 private boolean isMethodFilterSpecified()
190 {
191 return !StringUtils.isBlank( requestedTestMethod );
192 }
193 }