1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.booter;
20
21 import java.io.File;
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26
27 import junit.framework.TestCase;
28 import org.apache.maven.surefire.api.booter.BaseProviderFactory;
29 import org.apache.maven.surefire.api.provider.ProviderParameters;
30 import org.apache.maven.surefire.api.provider.SurefireProvider;
31 import org.apache.maven.surefire.api.report.ReporterConfiguration;
32 import org.apache.maven.surefire.api.report.ReporterFactory;
33 import org.apache.maven.surefire.api.report.TestReportListener;
34 import org.apache.maven.surefire.api.suite.RunResult;
35 import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
36 import org.apache.maven.surefire.api.testset.RunOrderParameters;
37 import org.apache.maven.surefire.api.testset.TestArtifactInfo;
38 import org.apache.maven.surefire.api.testset.TestListResolver;
39 import org.apache.maven.surefire.api.testset.TestRequest;
40 import org.apache.maven.surefire.api.util.RunOrder;
41
42 import static java.util.Arrays.asList;
43 import static org.apache.maven.surefire.api.cli.CommandLineOption.LOGGING_LEVEL_DEBUG;
44 import static org.apache.maven.surefire.api.cli.CommandLineOption.SHOW_ERRORS;
45
46
47
48
49 public class SurefireReflectorTest extends TestCase {
50 public void testShouldCreateFactoryWithoutException() {
51 ReporterFactory factory = new ReporterFactory() {
52 @Override
53 public TestReportListener createTestReportListener() {
54 return null;
55 }
56
57 @Override
58 public RunResult close() {
59 return null;
60 }
61 };
62 ClassLoader cl = Thread.currentThread().getContextClassLoader();
63 SurefireReflector reflector = new SurefireReflector(cl);
64 BaseProviderFactory bpf = (BaseProviderFactory) reflector.createBooterConfiguration(cl, true);
65 bpf.setReporterFactory(factory);
66 assertNotNull(bpf.getReporterFactory());
67 assertSame(factory, bpf.getReporterFactory());
68 }
69
70 public void testSetDirectoryScannerParameters() {
71 SurefireReflector surefireReflector = getReflector();
72 Object foo = getFoo();
73
74 DirectoryScannerParameters directoryScannerParameters = new DirectoryScannerParameters(
75 new File("ABC"), new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(), "hourly");
76 surefireReflector.setDirectoryScannerParameters(foo, directoryScannerParameters);
77 assertTrue(isCalled(foo));
78 assertNotNull(((Foo) foo).getDirectoryScannerParameters());
79 }
80
81 public void testNullSetDirectoryScannerParameters() {
82 SurefireReflector surefireReflector = getReflector();
83 Object foo = getFoo();
84
85 surefireReflector.setDirectoryScannerParameters(foo, null);
86 assertTrue(isCalled(foo));
87 assertNull(((Foo) foo).getDirectoryScannerParameters());
88 }
89
90 public void testSetIfDirScannerAware() {
91 SurefireReflector surefireReflector = getReflector();
92 Object foo = getFoo();
93
94 DirectoryScannerParameters directoryScannerParameters = new DirectoryScannerParameters(
95 new File("ABC"), new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(), "hourly");
96 surefireReflector.setIfDirScannerAware(foo, directoryScannerParameters);
97 assertTrue(isCalled(foo));
98 }
99
100 public void testRunOrderParameters() {
101 SurefireReflector surefireReflector = getReflector();
102 Object foo = getFoo();
103
104 RunOrderParameters runOrderParameters = new RunOrderParameters(RunOrder.DEFAULT, new File("."));
105 surefireReflector.setRunOrderParameters(foo, runOrderParameters);
106 assertTrue(isCalled(foo));
107 }
108
109 public void testRunOrderParametersWithRunOrderRandomSeed() {
110 SurefireReflector surefireReflector = getReflector();
111 Object foo = getFoo();
112
113
114 Long runOrderRandomSeed = 5L;
115
116 RunOrderParameters runOrderParameters =
117 new RunOrderParameters(RunOrder.DEFAULT, new File("."), runOrderRandomSeed);
118 surefireReflector.setRunOrderParameters(foo, runOrderParameters);
119 assertTrue(isCalled(foo));
120 }
121
122 public void testNullRunOrderParameters() {
123 SurefireReflector surefireReflector = getReflector();
124 Object foo = getFoo();
125
126 surefireReflector.setRunOrderParameters(foo, null);
127 assertTrue(isCalled(foo));
128 try {
129 ((Foo) foo).getRunOrderCalculator();
130 } catch (NullPointerException e) {
131 return;
132 }
133 fail();
134 }
135
136 public void testTestSuiteDefinition() {
137 SurefireReflector surefireReflector = getReflector();
138 Object foo = getFoo();
139
140 TestRequest testSuiteDefinition = new TestRequest(
141 asList(new File("file1"), new File("file2")),
142 new File("TestSOurce"),
143 new TestListResolver("aUserRequestedTest#aMethodRequested"));
144 surefireReflector.setTestSuiteDefinition(foo, testSuiteDefinition);
145 assertTrue(isCalled(foo));
146 assertNotNull(((Foo) foo).getTestRequest());
147 }
148
149 public void testNullTestSuiteDefinition() {
150 SurefireReflector surefireReflector = getReflector();
151 Object foo = getFoo();
152 surefireReflector.setTestSuiteDefinition(foo, null);
153 assertTrue(isCalled(foo));
154 assertNull(((Foo) foo).getTestRequest());
155 }
156
157 public void testProviderProperties() {
158 SurefireReflector surefireReflector = getReflector();
159 Object foo = getFoo();
160
161 surefireReflector.setProviderProperties(foo, new HashMap<>());
162 assertTrue(isCalled(foo));
163 }
164
165 public void testReporterConfiguration() {
166 SurefireReflector surefireReflector = getReflector();
167 Object foo = getFoo();
168
169 ReporterConfiguration reporterConfiguration = getReporterConfiguration();
170 surefireReflector.setReporterConfigurationAware(foo, reporterConfiguration);
171 assertTrue(isCalled(foo));
172 }
173
174 private ReporterConfiguration getReporterConfiguration() {
175 return new ReporterConfiguration(new File("CDE"), true);
176 }
177
178 public void testTestClassLoader() {
179 SurefireReflector surefireReflector = getReflector();
180 Object foo = getFoo();
181
182 surefireReflector.setTestClassLoader(foo, getClass().getClassLoader());
183 assertTrue(isCalled(foo));
184 }
185
186 public void testTestClassLoaderAware() {
187 SurefireReflector surefireReflector = getReflector();
188 Object foo = getFoo();
189
190 surefireReflector.setTestClassLoaderAware(foo, getClass().getClassLoader());
191 assertTrue(isCalled(foo));
192 }
193
194 public void testArtifactInfo() {
195 SurefireReflector surefireReflector = getReflector();
196 Object foo = getFoo();
197
198 TestArtifactInfo testArtifactInfo = new TestArtifactInfo("12.3", "test");
199 surefireReflector.setTestArtifactInfo(foo, testArtifactInfo);
200 assertTrue(isCalled(foo));
201 }
202
203 public void testNullArtifactInfo() {
204 SurefireReflector surefireReflector = getReflector();
205 Object foo = getFoo();
206
207 surefireReflector.setTestArtifactInfo(foo, null);
208 assertTrue(isCalled(foo));
209 assertNull(((Foo) foo).getTestArtifactInfo());
210 }
211
212 public void testArtifactInfoAware() {
213 SurefireReflector surefireReflector = getReflector();
214 Object foo = getFoo();
215
216 TestArtifactInfo testArtifactInfo = new TestArtifactInfo("12.3", "test");
217 surefireReflector.setTestArtifactInfoAware(foo, testArtifactInfo);
218 assertTrue(isCalled(foo));
219 assertEquals(testArtifactInfo.getClassifier(), "test");
220 assertEquals(testArtifactInfo.getVersion(), "12.3");
221 }
222
223 public void testReporterFactory() {
224 SurefireReflector surefireReflector = getReflector();
225 Object foo = getFoo();
226
227 ReporterFactory reporterFactory = new ReporterFactory() {
228 @Override
229 public TestReportListener createTestReportListener() {
230 return null;
231 }
232
233 @Override
234 public RunResult close() {
235 return null;
236 }
237 };
238
239 surefireReflector.setReporterFactory(foo, reporterFactory);
240 assertTrue(isCalled(foo));
241 }
242
243 public void testReporterFactoryAware() {
244 SurefireReflector surefireReflector = getReflector();
245 Object foo = getFoo();
246
247 ReporterFactory reporterFactory = new ReporterFactory() {
248 @Override
249 public TestReportListener createTestReportListener() {
250 return null;
251 }
252
253 @Override
254 public RunResult close() {
255 return null;
256 }
257 };
258
259 surefireReflector.setReporterFactoryAware(foo, reporterFactory);
260 assertTrue(isCalled(foo));
261 assertSame(((Foo) foo).getReporterFactory(), reporterFactory);
262 }
263
264 @SuppressWarnings("checkstyle:magicnumber")
265 public void testConvertIfRunResult() {
266 RunResult runResult = new RunResult(20, 1, 2, 3, 4, "IOException", true);
267 SurefireReflector reflector =
268 new SurefireReflector(Thread.currentThread().getContextClassLoader());
269 RunResult obj = (RunResult) reflector.convertIfRunResult(runResult);
270 assertEquals(obj.getCompletedCount(), 20);
271 assertEquals(obj.getErrors(), 1);
272 assertEquals(obj.getFailures(), 2);
273 assertEquals(obj.getSkipped(), 3);
274 assertFalse(obj.isErrorFree());
275 assertFalse(obj.isInternalError());
276 assertEquals(obj.getFailsafeCode(), (Integer) RunResult.FAILURE);
277
278 assertNull(reflector.convertIfRunResult(null));
279 assertEquals(reflector.convertIfRunResult(""), "");
280 }
281
282 public void testInstantiateProvider() {
283 SurefireReflector reflector =
284 new SurefireReflector(Thread.currentThread().getContextClassLoader());
285 Object booterParams = getFoo();
286 Object provider = reflector.instantiateProvider(DummyProvider.class.getName(), booterParams);
287 assertNotNull(provider);
288 assertEquals(provider.getClass(), DummyProvider.class);
289 }
290
291 public void testSetMainCliOptions() {
292 SurefireReflector reflector =
293 new SurefireReflector(Thread.currentThread().getContextClassLoader());
294 Object booterParams = getFoo();
295 reflector.setMainCliOptions(booterParams, asList(SHOW_ERRORS, LOGGING_LEVEL_DEBUG));
296 assertEquals(((BaseProviderFactory) booterParams).getMainCliOptions().size(), 2);
297 assertEquals(((BaseProviderFactory) booterParams).getMainCliOptions().get(0), SHOW_ERRORS);
298 assertEquals(((BaseProviderFactory) booterParams).getMainCliOptions().get(1), LOGGING_LEVEL_DEBUG);
299 }
300
301 public void testSetSkipAfterFailureCount() {
302 SurefireReflector reflector =
303 new SurefireReflector(Thread.currentThread().getContextClassLoader());
304 Foo booterParams = (Foo) getFoo();
305 assertEquals(booterParams.getSkipAfterFailureCount(), 0);
306 reflector.setSkipAfterFailureCount(booterParams, 5);
307 assertEquals(booterParams.getSkipAfterFailureCount(), 5);
308 }
309
310 @SuppressWarnings("checkstyle:magicnumber")
311 public void testSetSystemExitTimeout() {
312 SurefireReflector reflector =
313 new SurefireReflector(Thread.currentThread().getContextClassLoader());
314 Foo booterParams = (Foo) getFoo();
315 assertNull(booterParams.getSystemExitTimeout());
316 reflector.setSystemExitTimeout(booterParams, 60);
317 assertEquals(booterParams.getSystemExitTimeout(), (Integer) 60);
318 }
319
320 public void testSetTestSuiteDefinitionAware() {
321 SurefireReflector reflector =
322 new SurefireReflector(Thread.currentThread().getContextClassLoader());
323 Foo booterParams = (Foo) getFoo();
324 TestRequest request = new TestRequest(Collections.emptyList(), null, null);
325 reflector.setTestSuiteDefinitionAware(booterParams, request);
326 assertTrue(booterParams.isCalled());
327 assertNotNull(booterParams.getTestRequest());
328 assertTrue(booterParams.getTestRequest().getSuiteXmlFiles().isEmpty());
329 assertNull(booterParams.getTestRequest().getTestSourceDirectory());
330 assertNull(booterParams.getTestRequest().getTestListResolver());
331 assertEquals(booterParams.getTestRequest().getRerunFailingTestsCount(), 0);
332 }
333
334 public void testSetProviderPropertiesAware() {
335 SurefireReflector reflector =
336 new SurefireReflector(Thread.currentThread().getContextClassLoader());
337 Foo booterParams = (Foo) getFoo();
338 reflector.setProviderPropertiesAware(booterParams, Collections.singletonMap("k", "v"));
339 assertTrue(booterParams.isCalled());
340 assertNotNull(booterParams.getProviderProperties());
341 assertEquals(booterParams.getProviderProperties().size(), 1);
342 assertEquals(booterParams.getProviderProperties().get("k"), "v");
343 }
344
345 private SurefireReflector getReflector() {
346 return new SurefireReflector(getClass().getClassLoader());
347 }
348
349 private Object getFoo() {
350 return new Foo();
351 }
352
353 private Boolean isCalled(Object foo) {
354 final Method isCalled;
355 try {
356 isCalled = foo.getClass().getMethod("isCalled");
357 return (Boolean) isCalled.invoke(foo);
358 } catch (ReflectiveOperationException e) {
359 throw new RuntimeException(e);
360 }
361 }
362
363
364
365
366 public static final class DummyProvider implements SurefireProvider {
367 public DummyProvider(ProviderParameters providerParameters) {}
368
369 @Override
370 public Iterable<Class<?>> getSuites() {
371 return null;
372 }
373
374 @Override
375 public RunResult invoke(Object forkTestSet) {
376 return null;
377 }
378
379 @Override
380 public void cancel() {}
381 }
382 }