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 org.apache.maven.surefire.providerapi.SurefireProvider; |
23 | |
|
24 | |
import java.lang.reflect.InvocationHandler; |
25 | |
import java.lang.reflect.Method; |
26 | |
import java.lang.reflect.Proxy; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class ProviderFactory |
36 | |
{ |
37 | |
private final StartupConfiguration startupConfiguration; |
38 | |
|
39 | |
private final ProviderConfiguration providerConfiguration; |
40 | |
|
41 | |
private final ClassLoader surefireClassLoader; |
42 | |
|
43 | |
private final ClassLoader testsClassLoader; |
44 | |
|
45 | |
private final SurefireReflector surefireReflector; |
46 | |
|
47 | |
|
48 | |
public ProviderFactory( StartupConfiguration startupConfiguration, ProviderConfiguration providerConfiguration, |
49 | |
ClassLoader surefireClassLoader, ClassLoader testsClassLoader ) |
50 | 0 | { |
51 | 0 | this.providerConfiguration = providerConfiguration; |
52 | 0 | this.surefireClassLoader = surefireClassLoader; |
53 | 0 | this.startupConfiguration = startupConfiguration; |
54 | 0 | this.surefireReflector = new SurefireReflector( surefireClassLoader ); |
55 | 0 | this.testsClassLoader = testsClassLoader; |
56 | 0 | } |
57 | |
|
58 | |
public SurefireProvider createProvider() |
59 | |
{ |
60 | 0 | ClassLoader context = java.lang.Thread.currentThread().getContextClassLoader(); |
61 | 0 | Thread.currentThread().setContextClassLoader( surefireClassLoader ); |
62 | |
|
63 | 0 | StartupConfiguration starterConfiguration = startupConfiguration; |
64 | 0 | final Object o = surefireReflector.createBooterConfiguration(); |
65 | 0 | surefireReflector.setTestSuiteDefinitionAware( o, providerConfiguration.getTestSuiteDefinition() ); |
66 | 0 | surefireReflector.setProviderPropertiesAware( o, providerConfiguration.getProviderProperties() ); |
67 | 0 | surefireReflector.setReporterConfigurationAware( o, providerConfiguration.getReporterConfiguration() ); |
68 | 0 | surefireReflector.setTestClassLoaderAware( o, surefireClassLoader, testsClassLoader ); |
69 | 0 | surefireReflector.setTestArtifactInfoAware( o, providerConfiguration.getTestArtifact() ); |
70 | 0 | surefireReflector.setIfDirScannerAware( o, providerConfiguration.getDirScannerParams() ); |
71 | |
|
72 | 0 | Object provider = surefireReflector.instantiateProvider( starterConfiguration.getProviderClassName(), o ); |
73 | 0 | Thread.currentThread().setContextClassLoader( context ); |
74 | |
|
75 | 0 | return createClassLoaderProxy( provider ); |
76 | |
} |
77 | |
|
78 | |
private SurefireProvider createClassLoaderProxy( Object target ) |
79 | |
{ |
80 | 0 | return (SurefireProvider) Proxy.newProxyInstance( this.getClass().getClassLoader(), |
81 | 0 | new Class[]{ SurefireProvider.class }, |
82 | |
new ClassLoaderProxy( target ) ); |
83 | |
} |
84 | |
|
85 | |
private class ClassLoaderProxy |
86 | |
implements InvocationHandler |
87 | |
{ |
88 | |
private final Object target; |
89 | |
|
90 | |
public ClassLoaderProxy( Object delegate ) |
91 | 0 | { |
92 | 0 | this.target = delegate; |
93 | 0 | } |
94 | |
|
95 | |
public Object invoke( Object proxy, Method method, Object[] args ) |
96 | |
throws Throwable |
97 | |
{ |
98 | 0 | ClassLoader original = java.lang.Thread.currentThread().getContextClassLoader(); |
99 | 0 | Thread.currentThread().setContextClassLoader( testsClassLoader ); |
100 | |
try |
101 | |
{ |
102 | 0 | Method delegateMethod = target.getClass().getMethod( method.getName(), method.getParameterTypes() ); |
103 | 0 | final Object result = delegateMethod.invoke( target, args ); |
104 | 0 | return surefireReflector.convertIfRunResult( result ); |
105 | |
} |
106 | |
finally |
107 | |
{ |
108 | 0 | Thread.currentThread().setContextClassLoader( original ); |
109 | |
} |
110 | |
} |
111 | |
} |
112 | |
|
113 | |
} |