1 | |
package org.apache.maven.plugin.surefire.booterclient; |
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.IOException; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.Properties; |
26 | |
import org.apache.maven.plugin.surefire.CommonReflector; |
27 | |
import org.apache.maven.plugin.surefire.booterclient.output.ForkClient; |
28 | |
import org.apache.maven.plugin.surefire.booterclient.output.ThreadedStreamConsumer; |
29 | |
import org.apache.maven.plugin.surefire.report.FileReporterFactory; |
30 | |
import org.apache.maven.surefire.booter.Classpath; |
31 | |
import org.apache.maven.surefire.booter.ClasspathConfiguration; |
32 | |
import org.apache.maven.surefire.booter.ProviderConfiguration; |
33 | |
import org.apache.maven.surefire.booter.ProviderFactory; |
34 | |
import org.apache.maven.surefire.booter.StartupConfiguration; |
35 | |
import org.apache.maven.surefire.booter.StartupReportConfiguration; |
36 | |
import org.apache.maven.surefire.booter.SurefireBooterForkException; |
37 | |
import org.apache.maven.surefire.booter.SurefireExecutionException; |
38 | |
import org.apache.maven.surefire.booter.SystemPropertyManager; |
39 | |
import org.apache.maven.surefire.providerapi.SurefireProvider; |
40 | |
import org.apache.maven.surefire.report.RunStatistics; |
41 | |
import org.apache.maven.surefire.suite.RunResult; |
42 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
43 | |
import org.codehaus.plexus.util.cli.CommandLineTimeOutException; |
44 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
45 | |
import org.codehaus.plexus.util.cli.Commandline; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public class ForkStarter |
64 | |
{ |
65 | |
private final int forkedProcessTimeoutInSeconds; |
66 | |
|
67 | |
private final ProviderConfiguration providerConfiguration; |
68 | |
|
69 | |
private final StartupConfiguration startupConfiguration; |
70 | |
|
71 | |
private final ForkConfiguration forkConfiguration; |
72 | |
|
73 | |
private final StartupReportConfiguration startupReportConfiguration; |
74 | |
|
75 | |
public ForkStarter( ProviderConfiguration providerConfiguration, StartupConfiguration startupConfiguration, |
76 | |
ForkConfiguration forkConfiguration, int forkedProcessTimeoutInSeconds, |
77 | |
StartupReportConfiguration startupReportConfiguration ) |
78 | 0 | { |
79 | 0 | this.forkConfiguration = forkConfiguration; |
80 | 0 | this.providerConfiguration = providerConfiguration; |
81 | 0 | this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds; |
82 | 0 | this.startupConfiguration = startupConfiguration; |
83 | 0 | this.startupReportConfiguration = startupReportConfiguration; |
84 | 0 | } |
85 | |
|
86 | |
public RunResult run() |
87 | |
throws SurefireBooterForkException, SurefireExecutionException |
88 | |
{ |
89 | |
final RunResult result; |
90 | |
|
91 | 0 | final String requestedForkMode = forkConfiguration.getForkMode(); |
92 | 0 | final FileReporterFactory fileReporterFactory = new FileReporterFactory( startupReportConfiguration ); |
93 | |
try |
94 | |
{ |
95 | 0 | final ForkClient forkClient = |
96 | |
new ForkClient( fileReporterFactory, startupReportConfiguration.getTestVmSystemProperties() ); |
97 | 0 | final RunStatistics globalRunStatistics = fileReporterFactory.getGlobalRunStatistics(); |
98 | 0 | if ( ForkConfiguration.FORK_ONCE.equals( requestedForkMode ) ) |
99 | |
{ |
100 | 0 | result = fork( null, providerConfiguration.getProviderProperties(), forkClient, globalRunStatistics ); |
101 | |
} |
102 | 0 | else if ( ForkConfiguration.FORK_ALWAYS.equals( requestedForkMode ) ) |
103 | |
{ |
104 | 0 | result = runSuitesForkPerTestSet( providerConfiguration.getProviderProperties(), forkClient, |
105 | |
globalRunStatistics ); |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | 0 | throw new SurefireExecutionException( "Unknown forkmode: " + requestedForkMode, null ); |
110 | |
} |
111 | |
} |
112 | |
finally |
113 | |
{ |
114 | 0 | fileReporterFactory.close(); |
115 | 0 | } |
116 | 0 | return result; |
117 | |
} |
118 | |
|
119 | |
private RunResult runSuitesForkPerTestSet( Properties properties, ForkClient forkClient, |
120 | |
RunStatistics globalRunStatistics ) |
121 | |
throws SurefireBooterForkException |
122 | |
{ |
123 | 0 | RunResult globalResult = new RunResult( 0, 0, 0, 0 ); |
124 | |
|
125 | 0 | final Iterator suites = getSuitesIterator(); |
126 | |
|
127 | 0 | while ( suites.hasNext() ) |
128 | |
{ |
129 | 0 | Object testSet = suites.next(); |
130 | 0 | RunResult runResult = fork( testSet, properties, forkClient, globalRunStatistics ); |
131 | 0 | globalResult = globalResult.aggregate( runResult ); |
132 | 0 | } |
133 | |
|
134 | 0 | return globalResult; |
135 | |
} |
136 | |
|
137 | |
private RunResult fork( Object testSet, Properties properties, ForkClient forkClient, |
138 | |
RunStatistics globalRunStatistics ) |
139 | |
throws SurefireBooterForkException |
140 | |
{ |
141 | |
File surefireProperties; |
142 | 0 | File systemProperties = null; |
143 | |
try |
144 | |
{ |
145 | 0 | BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration, properties ); |
146 | |
|
147 | 0 | surefireProperties = booterSerializer.serialize( providerConfiguration, startupConfiguration, testSet, |
148 | |
forkConfiguration.getForkMode() ); |
149 | |
|
150 | 0 | if ( forkConfiguration.getSystemProperties() != null ) |
151 | |
{ |
152 | 0 | systemProperties = SystemPropertyManager.writePropertiesFile( forkConfiguration.getSystemProperties(), |
153 | |
forkConfiguration.getTempDirectory(), |
154 | |
"surefire", forkConfiguration.isDebug() ); |
155 | |
} |
156 | |
} |
157 | 0 | catch ( IOException e ) |
158 | |
{ |
159 | 0 | throw new SurefireBooterForkException( "Error creating properties files for forking", e ); |
160 | 0 | } |
161 | |
|
162 | 0 | final Classpath bootClasspathConfiguration = forkConfiguration.getBootClasspath(); |
163 | |
|
164 | 0 | final Classpath additionlClassPathUrls = startupConfiguration.useSystemClassLoader() |
165 | |
? startupConfiguration.getClasspathConfiguration().getTestClasspath() |
166 | |
: null; |
167 | |
|
168 | |
|
169 | |
|
170 | 0 | Classpath bootClasspath = Classpath.join( bootClasspathConfiguration, additionlClassPathUrls ); |
171 | |
|
172 | 0 | Commandline cli = forkConfiguration.createCommandLine( bootClasspath.getClassPath(), |
173 | |
startupConfiguration.getClassLoaderConfiguration(), |
174 | |
startupConfiguration.isShadefire() ); |
175 | |
|
176 | 0 | cli.createArg().setFile( surefireProperties ); |
177 | |
|
178 | 0 | if ( systemProperties != null ) |
179 | |
{ |
180 | 0 | cli.createArg().setFile( systemProperties ); |
181 | |
} |
182 | |
|
183 | 0 | ThreadedStreamConsumer threadedStreamConsumer = new ThreadedStreamConsumer( forkClient ); |
184 | |
|
185 | 0 | if ( forkConfiguration.isDebug() ) |
186 | |
{ |
187 | 0 | System.out.println( "Forking command line: " + cli ); |
188 | |
} |
189 | |
|
190 | |
RunResult runResult; |
191 | |
|
192 | |
try |
193 | |
{ |
194 | 0 | final int timeout = forkedProcessTimeoutInSeconds > 0 ? forkedProcessTimeoutInSeconds : 0; |
195 | 0 | final int result = |
196 | |
CommandLineUtils.executeCommandLine( cli, threadedStreamConsumer, threadedStreamConsumer, timeout ); |
197 | |
|
198 | 0 | if ( result != RunResult.SUCCESS ) |
199 | |
{ |
200 | 0 | throw new SurefireBooterForkException( "Error occured in starting fork, check output in log" ); |
201 | |
} |
202 | 0 | threadedStreamConsumer.close(); |
203 | 0 | forkClient.close(); |
204 | |
|
205 | 0 | runResult = globalRunStatistics.getRunResult(); |
206 | |
} |
207 | 0 | catch ( CommandLineTimeOutException e ) |
208 | |
{ |
209 | 0 | runResult = RunResult.Timeout; |
210 | |
} |
211 | 0 | catch ( CommandLineException e ) |
212 | |
{ |
213 | 0 | throw new SurefireBooterForkException( "Error while executing forked tests.", e.getCause() ); |
214 | 0 | } |
215 | |
|
216 | 0 | return runResult; |
217 | |
} |
218 | |
|
219 | |
private Iterator getSuitesIterator() |
220 | |
throws SurefireBooterForkException |
221 | |
{ |
222 | |
try |
223 | |
{ |
224 | 0 | final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration(); |
225 | 0 | ClassLoader testsClassLoader = classpathConfiguration.createTestClassLoader( false ); |
226 | 0 | ClassLoader surefireClassLoader = |
227 | |
classpathConfiguration.createInprocSurefireClassLoader( testsClassLoader ); |
228 | |
|
229 | 0 | CommonReflector commonReflector = new CommonReflector( surefireClassLoader ); |
230 | 0 | Object reporterFactory = commonReflector.createReportingReporterFactory( startupReportConfiguration ); |
231 | |
|
232 | 0 | final ProviderFactory providerFactory = |
233 | |
new ProviderFactory( startupConfiguration, providerConfiguration, surefireClassLoader, testsClassLoader, |
234 | |
reporterFactory ); |
235 | 0 | SurefireProvider surefireProvider = providerFactory.createProvider( false ); |
236 | 0 | return surefireProvider.getSuites(); |
237 | |
} |
238 | 0 | catch ( SurefireExecutionException e ) |
239 | |
{ |
240 | 0 | throw new SurefireBooterForkException( "Unable to create classloader to find test suites", e ); |
241 | |
} |
242 | |
} |
243 | |
|
244 | |
|
245 | |
} |