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, getErrors, getFailures, getSkipped );
119
120 }
121
122
123
124
125
126 class ClassLoaderProxy
127 implements InvocationHandler
128 {
129 private final Object target;
130
131
132
133
134
135 public ClassLoaderProxy( Object delegate )
136 {
137 this.target = delegate;
138 }
139
140 public Object invoke( Object proxy, Method method, Object[] args )
141 throws Throwable
142 {
143 Method delegateMethod = target.getClass().getMethod( method.getName(), method.getParameterTypes() );
144 return delegateMethod.invoke( target, args );
145 }
146 }
147
148
149 Object createTestRequest( TestRequest suiteDefinition )
150 {
151 if ( suiteDefinition == null )
152 {
153 return null;
154 }
155 Class[] arguments = { List.class, File.class, String.class, String.class };
156 Constructor constructor = ReflectionUtils.getConstructor( this.testRequest, arguments );
157 return ReflectionUtils.newInstance( constructor, new Object[]{ suiteDefinition.getSuiteXmlFiles(),
158 suiteDefinition.getTestSourceDirectory(), suiteDefinition.getRequestedTest(),
159 suiteDefinition.getRequestedTestMethod() } );
160 }
161
162
163 Object createDirectoryScannerParameters( DirectoryScannerParameters directoryScannerParameters )
164 {
165 if ( directoryScannerParameters == null )
166 {
167 return null;
168 }
169
170 Class[] arguments = { File.class, List.class, List.class, List.class, Boolean.class, String.class };
171 Constructor constructor = ReflectionUtils.getConstructor( this.directoryScannerParameters, arguments );
172 return ReflectionUtils.newInstance( constructor,
173 new Object[]{ directoryScannerParameters.getTestClassesDirectory(),
174 directoryScannerParameters.getIncludes(),
175 directoryScannerParameters.getExcludes(),
176 directoryScannerParameters.getSpecificTests(),
177 directoryScannerParameters.isFailIfNoTests(),
178 RunOrder.asString( directoryScannerParameters.getRunOrder() ) } );
179 }
180
181
182 Object createRunOrderParameters( RunOrderParameters runOrderParameters )
183 {
184 if ( runOrderParameters == null )
185 {
186 return null;
187 }
188
189 Class[] arguments = { String.class, String.class };
190 Constructor constructor = ReflectionUtils.getConstructor( this.runOrderParameters, arguments );
191 final File runStatisticsFile = runOrderParameters.getRunStatisticsFile();
192 return ReflectionUtils.newInstance( constructor,
193 new Object[]{ RunOrder.asString( runOrderParameters.getRunOrder() ),
194 runStatisticsFile != null
195 ? runStatisticsFile.getAbsolutePath()
196 : null } );
197 }
198
199 Object createTestArtifactInfo( TestArtifactInfo testArtifactInfo )
200 {
201 if ( testArtifactInfo == null )
202 {
203 return null;
204 }
205 Class[] arguments = { String.class, String.class };
206 Constructor constructor = ReflectionUtils.getConstructor( this.testArtifactInfo, arguments );
207 return ReflectionUtils.newInstance( constructor, new Object[]{ testArtifactInfo.getVersion(),
208 testArtifactInfo.getClassifier() } );
209 }
210
211
212 Object createReporterConfiguration( ReporterConfiguration reporterConfiguration )
213 {
214 Constructor constructor =
215 ReflectionUtils.getConstructor( this.reporterConfiguration, new Class[]{ File.class, Boolean.class } );
216 return ReflectionUtils.newInstance( constructor, new Object[]{ reporterConfiguration.getReportsDirectory(),
217 reporterConfiguration.isTrimStackTrace() } );
218 }
219
220 public static ReporterFactory createForkingReporterFactoryInCurrentClassLoader( Boolean trimStackTrace, PrintStream originalSystemOut )
221 {
222 return new ForkingReporterFactory( trimStackTrace, originalSystemOut );
223 }
224
225 public Object createBooterConfiguration( ClassLoader surefireClassLoader, Object factoryInstance,
226 boolean insideFork )
227 {
228 return ReflectionUtils.instantiateTwoArgs( surefireClassLoader, BaseProviderFactory.class.getName(),
229 reporterFactory, factoryInstance, Boolean.class,
230 insideFork ? Boolean.TRUE : Boolean.FALSE );
231 }
232
233 public Object instantiateProvider( String providerClassName, Object booterParameters )
234 {
235 return ReflectionUtils.instantiateOneArg( surefireClassLoader, providerClassName, this.booterParameters,
236 booterParameters );
237 }
238
239 public void setIfDirScannerAware( Object o, DirectoryScannerParameters dirScannerParams )
240 {
241 if ( directoryScannerParametersAware.isAssignableFrom( o.getClass() ) )
242 {
243 setDirectoryScannerParameters( o, dirScannerParams );
244 }
245 }
246
247 public void setDirectoryScannerParameters( Object o, DirectoryScannerParameters dirScannerParams )
248 {
249 final Object param = createDirectoryScannerParameters( dirScannerParams );
250 ReflectionUtils.invokeSetter( o, "setDirectoryScannerParameters", this.directoryScannerParameters, param );
251 }
252
253 public void setRunOrderParameters( Object o, RunOrderParameters runOrderParameters )
254 {
255 final Object param = createRunOrderParameters( runOrderParameters );
256 ReflectionUtils.invokeSetter( o, "setRunOrderParameters", this.runOrderParameters, param );
257 }
258
259
260 public void setTestSuiteDefinitionAware( Object o, TestRequest testSuiteDefinition2 )
261 {
262 if ( testSuiteDefinitionAware.isAssignableFrom( o.getClass() ) )
263 {
264 setTestSuiteDefinition( o, testSuiteDefinition2 );
265 }
266 }
267
268 void setTestSuiteDefinition( Object o, TestRequest testSuiteDefinition1 )
269 {
270 final Object param = createTestRequest( testSuiteDefinition1 );
271 ReflectionUtils.invokeSetter( o, "setTestRequest", this.testRequest, param );
272 }
273
274 public void setProviderPropertiesAware( Object o, Properties properties )
275 {
276 if ( providerPropertiesAware.isAssignableFrom( o.getClass() ) )
277 {
278 setProviderProperties( o, properties );
279 }
280 }
281
282 void setProviderProperties( Object o, Properties providerProperties )
283 {
284 ReflectionUtils.invokeSetter( o, "setProviderProperties", Properties.class, providerProperties );
285 }
286
287 public void setReporterConfigurationAware( Object o, ReporterConfiguration reporterConfiguration1 )
288 {
289 if ( reporterConfigurationAware.isAssignableFrom( o.getClass() ) )
290 {
291 setReporterConfiguration( o, reporterConfiguration1 );
292 }
293 }
294
295
296 void setReporterConfiguration( Object o, ReporterConfiguration reporterConfiguration )
297 {
298 final Object param = createReporterConfiguration( reporterConfiguration );
299 ReflectionUtils.invokeSetter( o, "setReporterConfiguration", this.reporterConfiguration, param );
300 }
301
302 public void setTestClassLoaderAware( Object o, ClassLoader testClassLoader )
303 {
304 if ( testClassLoaderAware.isAssignableFrom( o.getClass() ) )
305 {
306 setTestClassLoader( o, testClassLoader );
307 }
308 }
309
310 void setTestClassLoader( Object o, ClassLoader testClassLoader )
311 {
312 final Method setter =
313 ReflectionUtils.getMethod( o, "setClassLoaders", new Class[]{ ClassLoader.class } );
314 ReflectionUtils.invokeMethodWithArray( o, setter, new Object[]{ testClassLoader } );
315 }
316
317 public void setTestArtifactInfoAware( Object o, TestArtifactInfo testArtifactInfo1 )
318 {
319 if ( testArtifactInfoAware.isAssignableFrom( o.getClass() ) )
320 {
321 setTestArtifactInfo( o, testArtifactInfo1 );
322 }
323 }
324
325 void setTestArtifactInfo( Object o, TestArtifactInfo testArtifactInfo )
326 {
327 final Object param = createTestArtifactInfo( testArtifactInfo );
328 ReflectionUtils.invokeSetter( o, "setTestArtifactInfo", this.testArtifactInfo, param );
329 }
330
331 private boolean isRunResult( Object o )
332 {
333 return runResult.isAssignableFrom( o.getClass() );
334 }
335
336 }