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 org.apache.maven.plugin.surefire.JdkAttributes;
23 import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.OutputStreamFlushableCommandline;
24 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
25 import org.apache.maven.surefire.booter.AbstractPathConfiguration;
26 import org.apache.maven.surefire.booter.Classpath;
27 import org.apache.maven.surefire.booter.StartupConfiguration;
28 import org.apache.maven.surefire.booter.SurefireBooterForkException;
29 import org.apache.maven.surefire.util.internal.ImmutableMap;
30
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
33 import java.io.File;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.Properties;
39
40 import static org.apache.maven.plugin.surefire.SurefireHelper.replaceForkThreadsInPath;
41 import static org.apache.maven.plugin.surefire.util.Relocator.relocate;
42 import static org.apache.maven.plugin.surefire.SurefireHelper.replaceThreadNumberPlaceholders;
43 import static org.apache.maven.surefire.booter.Classpath.join;
44
45
46
47
48
49
50
51 public abstract class DefaultForkConfiguration
52 extends ForkConfiguration
53 {
54 @Nonnull private final Classpath booterClasspath;
55 @Nonnull private final File tempDirectory;
56 @Nullable
57 private final String debugLine;
58 @Nonnull private final File workingDirectory;
59 @Nonnull private final Properties modelProperties;
60 @Nullable private final String argLine;
61 @Nonnull private final Map<String, String> environmentVariables;
62 private final boolean debug;
63 private final int forkCount;
64 private final boolean reuseForks;
65 @Nonnull private final Platform pluginPlatform;
66 @Nonnull private final ConsoleLogger log;
67
68 @SuppressWarnings( "checkstyle:parameternumber" )
69 protected DefaultForkConfiguration( @Nonnull Classpath booterClasspath,
70 @Nonnull File tempDirectory,
71 @Nullable String debugLine,
72 @Nonnull File workingDirectory,
73 @Nonnull Properties modelProperties,
74 @Nullable String argLine,
75 @Nonnull Map<String, String> environmentVariables,
76 boolean debug,
77 int forkCount,
78 boolean reuseForks,
79 @Nonnull Platform pluginPlatform,
80 @Nonnull ConsoleLogger log )
81 {
82 this.booterClasspath = booterClasspath;
83 this.tempDirectory = tempDirectory;
84 this.debugLine = debugLine;
85 this.workingDirectory = workingDirectory;
86 this.modelProperties = modelProperties;
87 this.argLine = argLine;
88 this.environmentVariables = toImmutable( environmentVariables );
89 this.debug = debug;
90 this.forkCount = forkCount;
91 this.reuseForks = reuseForks;
92 this.pluginPlatform = pluginPlatform;
93 this.log = log;
94 }
95
96 protected abstract void resolveClasspath( @Nonnull OutputStreamFlushableCommandline cli,
97 @Nonnull String booterThatHasMainMethod,
98 @Nonnull StartupConfiguration config,
99 @Nonnull File dumpLogDirectory )
100 throws SurefireBooterForkException;
101
102 @Nonnull
103 protected String extendJvmArgLine( @Nonnull String jvmArgLine )
104 {
105 return jvmArgLine;
106 }
107
108
109
110
111
112
113
114
115 @Nonnull
116 @Override
117 public OutputStreamFlushableCommandline createCommandLine( @Nonnull StartupConfiguration config,
118 int forkNumber,
119 @Nonnull File dumpLogDirectory )
120 throws SurefireBooterForkException
121 {
122 OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
123
124 cli.setWorkingDirectory( getWorkingDirectory( forkNumber ).getAbsolutePath() );
125
126 for ( Entry<String, String> entry : getEnvironmentVariables().entrySet() )
127 {
128 String value = entry.getValue();
129 cli.addEnvironment( entry.getKey(), value == null ? "" : value );
130 }
131
132 cli.setExecutable( getJdkForTests().getJvmExecutable() );
133
134 String jvmArgLine = newJvmArgLine( forkNumber );
135 if ( !jvmArgLine.isEmpty() )
136 {
137 cli.createArg()
138 .setLine( jvmArgLine );
139 }
140
141 if ( getDebugLine() != null && !getDebugLine().isEmpty() )
142 {
143 cli.createArg()
144 .setLine( getDebugLine() );
145 }
146
147 resolveClasspath( cli, findStartClass( config ), config, dumpLogDirectory );
148
149 return cli;
150 }
151
152 @Nonnull
153 protected List<String> toCompleteClasspath( StartupConfiguration conf ) throws SurefireBooterForkException
154 {
155 AbstractPathConfiguration pathConfig = conf.getClasspathConfiguration();
156 if ( pathConfig.isClassPathConfig() == pathConfig.isModularPathConfig() )
157 {
158 throw new SurefireBooterForkException( "Could not find class-path config nor modular class-path either." );
159 }
160
161 Classpath bootClasspath = getBooterClasspath();
162 Classpath testClasspath = pathConfig.getTestClasspath();
163 Classpath providerClasspath = pathConfig.getProviderClasspath();
164 Classpath completeClasspath = join( join( bootClasspath, testClasspath ), providerClasspath );
165
166 log.debug( completeClasspath.getLogMessage( "boot classpath:" ) );
167 log.debug( completeClasspath.getCompactLogMessage( "boot(compact) classpath:" ) );
168
169 return completeClasspath.getClassPath();
170 }
171
172 @Nonnull
173 private File getWorkingDirectory( int forkNumber )
174 throws SurefireBooterForkException
175 {
176 File cwd = replaceForkThreadsInPath( getWorkingDirectory(), forkNumber );
177
178 if ( !cwd.exists() && !cwd.mkdirs() )
179 {
180 throw new SurefireBooterForkException( "Cannot create workingDirectory " + cwd.getAbsolutePath() );
181 }
182
183 if ( !cwd.isDirectory() )
184 {
185 throw new SurefireBooterForkException(
186 "WorkingDirectory " + cwd.getAbsolutePath() + " exists and is not a directory" );
187 }
188 return cwd;
189 }
190
191
192
193
194
195
196
197
198 @Nonnull
199 private String interpolateArgLineWithPropertyExpressions()
200 {
201 if ( getArgLine() == null )
202 {
203 return "";
204 }
205
206 String resolvedArgLine = getArgLine().trim();
207
208 if ( resolvedArgLine.isEmpty() )
209 {
210 return "";
211 }
212
213 for ( final String key : getModelProperties().stringPropertyNames() )
214 {
215 String field = "@{" + key + "}";
216 if ( getArgLine().contains( field ) )
217 {
218 resolvedArgLine = resolvedArgLine.replace( field, getModelProperties().getProperty( key, "" ) );
219 }
220 }
221
222 return resolvedArgLine;
223 }
224
225 @Nonnull
226 private static String stripNewLines( @Nonnull String argLine )
227 {
228 return argLine.replace( "\n", " " ).replace( "\r", " " );
229 }
230
231
232
233
234
235
236
237
238
239 @Nonnull
240 private static <K, V> Map<K, V> toImmutable( @Nullable Map<K, V> map )
241 {
242 return map == null ? Collections.<K, V>emptyMap() : new ImmutableMap<K, V>( map );
243 }
244
245 @Override
246 @Nonnull
247 public File getTempDirectory()
248 {
249 return tempDirectory;
250 }
251
252 @Override
253 @Nullable
254 protected String getDebugLine()
255 {
256 return debugLine;
257 }
258
259 @Override
260 @Nonnull
261 protected File getWorkingDirectory()
262 {
263 return workingDirectory;
264 }
265
266 @Override
267 @Nonnull
268 protected Properties getModelProperties()
269 {
270 return modelProperties;
271 }
272
273 @Override
274 @Nullable
275 protected String getArgLine()
276 {
277 return argLine;
278 }
279
280 @Override
281 @Nonnull
282 protected Map<String, String> getEnvironmentVariables()
283 {
284 return environmentVariables;
285 }
286
287 @Override
288 protected boolean isDebug()
289 {
290 return debug;
291 }
292
293 @Override
294 protected int getForkCount()
295 {
296 return forkCount;
297 }
298
299 @Override
300 protected boolean isReuseForks()
301 {
302 return reuseForks;
303 }
304
305 @Override
306 @Nonnull
307 protected Platform getPluginPlatform()
308 {
309 return pluginPlatform;
310 }
311
312 @Override
313 @Nonnull
314 protected JdkAttributes getJdkForTests()
315 {
316 return getPluginPlatform().getJdkExecAttributesForTests();
317 }
318
319 @Override
320 @Nonnull
321 protected Classpath getBooterClasspath()
322 {
323 return booterClasspath;
324 }
325
326 @Nonnull
327 private String newJvmArgLine( int forks )
328 {
329 String interpolatedArgs = stripNewLines( interpolateArgLineWithPropertyExpressions() );
330 String argsWithReplacedForkNumbers = replaceThreadNumberPlaceholders( interpolatedArgs, forks );
331 return extendJvmArgLine( argsWithReplacedForkNumbers );
332 }
333
334 @Nonnull
335 private static String findStartClass( StartupConfiguration config )
336 {
337 return config.isShadefire() ? relocate( DEFAULT_PROVIDER_CLASS ) : DEFAULT_PROVIDER_CLASS;
338 }
339 }