1 package org.apache.maven.surefire.booter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.PrintStream;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationHandler;
26 import java.lang.reflect.Method;
27 import java.util.List;
28 import java.util.Properties;
29 import org.apache.maven.surefire.providerapi.ProviderParameters;
30 import org.apache.maven.surefire.report.ReporterConfiguration;
31 import org.apache.maven.surefire.report.ReporterFactory;
32 import org.apache.maven.surefire.suite.RunResult;
33 import org.apache.maven.surefire.testset.DirectoryScannerParameters;
34 import org.apache.maven.surefire.testset.RunOrderParameters;
35 import org.apache.maven.surefire.testset.TestArtifactInfo;
36 import org.apache.maven.surefire.testset.TestRequest;
37 import org.apache.maven.surefire.util.ReflectionUtils;
38 import org.apache.maven.surefire.util.RunOrder;
39 import org.apache.maven.surefire.util.SurefireReflectionException;
40
41
42
43
44
45
46
47
48 public class SurefireReflector
49 {
50 private final ClassLoader surefireClassLoader;
51
52 private final Class reporterConfiguration;
53
54 private final Class testRequest;
55
56 private final Class testArtifactInfo;
57
58 private final Class testArtifactInfoAware;
59
60 private final Class directoryScannerParameters;
61
62 private final Class runOrderParameters;
63
64 private final Class directoryScannerParametersAware;
65
66 private final Class testSuiteDefinitionAware;
67
68 private final Class testClassLoaderAware;
69
70 private final Class reporterConfigurationAware;
71
72 private final Class providerPropertiesAware;
73
74 private final Class runResult;
75
76 private final Class booterParameters;
77
78 private final Class reporterFactory;
79
80
81 public SurefireReflector( ClassLoader surefireClassLoader )
82 {
83 this.surefireClassLoader = surefireClassLoader;
84 try
85 {
86 reporterConfiguration = surefireClassLoader.loadClass( ReporterConfiguration.class.getName() );
87 testRequest = surefireClassLoader.loadClass( TestRequest.class.getName() );
88 testArtifactInfo = surefireClassLoader.loadClass( TestArtifactInfo.class.getName() );
89 testArtifactInfoAware = surefireClassLoader.loadClass( TestArtifactInfoAware.class.getName() );
90 directoryScannerParameters = surefireClassLoader.loadClass( DirectoryScannerParameters.class.getName() );
91 runOrderParameters = surefireClassLoader.loadClass( RunOrderParameters.class.getName() );
92 directoryScannerParametersAware =
93 surefireClassLoader.loadClass( DirectoryScannerParametersAware.class.getName() );
94 testSuiteDefinitionAware = surefireClassLoader.loadClass( TestRequestAware.class.getName() );
95 testClassLoaderAware = surefireClassLoader.loadClass( SurefireClassLoadersAware.class.getName() );
96 reporterConfigurationAware = surefireClassLoader.loadClass( ReporterConfigurationAware.class.getName() );
97 providerPropertiesAware = surefireClassLoader.loadClass( ProviderPropertiesAware.class.getName() );
98 reporterFactory = surefireClassLoader.loadClass( ReporterFactory.class.getName() );
99 runResult = surefireClassLoader.loadClass( RunResult.class.getName() );
100 booterParameters = surefireClassLoader.loadClass( ProviderParameters.class.getName() );
101 }
102 catch ( ClassNotFoundException e )
103 {
104 throw new SurefireReflectionException( e );
105 }
106 }
107
108 public Object convertIfRunResult( Object result )
109 {
110 if ( result == null || !isRunResult( result ) )
111 {
112 return result;
113 }
114 final Integer getCompletedCount1 = (Integer) ReflectionUtils.invokeGetter( result, "getCompletedCount" );
115 final Integer getErrors = (Integer) ReflectionUtils.invokeGetter( result, "getErrors" );
116 final Integer getSkipped = (Integer) ReflectionUtils.invokeGetter( result, "getSkipped" );
117 final Integer getFailures = (Integer) ReflectionUtils.invokeGetter( result, "getFailures" );
118 return new RunResult( getCompletedCount1.intValue(), getErrors.intValue(), getFailures.intValue(),
119 getSkipped.intValue() );
120
121 }
122
123
124
125
126
127 class ClassLoaderProxy
128 implements InvocationHandler
129 {
130 private final Object target;
131
132
133
134
135
136 public ClassLoaderProxy( Object delegate )
137 {
138 this.target = delegate;
139 }
140
141 public Object invoke( Object proxy, Method method, Object[] args )
142 throws Throwable
143 {
144 Method delegateMethod = target.getClass().getMethod( method.getName(), method.getParameterTypes() );
145 return delegateMethod.invoke( target, args );
146 }
147 }
148
149
150 Object createTestRequest( TestRequest suiteDefinition )
151 {
152 if ( suiteDefinition == null )
153 {
154 return null;
155 }
156 Class[] arguments = { List.class, File.class, String.class, String.class };
157 Constructor constructor = ReflectionUtils.getConstructor( this.testRequest, arguments );
158 return ReflectionUtils.newInstance( constructor, new Object[]{ suiteDefinition.getSuiteXmlFiles(),
159 suiteDefinition.getTestSourceDirectory(), suiteDefinition.getRequestedTest(),
160 suiteDefinition.getRequestedTestMethod() } );
161 }
162
163
164 Object createDirectoryScannerParameters( DirectoryScannerParameters directoryScannerParameters )
165 {
166 if ( directoryScannerParameters == null )
167 {
168 return null;
169 }
170
171 Class[] arguments = { File.class, List.class, List.class, List.class, Boolean.class, String.class };
172 Constructor constructor = ReflectionUtils.getConstructor( this.directoryScannerParameters, arguments );
173 return ReflectionUtils.newInstance( constructor,
174 new Object[]{ directoryScannerParameters.getTestClassesDirectory(),
175 directoryScannerParameters.getIncludes(),
176 directoryScannerParameters.getExcludes(),
177 directoryScannerParameters.getSpecificTests(),
178 directoryScannerParameters.isFailIfNoTests(),
179 RunOrder.asString( directoryScannerParameters.getRunOrder() ) } );
180 }
181
182
183 Object createRunOrderParameters( RunOrderParameters runOrderParameters )
184 {
185 if ( runOrderParameters == null )
186 {
187 return null;
188 }
189
190 Class[] arguments = { String.class, String.class };
191 Constructor constructor = ReflectionUtils.getConstructor( this.runOrderParameters, arguments );
192 final File runStatisticsFile = runOrderParameters.getRunStatisticsFile();
193 return ReflectionUtils.newInstance( constructor,
194 new Object[]{ RunOrder.asString( runOrderParameters.getRunOrder() ),
195 runStatisticsFile != null
196 ? runStatisticsFile.getAbsolutePath()
197 : null } );
198 }
199
200 Object createTestArtifactInfo( TestArtifactInfo testArtifactInfo )
201 {
202 if ( testArtifactInfo == null )
203 {
204 return null;
205 }
206 Class[] arguments = { String.class, String.class };
207 Constructor constructor = ReflectionUtils.getConstructor( this.testArtifactInfo, arguments );
208 return ReflectionUtils.newInstance( constructor, new Object[]{ testArtifactInfo.getVersion(),
209 testArtifactInfo.getClassifier() } );
210 }
211
212
213 Object createReporterConfiguration( ReporterConfiguration reporterConfiguration )
214 {
215 Constructor constructor =
216 ReflectionUtils.getConstructor( this.reporterConfiguration, new Class[]{ File.class, Boolean.class } );
217 return ReflectionUtils.newInstance( constructor, new Object[]{ reporterConfiguration.getReportsDirectory(),
218 reporterConfiguration.isTrimStackTrace() } );
219 }
220
221 public Object createForkingReporterFactory( Boolean trimStackTrace, PrintStream originalSystemOut )
222 {
223 Class[] args = new Class[]{ Boolean.class, PrintStream.class };
224 Object[] values = new Object[]{ trimStackTrace, originalSystemOut };
225 return ReflectionUtils.instantiateObject( ForkingReporterFactory.class.getName(), args, values,
226 surefireClassLoader );
227 }
228
229 public Object createBooterConfiguration( ClassLoader surefireClassLoader, Object factoryInstance,
230 boolean insideFork )
231 {
232 return ReflectionUtils.instantiateTwoArgs( surefireClassLoader, BaseProviderFactory.class.getName(),
233 reporterFactory, factoryInstance, Boolean.class,
234 insideFork ? Boolean.TRUE : Boolean.FALSE );
235 }
236
237 public Object instantiateProvider( String providerClassName, Object booterParameters )
238 {
239 return ReflectionUtils.instantiateOneArg( surefireClassLoader, providerClassName, this.booterParameters,
240 booterParameters );
241 }
242
243 public void setIfDirScannerAware( Object o, DirectoryScannerParameters dirScannerParams )
244 {
245 if ( directoryScannerParametersAware.isAssignableFrom( o.getClass() ) )
246 {
247 setDirectoryScannerParameters( o, dirScannerParams );
248 }
249 }
250
251 public void setDirectoryScannerParameters( Object o, DirectoryScannerParameters dirScannerParams )
252 {
253 final Object param = createDirectoryScannerParameters( dirScannerParams );
254 ReflectionUtils.invokeSetter( o, "setDirectoryScannerParameters", this.directoryScannerParameters, param );
255 }
256
257 public void setRunOrderParameters( Object o, RunOrderParameters runOrderParameters )
258 {
259 final Object param = createRunOrderParameters( runOrderParameters );
260 ReflectionUtils.invokeSetter( o, "setRunOrderParameters", this.runOrderParameters, param );
261 }
262
263
264 public void setTestSuiteDefinitionAware( Object o, TestRequest testSuiteDefinition2 )
265 {
266 if ( testSuiteDefinitionAware.isAssignableFrom( o.getClass() ) )
267 {
268 setTestSuiteDefinition( o, testSuiteDefinition2 );
269 }
270 }
271
272 void setTestSuiteDefinition( Object o, TestRequest testSuiteDefinition1 )
273 {
274 final Object param = createTestRequest( testSuiteDefinition1 );
275 ReflectionUtils.invokeSetter( o, "setTestRequest", this.testRequest, param );
276 }
277
278 public void setProviderPropertiesAware( Object o, Properties properties )
279 {
280 if ( providerPropertiesAware.isAssignableFrom( o.getClass() ) )
281 {
282 setProviderProperties( o, properties );
283 }
284 }
285
286 void setProviderProperties( Object o, Properties providerProperties )
287 {
288 ReflectionUtils.invokeSetter( o, "setProviderProperties", Properties.class, providerProperties );
289 }
290
291 public void setReporterConfigurationAware( Object o, ReporterConfiguration reporterConfiguration1 )
292 {
293 if ( reporterConfigurationAware.isAssignableFrom( o.getClass() ) )
294 {
295 setReporterConfiguration( o, reporterConfiguration1 );
296 }
297 }
298
299
300 void setReporterConfiguration( Object o, ReporterConfiguration reporterConfiguration )
301 {
302 final Object param = createReporterConfiguration( reporterConfiguration );
303 ReflectionUtils.invokeSetter( o, "setReporterConfiguration", this.reporterConfiguration, param );
304 }
305
306 public void setTestClassLoaderAware( Object o, ClassLoader surefireClassLoader, ClassLoader testClassLoader )
307 {
308 if ( testClassLoaderAware.isAssignableFrom( o.getClass() ) )
309 {
310 setTestClassLoader( o, surefireClassLoader, testClassLoader );
311 }
312 }
313
314 void setTestClassLoader( Object o, ClassLoader surefireClassLoader, ClassLoader testClassLoader )
315 {
316 final Method setter =
317 ReflectionUtils.getMethod( o, "setClassLoaders", new Class[]{ ClassLoader.class, ClassLoader.class } );
318 ReflectionUtils.invokeMethodWithArray( o, setter, new Object[]{ surefireClassLoader, testClassLoader } );
319 }
320
321 public void setTestArtifactInfoAware( Object o, TestArtifactInfo testArtifactInfo1 )
322 {
323 if ( testArtifactInfoAware.isAssignableFrom( o.getClass() ) )
324 {
325 setTestArtifactInfo( o, testArtifactInfo1 );
326 }
327 }
328
329 void setTestArtifactInfo( Object o, TestArtifactInfo testArtifactInfo )
330 {
331 final Object param = createTestArtifactInfo( testArtifactInfo );
332 ReflectionUtils.invokeSetter( o, "setTestArtifactInfo", this.testArtifactInfo, param );
333 }
334
335 private boolean isRunResult( Object o )
336 {
337 return runResult.isAssignableFrom( o.getClass() );
338 }
339
340 }