View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.TestOutputReportEntry;
34  import org.apache.maven.surefire.api.report.TestReportListener;
35  import org.apache.maven.surefire.api.suite.RunResult;
36  import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
37  import org.apache.maven.surefire.api.testset.RunOrderParameters;
38  import org.apache.maven.surefire.api.testset.TestArtifactInfo;
39  import org.apache.maven.surefire.api.testset.TestListResolver;
40  import org.apache.maven.surefire.api.testset.TestRequest;
41  import org.apache.maven.surefire.api.util.RunOrder;
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  
47  /**
48   *
49   */
50  public class SurefireReflectorTest extends TestCase {
51      public void testShouldCreateFactoryWithoutException() {
52          ReporterFactory factory = new ReporterFactory() {
53              @Override
54              public TestReportListener<TestOutputReportEntry> createTestReportListener() {
55                  return null;
56              }
57  
58              @Override
59              public RunResult close() {
60                  return null;
61              }
62          };
63          ClassLoader cl = Thread.currentThread().getContextClassLoader();
64          SurefireReflector reflector = new SurefireReflector(cl);
65          BaseProviderFactory bpf = (BaseProviderFactory) reflector.createBooterConfiguration(cl, true);
66          bpf.setReporterFactory(factory);
67          assertNotNull(bpf.getReporterFactory());
68          assertSame(factory, bpf.getReporterFactory());
69      }
70  
71      public void testSetDirectoryScannerParameters() {
72          SurefireReflector surefireReflector = getReflector();
73          Object foo = getFoo();
74  
75          DirectoryScannerParameters directoryScannerParameters = new DirectoryScannerParameters(
76                  new File("ABC"), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null);
77          surefireReflector.setDirectoryScannerParameters(foo, directoryScannerParameters);
78          assertTrue(isCalled(foo));
79          assertNotNull(((Foo) foo).getDirectoryScannerParameters());
80      }
81  
82      public void testNullSetDirectoryScannerParameters() {
83          SurefireReflector surefireReflector = getReflector();
84          Object foo = getFoo();
85  
86          surefireReflector.setDirectoryScannerParameters(foo, null);
87          assertTrue(isCalled(foo));
88          assertNull(((Foo) foo).getDirectoryScannerParameters());
89      }
90  
91      public void testSetIfDirScannerAware() {
92          SurefireReflector surefireReflector = getReflector();
93          Object foo = getFoo();
94  
95          DirectoryScannerParameters directoryScannerParameters = new DirectoryScannerParameters(
96                  new File("ABC"), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null);
97          surefireReflector.setIfDirScannerAware(foo, directoryScannerParameters);
98          assertTrue(isCalled(foo));
99      }
100 
101     public void testRunOrderParameters() {
102         SurefireReflector surefireReflector = getReflector();
103         Object foo = getFoo();
104 
105         RunOrderParameters runOrderParameters = new RunOrderParameters(RunOrder.DEFAULT, new File("."));
106         surefireReflector.setRunOrderParameters(foo, runOrderParameters);
107         assertTrue(isCalled(foo));
108     }
109 
110     public void testRunOrderParametersWithRunOrderRandomSeed() {
111         SurefireReflector surefireReflector = getReflector();
112         Object foo = getFoo();
113 
114         // Arbitrary random seed that should be ignored because RunOrder is not RANDOM
115         Long runOrderRandomSeed = 5L;
116 
117         RunOrderParameters runOrderParameters =
118                 new RunOrderParameters(RunOrder.DEFAULT, new File("."), runOrderRandomSeed);
119         surefireReflector.setRunOrderParameters(foo, runOrderParameters);
120         assertTrue(isCalled(foo));
121     }
122 
123     public void testNullRunOrderParameters() {
124         SurefireReflector surefireReflector = getReflector();
125         Object foo = getFoo();
126 
127         surefireReflector.setRunOrderParameters(foo, null);
128         assertTrue(isCalled(foo));
129         try {
130             ((Foo) foo).getRunOrderCalculator();
131         } catch (NullPointerException e) {
132             return;
133         }
134         fail();
135     }
136 
137     public void testTestSuiteDefinition() {
138         SurefireReflector surefireReflector = getReflector();
139         Object foo = getFoo();
140 
141         TestRequest testSuiteDefinition =
142                 new TestRequest(new File("TestSOurce"), new TestListResolver("aUserRequestedTest#aMethodRequested"), 0);
143         surefireReflector.setTestSuiteDefinition(foo, testSuiteDefinition);
144         assertTrue(isCalled(foo));
145         assertNotNull(((Foo) foo).getTestRequest());
146     }
147 
148     public void testNullTestSuiteDefinition() {
149         SurefireReflector surefireReflector = getReflector();
150         Object foo = getFoo();
151         surefireReflector.setTestSuiteDefinition(foo, null);
152         assertTrue(isCalled(foo));
153         assertNull(((Foo) foo).getTestRequest());
154     }
155 
156     public void testProviderProperties() {
157         SurefireReflector surefireReflector = getReflector();
158         Object foo = getFoo();
159 
160         surefireReflector.setProviderProperties(foo, new HashMap<>());
161         assertTrue(isCalled(foo));
162     }
163 
164     public void testReporterConfiguration() {
165         SurefireReflector surefireReflector = getReflector();
166         Object foo = getFoo();
167 
168         ReporterConfiguration reporterConfiguration = getReporterConfiguration();
169         surefireReflector.setReporterConfigurationAware(foo, reporterConfiguration);
170         assertTrue(isCalled(foo));
171     }
172 
173     private ReporterConfiguration getReporterConfiguration() {
174         return new ReporterConfiguration(new File("CDE"), true);
175     }
176 
177     public void testTestClassLoader() {
178         SurefireReflector surefireReflector = getReflector();
179         Object foo = getFoo();
180 
181         surefireReflector.setTestClassLoader(foo, getClass().getClassLoader());
182         assertTrue(isCalled(foo));
183     }
184 
185     public void testTestClassLoaderAware() {
186         SurefireReflector surefireReflector = getReflector();
187         Object foo = getFoo();
188 
189         surefireReflector.setTestClassLoaderAware(foo, getClass().getClassLoader());
190         assertTrue(isCalled(foo));
191     }
192 
193     public void testArtifactInfo() {
194         SurefireReflector surefireReflector = getReflector();
195         Object foo = getFoo();
196 
197         TestArtifactInfo testArtifactInfo = new TestArtifactInfo("12.3", "test");
198         surefireReflector.setTestArtifactInfo(foo, testArtifactInfo);
199         assertTrue(isCalled(foo));
200     }
201 
202     public void testNullArtifactInfo() {
203         SurefireReflector surefireReflector = getReflector();
204         Object foo = getFoo();
205 
206         surefireReflector.setTestArtifactInfo(foo, null);
207         assertTrue(isCalled(foo));
208         assertNull(((Foo) foo).getTestArtifactInfo());
209     }
210 
211     public void testArtifactInfoAware() {
212         SurefireReflector surefireReflector = getReflector();
213         Object foo = getFoo();
214 
215         TestArtifactInfo testArtifactInfo = new TestArtifactInfo("12.3", "test");
216         surefireReflector.setTestArtifactInfoAware(foo, testArtifactInfo);
217         assertTrue(isCalled(foo));
218         assertEquals(testArtifactInfo.getClassifier(), "test");
219         assertEquals(testArtifactInfo.getVersion(), "12.3");
220     }
221 
222     public void testReporterFactory() {
223         SurefireReflector surefireReflector = getReflector();
224         Object foo = getFoo();
225 
226         ReporterFactory reporterFactory = new ReporterFactory() {
227             @Override
228             public TestReportListener<TestOutputReportEntry> createTestReportListener() {
229                 return null;
230             }
231 
232             @Override
233             public RunResult close() {
234                 return null;
235             }
236         };
237 
238         surefireReflector.setReporterFactory(foo, reporterFactory);
239         assertTrue(isCalled(foo));
240     }
241 
242     public void testReporterFactoryAware() {
243         SurefireReflector surefireReflector = getReflector();
244         Object foo = getFoo();
245 
246         ReporterFactory reporterFactory = new ReporterFactory() {
247             @Override
248             public TestReportListener<TestOutputReportEntry> createTestReportListener() {
249                 return null;
250             }
251 
252             @Override
253             public RunResult close() {
254                 return null;
255             }
256         };
257 
258         surefireReflector.setReporterFactoryAware(foo, reporterFactory);
259         assertTrue(isCalled(foo));
260         assertSame(((Foo) foo).getReporterFactory(), reporterFactory);
261     }
262 
263     @SuppressWarnings("checkstyle:magicnumber")
264     public void testConvertIfRunResult() {
265         RunResult runResult = new RunResult(20, 1, 2, 3, 4, "IOException", true);
266         SurefireReflector reflector =
267                 new SurefireReflector(Thread.currentThread().getContextClassLoader());
268         RunResult obj = (RunResult) reflector.convertIfRunResult(runResult);
269         assertEquals(obj.getCompletedCount(), 20);
270         assertEquals(obj.getErrors(), 1);
271         assertEquals(obj.getFailures(), 2);
272         assertEquals(obj.getSkipped(), 3);
273         assertFalse(obj.isErrorFree());
274         assertFalse(obj.isInternalError());
275         assertEquals(obj.getFailsafeCode(), (Integer) RunResult.FAILURE);
276 
277         assertNull(reflector.convertIfRunResult(null));
278         assertEquals(reflector.convertIfRunResult(""), "");
279     }
280 
281     public void testInstantiateProvider() {
282         SurefireReflector reflector =
283                 new SurefireReflector(Thread.currentThread().getContextClassLoader());
284         Object booterParams = getFoo();
285         Object provider = reflector.instantiateProvider(DummyProvider.class.getName(), booterParams);
286         assertNotNull(provider);
287         assertEquals(provider.getClass(), DummyProvider.class);
288     }
289 
290     public void testSetMainCliOptions() {
291         SurefireReflector reflector =
292                 new SurefireReflector(Thread.currentThread().getContextClassLoader());
293         Object booterParams = getFoo();
294         reflector.setMainCliOptions(booterParams, asList(SHOW_ERRORS, LOGGING_LEVEL_DEBUG));
295         assertEquals(((BaseProviderFactory) booterParams).getMainCliOptions().size(), 2);
296         assertEquals(((BaseProviderFactory) booterParams).getMainCliOptions().get(0), SHOW_ERRORS);
297         assertEquals(((BaseProviderFactory) booterParams).getMainCliOptions().get(1), LOGGING_LEVEL_DEBUG);
298     }
299 
300     public void testSetSkipAfterFailureCount() {
301         SurefireReflector reflector =
302                 new SurefireReflector(Thread.currentThread().getContextClassLoader());
303         Foo booterParams = (Foo) getFoo();
304         assertEquals(booterParams.getSkipAfterFailureCount(), 0);
305         reflector.setSkipAfterFailureCount(booterParams, 5);
306         assertEquals(booterParams.getSkipAfterFailureCount(), 5);
307     }
308 
309     @SuppressWarnings("checkstyle:magicnumber")
310     public void testSetSystemExitTimeout() {
311         SurefireReflector reflector =
312                 new SurefireReflector(Thread.currentThread().getContextClassLoader());
313         Foo booterParams = (Foo) getFoo();
314         assertNull(booterParams.getSystemExitTimeout());
315         reflector.setSystemExitTimeout(booterParams, 60);
316         assertEquals(booterParams.getSystemExitTimeout(), (Integer) 60);
317     }
318 
319     public void testSetProviderPropertiesAware() {
320         SurefireReflector reflector =
321                 new SurefireReflector(Thread.currentThread().getContextClassLoader());
322         Foo booterParams = (Foo) getFoo();
323         reflector.setProviderPropertiesAware(booterParams, Collections.singletonMap("k", "v"));
324         assertTrue(booterParams.isCalled());
325         assertNotNull(booterParams.getProviderProperties());
326         assertEquals(booterParams.getProviderProperties().size(), 1);
327         assertEquals(booterParams.getProviderProperties().get("k"), "v");
328     }
329 
330     private SurefireReflector getReflector() {
331         return new SurefireReflector(getClass().getClassLoader());
332     }
333 
334     private Object getFoo() { // Todo: Setup a different classloader so we can really test crossing
335         return new Foo();
336     }
337 
338     private Boolean isCalled(Object foo) {
339         final Method isCalled;
340         try {
341             isCalled = foo.getClass().getMethod("isCalled");
342             return (Boolean) isCalled.invoke(foo);
343         } catch (ReflectiveOperationException e) {
344             throw new RuntimeException(e);
345         }
346     }
347 
348     /**
349      *
350      */
351     public static final class DummyProvider implements SurefireProvider {
352         public DummyProvider(ProviderParameters providerParameters) {}
353 
354         @Override
355         public Iterable<Class<?>> getSuites() {
356             return null;
357         }
358 
359         @Override
360         public RunResult invoke(Object forkTestSet) {
361             return null;
362         }
363 
364         @Override
365         public void cancel() {}
366     }
367 }