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