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.PrintStream; |
23 | |
import java.lang.reflect.InvocationTargetException; |
24 | |
import java.lang.reflect.Method; |
25 | |
import java.util.Iterator; |
26 | |
import org.apache.maven.surefire.providerapi.SurefireProvider; |
27 | |
import org.apache.maven.surefire.report.ReporterException; |
28 | |
import org.apache.maven.surefire.suite.RunResult; |
29 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
30 | |
import org.apache.maven.surefire.util.ReflectionUtils; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class ProviderFactory |
40 | |
{ |
41 | |
private final StartupConfiguration startupConfiguration; |
42 | |
|
43 | |
private final ProviderConfiguration providerConfiguration; |
44 | |
|
45 | |
private final ClassLoader surefireClassLoader; |
46 | |
|
47 | |
private final ClassLoader testsClassLoader; |
48 | |
|
49 | |
private final SurefireReflector surefireReflector; |
50 | |
|
51 | |
private final Object reporterManagerFactory; |
52 | |
|
53 | 0 | private static final Class[] invokeParamaters = new Class[]{ Object.class }; |
54 | |
|
55 | |
|
56 | |
public ProviderFactory( StartupConfiguration startupConfiguration, ProviderConfiguration providerConfiguration, |
57 | |
ClassLoader surefireClassLoader, ClassLoader testsClassLoader, |
58 | |
Object reporterManagerFactory ) |
59 | 0 | { |
60 | 0 | this.providerConfiguration = providerConfiguration; |
61 | 0 | this.surefireClassLoader = surefireClassLoader; |
62 | 0 | this.startupConfiguration = startupConfiguration; |
63 | 0 | this.surefireReflector = new SurefireReflector( surefireClassLoader ); |
64 | 0 | this.testsClassLoader = testsClassLoader; |
65 | 0 | this.reporterManagerFactory = reporterManagerFactory; |
66 | 0 | } |
67 | |
|
68 | |
public static RunResult invokeProvider( Object testSet, ClassLoader testsClassLoader, |
69 | |
ClassLoader surefireClassLoader, Object factory, |
70 | |
ProviderConfiguration providerConfiguration, boolean insideFork, |
71 | |
StartupConfiguration startupConfiguration1, boolean restoreStreams ) |
72 | |
throws TestSetFailedException, InvocationTargetException |
73 | |
{ |
74 | 0 | final PrintStream orgSystemOut = System.out; |
75 | 0 | final PrintStream orgSystemErr = System.err; |
76 | |
|
77 | |
|
78 | |
|
79 | 0 | ProviderFactory providerFactory = |
80 | |
new ProviderFactory( startupConfiguration1, providerConfiguration, surefireClassLoader, testsClassLoader, |
81 | |
factory ); |
82 | 0 | final SurefireProvider provider = providerFactory.createProvider( insideFork ); |
83 | |
|
84 | |
try |
85 | |
{ |
86 | 0 | return provider.invoke( testSet ); |
87 | |
} |
88 | |
finally |
89 | |
{ |
90 | 0 | if ( restoreStreams && System.getSecurityManager() == null ) |
91 | |
{ |
92 | 0 | System.setOut( orgSystemOut ); |
93 | 0 | System.setErr( orgSystemErr ); |
94 | |
} |
95 | |
} |
96 | |
} |
97 | |
|
98 | |
public SurefireProvider createProvider( boolean isInsideFork ) |
99 | |
{ |
100 | 0 | ClassLoader systemClassLoader = java.lang.Thread.currentThread().getContextClassLoader(); |
101 | 0 | Thread.currentThread().setContextClassLoader( surefireClassLoader ); |
102 | |
|
103 | 0 | StartupConfiguration starterConfiguration = startupConfiguration; |
104 | |
|
105 | 0 | final Object o = |
106 | |
surefireReflector.createBooterConfiguration( surefireClassLoader, reporterManagerFactory, isInsideFork ); |
107 | 0 | surefireReflector.setTestSuiteDefinitionAware( o, providerConfiguration.getTestSuiteDefinition() ); |
108 | 0 | surefireReflector.setProviderPropertiesAware( o, providerConfiguration.getProviderProperties() ); |
109 | 0 | surefireReflector.setReporterConfigurationAware( o, providerConfiguration.getReporterConfiguration() ); |
110 | 0 | surefireReflector.setTestClassLoaderAware( o, surefireClassLoader, testsClassLoader ); |
111 | 0 | surefireReflector.setTestArtifactInfoAware( o, providerConfiguration.getTestArtifact() ); |
112 | 0 | surefireReflector.setRunOrderParameters( o, providerConfiguration.getRunOrderParameters() ); |
113 | 0 | surefireReflector.setIfDirScannerAware( o, providerConfiguration.getDirScannerParams() ); |
114 | |
|
115 | 0 | Object provider = surefireReflector.instantiateProvider( starterConfiguration.getProviderClassName(), o ); |
116 | 0 | Thread.currentThread().setContextClassLoader( systemClassLoader ); |
117 | |
|
118 | 0 | return new ProviderProxy( provider, testsClassLoader ); |
119 | |
} |
120 | |
|
121 | |
|
122 | 0 | private class ProviderProxy |
123 | |
implements SurefireProvider |
124 | |
{ |
125 | |
private final Object providerInOtherClassLoader; |
126 | |
|
127 | |
private final ClassLoader testsClassLoader; |
128 | |
|
129 | |
|
130 | |
private ProviderProxy( Object providerInOtherClassLoader, ClassLoader testsClassLoader ) |
131 | 0 | { |
132 | 0 | this.providerInOtherClassLoader = providerInOtherClassLoader; |
133 | 0 | this.testsClassLoader = testsClassLoader; |
134 | 0 | } |
135 | |
|
136 | |
public Iterator getSuites() |
137 | |
{ |
138 | 0 | ClassLoader current = swapClassLoader( testsClassLoader ); |
139 | |
try |
140 | |
{ |
141 | 0 | return (Iterator) ReflectionUtils.invokeGetter( providerInOtherClassLoader, "getSuites" ); |
142 | |
} |
143 | |
finally |
144 | |
{ |
145 | 0 | Thread.currentThread().setContextClassLoader( current ); |
146 | |
} |
147 | |
} |
148 | |
|
149 | |
public RunResult invoke( Object forkTestSet ) |
150 | |
throws TestSetFailedException, ReporterException, InvocationTargetException |
151 | |
{ |
152 | 0 | ClassLoader current = swapClassLoader( testsClassLoader ); |
153 | |
try |
154 | |
{ |
155 | 0 | final Method invoke = |
156 | |
ReflectionUtils.getMethod( providerInOtherClassLoader.getClass(), "invoke", invokeParamaters ); |
157 | |
|
158 | 0 | final Object result = ReflectionUtils.invokeMethodWithArray2( providerInOtherClassLoader, invoke, |
159 | |
new Object[]{ forkTestSet } ); |
160 | 0 | return (RunResult) surefireReflector.convertIfRunResult( result ); |
161 | |
} |
162 | |
finally |
163 | |
{ |
164 | 0 | if ( System.getSecurityManager() == null ) |
165 | |
{ |
166 | 0 | Thread.currentThread().setContextClassLoader( current ); |
167 | |
} |
168 | |
} |
169 | |
|
170 | |
} |
171 | |
|
172 | |
private ClassLoader swapClassLoader( ClassLoader newClassLoader ) |
173 | |
{ |
174 | 0 | ClassLoader current = Thread.currentThread().getContextClassLoader(); |
175 | 0 | Thread.currentThread().setContextClassLoader( newClassLoader ); |
176 | 0 | return current; |
177 | |
} |
178 | |
|
179 | |
public void cancel() |
180 | |
{ |
181 | 0 | final Method invoke = |
182 | |
ReflectionUtils.getMethod( providerInOtherClassLoader.getClass(), "cancel", new Class[]{ } ); |
183 | 0 | ReflectionUtils.invokeMethodWithArray( providerInOtherClassLoader, invoke, null ); |
184 | 0 | } |
185 | |
} |
186 | |
} |