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 throws SurefireBooterForkException;
100
101 @Nonnull
102 protected String extendJvmArgLine( @Nonnull String jvmArgLine )
103 {
104 return jvmArgLine;
105 }
106
107
108
109
110
111
112
113 @Nonnull
114 @Override
115 public OutputStreamFlushableCommandline createCommandLine( @Nonnull StartupConfiguration config, int forkNumber )
116 throws SurefireBooterForkException
117 {
118 OutputStreamFlushableCommandline cli = new OutputStreamFlushableCommandline();
119
120 cli.setWorkingDirectory( getWorkingDirectory( forkNumber ).getAbsolutePath() );
121
122 for ( Entry<String, String> entry : getEnvironmentVariables().entrySet() )
123 {
124 String value = entry.getValue();
125 cli.addEnvironment( entry.getKey(), value == null ? "" : value );
126 }
127
128 cli.setExecutable( getJdkForTests().getJvmExecutable() );
129
130 String jvmArgLine = newJvmArgLine( forkNumber );
131 if ( !jvmArgLine.isEmpty() )
132 {
133 cli.createArg()
134 .setLine( jvmArgLine );
135 }
136
137 if ( getDebugLine() != null && !getDebugLine().isEmpty() )
138 {
139 cli.createArg()
140 .setLine( getDebugLine() );
141 }
142
143 resolveClasspath( cli, findStartClass( config ), config );
144
145 return cli;
146 }
147
148 @Nonnull
149 protected List<String> toCompleteClasspath( StartupConfiguration conf ) throws SurefireBooterForkException
150 {
151 AbstractPathConfiguration pathConfig = conf.getClasspathConfiguration();
152 if ( pathConfig.isClassPathConfig() == pathConfig.isModularPathConfig() )
153 {
154 throw new SurefireBooterForkException( "Could not find class-path config nor modular class-path either." );
155 }
156
157 Classpath bootClasspath = getBooterClasspath();
158 Classpath testClasspath = pathConfig.getTestClasspath();
159 Classpath providerClasspath = pathConfig.getProviderClasspath();
160 Classpath completeClasspath = join( join( bootClasspath, testClasspath ), providerClasspath );
161
162 log.debug( completeClasspath.getLogMessage( "boot classpath:" ) );
163 log.debug( completeClasspath.getCompactLogMessage( "boot(compact) classpath:" ) );
164
165 return completeClasspath.getClassPath();
166 }
167
168 @Nonnull
169 private File getWorkingDirectory( int forkNumber )
170 throws SurefireBooterForkException
171 {
172 File cwd = replaceForkThreadsInPath( getWorkingDirectory(), forkNumber );
173
174 if ( !cwd.exists() && !cwd.mkdirs() )
175 {
176 throw new SurefireBooterForkException( "Cannot create workingDirectory " + cwd.getAbsolutePath() );
177 }
178
179 if ( !cwd.isDirectory() )
180 {
181 throw new SurefireBooterForkException(
182 "WorkingDirectory " + cwd.getAbsolutePath() + " exists and is not a directory" );
183 }
184 return cwd;
185 }
186
187
188
189
190
191
192
193
194 @Nonnull
195 private String interpolateArgLineWithPropertyExpressions()
196 {
197 if ( getArgLine() == null )
198 {
199 return "";
200 }
201
202 String resolvedArgLine = getArgLine().trim();
203
204 if ( resolvedArgLine.isEmpty() )
205 {
206 return "";
207 }
208
209 for ( final String key : getModelProperties().stringPropertyNames() )
210 {
211 String field = "@{" + key + "}";
212 if ( getArgLine().contains( field ) )
213 {
214 resolvedArgLine = resolvedArgLine.replace( field, getModelProperties().getProperty( key, "" ) );
215 }
216 }
217
218 return resolvedArgLine;
219 }
220
221 @Nonnull
222 private static String stripNewLines( @Nonnull String argLine )
223 {
224 return argLine.replace( "\n", " " ).replace( "\r", " " );
225 }
226
227
228
229
230
231
232
233
234
235 @Nonnull
236 private static <K, V> Map<K, V> toImmutable( @Nullable Map<K, V> map )
237 {
238 return map == null ? Collections.<K, V>emptyMap() : new ImmutableMap<K, V>( map );
239 }
240
241 @Override
242 @Nonnull
243 public File getTempDirectory()
244 {
245 return tempDirectory;
246 }
247
248 @Override
249 @Nullable
250 protected String getDebugLine()
251 {
252 return debugLine;
253 }
254
255 @Override
256 @Nonnull
257 protected File getWorkingDirectory()
258 {
259 return workingDirectory;
260 }
261
262 @Override
263 @Nonnull
264 protected Properties getModelProperties()
265 {
266 return modelProperties;
267 }
268
269 @Override
270 @Nullable
271 protected String getArgLine()
272 {
273 return argLine;
274 }
275
276 @Override
277 @Nonnull
278 protected Map<String, String> getEnvironmentVariables()
279 {
280 return environmentVariables;
281 }
282
283 @Override
284 protected boolean isDebug()
285 {
286 return debug;
287 }
288
289 @Override
290 protected int getForkCount()
291 {
292 return forkCount;
293 }
294
295 @Override
296 protected boolean isReuseForks()
297 {
298 return reuseForks;
299 }
300
301 @Override
302 @Nonnull
303 protected Platform getPluginPlatform()
304 {
305 return pluginPlatform;
306 }
307
308 @Override
309 @Nonnull
310 protected JdkAttributes getJdkForTests()
311 {
312 return getPluginPlatform().getJdkExecAttributesForTests();
313 }
314
315 @Override
316 @Nonnull
317 protected Classpath getBooterClasspath()
318 {
319 return booterClasspath;
320 }
321
322 @Nonnull
323 private String newJvmArgLine( int forks )
324 {
325 String interpolatedArgs = stripNewLines( interpolateArgLineWithPropertyExpressions() );
326 String argsWithReplacedForkNumbers = replaceThreadNumberPlaceholders( interpolatedArgs, forks );
327 return extendJvmArgLine( argsWithReplacedForkNumbers );
328 }
329
330 @Nonnull
331 private static String findStartClass( StartupConfiguration config )
332 {
333 return config.isShadefire() ? relocate( DEFAULT_PROVIDER_CLASS ) : DEFAULT_PROVIDER_CLASS;
334 }
335 }