View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.lang.reflect.Constructor;
24  import java.lang.reflect.InvocationHandler;
25  import java.lang.reflect.Method;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
32  import org.apache.maven.plugin.surefire.log.api.ConsoleLoggerDecorator;
33  import org.apache.maven.surefire.cli.CommandLineOption;
34  import org.apache.maven.surefire.providerapi.ProviderParameters;
35  import org.apache.maven.surefire.report.ReporterConfiguration;
36  import org.apache.maven.surefire.report.ReporterFactory;
37  import org.apache.maven.surefire.suite.RunResult;
38  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
39  import org.apache.maven.surefire.testset.RunOrderParameters;
40  import org.apache.maven.surefire.testset.TestArtifactInfo;
41  import org.apache.maven.surefire.testset.TestListResolver;
42  import org.apache.maven.surefire.testset.TestRequest;
43  import org.apache.maven.surefire.util.RunOrder;
44  import org.apache.maven.surefire.util.SurefireReflectionException;
45  
46  import javax.annotation.Nonnull;
47  
48  import static java.util.Collections.checkedList;
49  
50  import static org.apache.maven.surefire.util.ReflectionUtils.getConstructor;
51  import static org.apache.maven.surefire.util.ReflectionUtils.getMethod;
52  import static org.apache.maven.surefire.util.ReflectionUtils.invokeGetter;
53  import static org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray;
54  import static org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg;
55  import static org.apache.maven.surefire.util.ReflectionUtils.invokeSetter;
56  import static org.apache.maven.surefire.util.ReflectionUtils.instantiateTwoArgs;
57  import static org.apache.maven.surefire.util.ReflectionUtils.newInstance;
58  
59  /**
60   * Does reflection based invocation of the surefire methods.
61   * <br>
62   * This is to avoid complications with linkage issues
63   *
64   * @author Kristian Rosenvold
65   */
66  public class SurefireReflector
67  {
68      private final ClassLoader surefireClassLoader;
69  
70      private final Class<?> reporterConfiguration;
71  
72      private final Class<?> testRequest;
73  
74      private final Class<?> testArtifactInfo;
75  
76      private final Class<?> testArtifactInfoAware;
77  
78      private final Class<?> directoryScannerParameters;
79  
80      private final Class<?> runOrderParameters;
81  
82      private final Class<?> directoryScannerParametersAware;
83  
84      private final Class<?> testSuiteDefinitionAware;
85  
86      private final Class<?> testClassLoaderAware;
87  
88      private final Class<?> reporterConfigurationAware;
89  
90      private final Class<?> providerPropertiesAware;
91  
92      private final Class<?> runResult;
93  
94      private final Class<?> booterParameters;
95  
96      private final Class<?> reporterFactory;
97  
98      private final Class<?> testListResolver;
99  
100     private final Class<?> mainCliOptions;
101 
102     private final Class<Enum> commandLineOptionsClass;
103 
104     private final Class<?> shutdownAwareClass;
105 
106     private final Class<Enum> shutdownClass;
107 
108     @SuppressWarnings( "unchecked" )
109     public SurefireReflector( ClassLoader surefireClassLoader )
110     {
111         this.surefireClassLoader = surefireClassLoader;
112         try
113         {
114             reporterConfiguration = surefireClassLoader.loadClass( ReporterConfiguration.class.getName() );
115             testRequest = surefireClassLoader.loadClass( TestRequest.class.getName() );
116             testArtifactInfo = surefireClassLoader.loadClass( TestArtifactInfo.class.getName() );
117             testArtifactInfoAware = surefireClassLoader.loadClass( TestArtifactInfoAware.class.getName() );
118             directoryScannerParameters = surefireClassLoader.loadClass( DirectoryScannerParameters.class.getName() );
119             runOrderParameters = surefireClassLoader.loadClass( RunOrderParameters.class.getName() );
120             directoryScannerParametersAware =
121                 surefireClassLoader.loadClass( DirectoryScannerParametersAware.class.getName() );
122             testSuiteDefinitionAware = surefireClassLoader.loadClass( TestRequestAware.class.getName() );
123             testClassLoaderAware = surefireClassLoader.loadClass( SurefireClassLoadersAware.class.getName() );
124             reporterConfigurationAware = surefireClassLoader.loadClass( ReporterConfigurationAware.class.getName() );
125             providerPropertiesAware = surefireClassLoader.loadClass( ProviderPropertiesAware.class.getName() );
126             reporterFactory = surefireClassLoader.loadClass( ReporterFactory.class.getName() );
127             runResult = surefireClassLoader.loadClass( RunResult.class.getName() );
128             booterParameters = surefireClassLoader.loadClass( ProviderParameters.class.getName() );
129             testListResolver = surefireClassLoader.loadClass( TestListResolver.class.getName() );
130             mainCliOptions = surefireClassLoader.loadClass( MainCliOptionsAware.class.getName() );
131             commandLineOptionsClass = (Class<Enum>) surefireClassLoader.loadClass( CommandLineOption.class.getName() );
132             shutdownAwareClass = surefireClassLoader.loadClass( ShutdownAware.class.getName() );
133             shutdownClass = (Class<Enum>) surefireClassLoader.loadClass( Shutdown.class.getName() );
134         }
135         catch ( ClassNotFoundException e )
136         {
137             throw new SurefireReflectionException( e );
138         }
139     }
140 
141     public Object convertIfRunResult( Object result )
142     {
143         if ( result == null || !isRunResult( result ) )
144         {
145             return result;
146         }
147         int getCompletedCount1 = (Integer) invokeGetter( result, "getCompletedCount" );
148         int getErrors = (Integer) invokeGetter( result, "getErrors" );
149         int getSkipped = (Integer) invokeGetter( result, "getSkipped" );
150         int getFailures = (Integer) invokeGetter( result, "getFailures" );
151         return new RunResult( getCompletedCount1, getErrors, getFailures, getSkipped );
152 
153     }
154 
155     class ClassLoaderProxy
156         implements InvocationHandler
157     {
158         private final Object target;
159 
160         /**
161          * @param delegate a target
162          */
163         public ClassLoaderProxy( Object delegate )
164         {
165             this.target = delegate;
166         }
167 
168         @Override
169         public Object invoke( Object proxy, Method method, Object[] args )
170             throws Throwable
171         {
172             Method delegateMethod = target.getClass().getMethod( method.getName(), method.getParameterTypes() );
173             return delegateMethod.invoke( target, args );
174         }
175     }
176 
177     Object createTestRequest( TestRequest suiteDefinition )
178     {
179         if ( suiteDefinition == null )
180         {
181             return null;
182         }
183         else
184         {
185             Object resolver = createTestListResolver( suiteDefinition.getTestListResolver() );
186             Class<?>[] arguments = { List.class, File.class, testListResolver, int.class };
187             Constructor constructor = getConstructor( testRequest, arguments );
188             return newInstance( constructor,
189                                 suiteDefinition.getSuiteXmlFiles(),
190                                 suiteDefinition.getTestSourceDirectory(),
191                                 resolver,
192                                 suiteDefinition.getRerunFailingTestsCount() );
193         }
194     }
195 
196     Object createTestListResolver( TestListResolver resolver )
197     {
198         if ( resolver == null )
199         {
200             return null;
201         }
202         else
203         {
204             Constructor constructor = getConstructor( testListResolver, String.class );
205             return newInstance( constructor, resolver.getPluginParameterTest() );
206         }
207     }
208 
209     Object createDirectoryScannerParameters( DirectoryScannerParameters directoryScannerParameters )
210     {
211         if ( directoryScannerParameters == null )
212         {
213             return null;
214         }
215         //Can't use the constructor with the RunOrder parameter. Using it causes some integration tests to fail.
216         Class<?>[] arguments = { File.class, List.class, List.class, List.class, boolean.class, String.class };
217         Constructor constructor = getConstructor( this.directoryScannerParameters, arguments );
218         return newInstance( constructor,
219                             directoryScannerParameters.getTestClassesDirectory(),
220                             directoryScannerParameters.getIncludes(),
221                             directoryScannerParameters.getExcludes(),
222                             directoryScannerParameters.getSpecificTests(),
223                             directoryScannerParameters.isFailIfNoTests(),
224                             RunOrder.asString( directoryScannerParameters.getRunOrder() ) );
225     }
226 
227 
228     Object createRunOrderParameters( RunOrderParameters runOrderParameters )
229     {
230         if ( runOrderParameters == null )
231         {
232             return null;
233         }
234         //Can't use the constructor with the RunOrder parameter. Using it causes some integration tests to fail.
235         Class<?>[] arguments = { String.class, File.class };
236         Constructor constructor = getConstructor( this.runOrderParameters, arguments );
237         File runStatisticsFile = runOrderParameters.getRunStatisticsFile();
238         return newInstance( constructor, RunOrder.asString( runOrderParameters.getRunOrder() ), runStatisticsFile );
239     }
240 
241     Object createTestArtifactInfo( TestArtifactInfo testArtifactInfo )
242     {
243         if ( testArtifactInfo == null )
244         {
245             return null;
246         }
247         Class<?>[] arguments = { String.class, String.class };
248         Constructor constructor = getConstructor( this.testArtifactInfo, arguments );
249         return newInstance( constructor, testArtifactInfo.getVersion(), testArtifactInfo.getClassifier() );
250     }
251 
252     Object createReporterConfiguration( ReporterConfiguration reporterConfig )
253     {
254         Constructor constructor = getConstructor( reporterConfiguration, File.class, boolean.class );
255         return newInstance( constructor, reporterConfig.getReportsDirectory(), reporterConfig.isTrimStackTrace() );
256     }
257 
258     public Object createBooterConfiguration( ClassLoader surefireClassLoader, Object factoryInstance,
259                                              boolean insideFork )
260     {
261         return instantiateTwoArgs( surefireClassLoader, BaseProviderFactory.class.getName(),
262                                    reporterFactory, factoryInstance, boolean.class, insideFork );
263     }
264 
265     public Object instantiateProvider( String providerClassName, Object booterParameters )
266     {
267         return instantiateOneArg( surefireClassLoader, providerClassName, this.booterParameters, booterParameters );
268     }
269 
270     public void setIfDirScannerAware( Object o, DirectoryScannerParameters dirScannerParams )
271     {
272         if ( directoryScannerParametersAware.isAssignableFrom( o.getClass() ) )
273         {
274             setDirectoryScannerParameters( o, dirScannerParams );
275         }
276     }
277 
278     public void setMainCliOptions( Object o, List<CommandLineOption> options )
279     {
280         if ( mainCliOptions.isAssignableFrom( o.getClass() ) )
281         {
282             List<Enum> newOptions = checkedList( new ArrayList<Enum>( options.size() ), commandLineOptionsClass );
283             Collection<Integer> ordinals = toOrdinals( options );
284             for ( Enum e : commandLineOptionsClass.getEnumConstants() )
285             {
286                 if ( ordinals.contains( e.ordinal() ) )
287                 {
288                     newOptions.add( e );
289                 }
290             }
291             invokeSetter( o, "setMainCliOptions", List.class, newOptions );
292         }
293     }
294 
295     public void setSkipAfterFailureCount( Object o, int skipAfterFailureCount )
296     {
297         invokeSetter( o, "setSkipAfterFailureCount", int.class, skipAfterFailureCount );
298     }
299 
300     public void setShutdown( Object o, Shutdown shutdown )
301     {
302         if ( shutdownAwareClass.isAssignableFrom( o.getClass() ) )
303         {
304             for ( Enum e : shutdownClass.getEnumConstants() )
305             {
306                 if ( shutdown.ordinal() == e.ordinal() )
307                 {
308                     invokeSetter( o, "setShutdown", shutdownClass, e );
309                     break;
310                 }
311             }
312         }
313     }
314 
315     public void setSystemExitTimeout( Object o, Integer systemExitTimeout )
316     {
317         invokeSetter( o, "setSystemExitTimeout", Integer.class, systemExitTimeout );
318     }
319 
320     public void setDirectoryScannerParameters( Object o, DirectoryScannerParameters dirScannerParams )
321     {
322         Object param = createDirectoryScannerParameters( dirScannerParams );
323         invokeSetter( o, "setDirectoryScannerParameters", directoryScannerParameters, param );
324     }
325 
326     public void setRunOrderParameters( Object o, RunOrderParameters runOrderParameters )
327     {
328         Object param = createRunOrderParameters( runOrderParameters );
329         invokeSetter( o, "setRunOrderParameters", this.runOrderParameters, param );
330     }
331 
332     public void setTestSuiteDefinitionAware( Object o, TestRequest testSuiteDefinition2 )
333     {
334         if ( testSuiteDefinitionAware.isAssignableFrom( o.getClass() ) )
335         {
336             setTestSuiteDefinition( o, testSuiteDefinition2 );
337         }
338     }
339 
340     void setTestSuiteDefinition( Object o, TestRequest testSuiteDefinition1 )
341     {
342         Object param = createTestRequest( testSuiteDefinition1 );
343         invokeSetter( o, "setTestRequest", testRequest, param );
344     }
345 
346     public void setProviderPropertiesAware( Object o, Map<String, String> properties )
347     {
348         if ( providerPropertiesAware.isAssignableFrom( o.getClass() ) )
349         {
350             setProviderProperties( o, properties );
351         }
352     }
353 
354     void setProviderProperties( Object o, Map<String, String> providerProperties )
355     {
356         invokeSetter( o, "setProviderProperties", Map.class, providerProperties );
357     }
358 
359     public void setReporterConfigurationAware( Object o, ReporterConfiguration reporterConfiguration1 )
360     {
361         if ( reporterConfigurationAware.isAssignableFrom( o.getClass() ) )
362         {
363             setReporterConfiguration( o, reporterConfiguration1 );
364         }
365     }
366 
367 
368     void setReporterConfiguration( Object o, ReporterConfiguration reporterConfiguration )
369     {
370         Object param = createReporterConfiguration( reporterConfiguration );
371         invokeSetter( o, "setReporterConfiguration", this.reporterConfiguration, param );
372     }
373 
374     public void setTestClassLoaderAware( Object o, ClassLoader testClassLoader )
375     {
376         if ( testClassLoaderAware.isAssignableFrom( o.getClass() ) )
377         {
378             setTestClassLoader( o, testClassLoader );
379         }
380     }
381 
382     void setTestClassLoader( Object o, ClassLoader testClassLoader )
383     {
384         Method setter = getMethod( o, "setClassLoaders", ClassLoader.class );
385         invokeMethodWithArray( o, setter, testClassLoader );
386     }
387 
388     public void setTestArtifactInfoAware( Object o, TestArtifactInfo testArtifactInfo1 )
389     {
390         if ( testArtifactInfoAware.isAssignableFrom( o.getClass() ) )
391         {
392             setTestArtifactInfo( o, testArtifactInfo1 );
393         }
394     }
395 
396     void setTestArtifactInfo( Object o, TestArtifactInfo testArtifactInfo )
397     {
398         Object param = createTestArtifactInfo( testArtifactInfo );
399         invokeSetter( o, "setTestArtifactInfo", this.testArtifactInfo, param );
400     }
401 
402     private boolean isRunResult( Object o )
403     {
404         return runResult.isAssignableFrom( o.getClass() );
405     }
406 
407     public Object createConsoleLogger( @Nonnull ConsoleLogger consoleLogger )
408     {
409         return createConsoleLogger( consoleLogger, surefireClassLoader );
410     }
411 
412     private static Collection<Integer> toOrdinals( Collection<? extends Enum> enums )
413     {
414         Collection<Integer> ordinals = new ArrayList<Integer>( enums.size() );
415         for ( Enum e : enums )
416         {
417             ordinals.add( e.ordinal() );
418         }
419         return ordinals;
420     }
421 
422     public static Object createConsoleLogger( ConsoleLogger consoleLogger, ClassLoader cl )
423     {
424         try
425         {
426             Class<?> decoratorClass = cl.loadClass( ConsoleLoggerDecorator.class.getName() );
427             return getConstructor( decoratorClass, Object.class ).newInstance( consoleLogger );
428         }
429         catch ( Exception e )
430         {
431             throw new SurefireReflectionException( e );
432         }
433     }
434 
435 }