1
package org.apache.maven.surefire.booter;
4
* Licensed to the Apache Software Foundation (ASF) under one
5
* or more contributor license agreements. See the NOTICE file
6
* distributed with this work for additional information
7
* regarding copyright ownership. The ASF licenses this file
8
* to you under the Apache License, Version 2.0 (the
9
* "License"); you may not use this file except in compliance
10
* with the License. You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing,
15
* software distributed under the License is distributed on an
16
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
* KIND, either express or implied. See the License for the
18
* specific language governing permissions and limitations
22
import org.apache.maven.surefire.util.UrlUtils;
23
import org.codehaus.plexus.util.StringUtils;
24
import org.codehaus.plexus.util.cli.Commandline;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.util.HashMap;
30
import java.util.Iterator;
31
import java.util.List;
33
import java.util.Properties;
34
import java.util.jar.JarEntry;
35
import java.util.jar.JarOutputStream;
36
import java.util.jar.Manifest;
39
* Configuration for forking tests.
41
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
42
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
44
public class ForkConfiguration
46
public static final String FORK_ONCE = "once";
48
public static final String FORK_ALWAYS = "always";
50
public static final String FORK_NEVER = "never";
52
private String forkMode;
54
private boolean useSystemClassLoader;
55
private boolean useManifestOnlyJar;
57
private Properties systemProperties;
59
private String jvmExecutable;
61
private String argLine;
63
private Map environmentVariables;
65
private File workingDirectory;
67
private boolean debug;
69
private String debugLine;
71
public void setForkMode( String forkMode )
73
if ( "pertest".equalsIgnoreCase( forkMode ) )
75
this.forkMode = FORK_ALWAYS;
77
else if ( "none".equalsIgnoreCase( forkMode ) )
79
this.forkMode = FORK_NEVER;
81
else if ( forkMode.equals( FORK_NEVER ) || forkMode.equals( FORK_ONCE ) || forkMode.equals( FORK_ALWAYS ) )
83
this.forkMode = forkMode;
87
throw new IllegalArgumentException( "Fork mode " + forkMode + " is not a legal value" );
91
public boolean isForking()
93
return !FORK_NEVER.equals( forkMode );
96
public void setUseSystemClassLoader( boolean useSystemClassLoader )
98
this.useSystemClassLoader = useSystemClassLoader;
101
public boolean isUseSystemClassLoader()
103
return useSystemClassLoader;
106
public void setSystemProperties( Properties systemProperties )
108
this.systemProperties = (Properties) systemProperties.clone();
111
public void setJvmExecutable( String jvmExecutable )
113
this.jvmExecutable = jvmExecutable;
116
public void setArgLine( String argLine )
118
this.argLine = argLine;
121
public void setDebugLine( String debugLine )
123
this.debugLine = debugLine;
126
public void setEnvironmentVariables( Map environmentVariables )
128
this.environmentVariables = new HashMap( environmentVariables );
131
public void setWorkingDirectory( File workingDirectory )
133
this.workingDirectory = workingDirectory;
136
public String getForkMode()
141
public Properties getSystemProperties()
143
return systemProperties;
147
* @throws SurefireBooterForkException
148
* @deprecated use the 2-arg alternative.
150
public Commandline createCommandLine( List classPath )
151
throws SurefireBooterForkException
153
return createCommandLine( classPath, false );
156
public Commandline createCommandLine( List classPath, boolean useJar )
157
throws SurefireBooterForkException
159
Commandline cli = new Commandline();
161
cli.setExecutable( jvmExecutable );
163
if ( argLine != null )
165
cli.createArg().setLine( argLine );
168
if ( environmentVariables != null )
170
Iterator iter = environmentVariables.keySet().iterator();
172
while ( iter.hasNext() )
174
String key = (String) iter.next();
176
String value = (String) environmentVariables.get( key );
178
cli.addEnvironment( key, value );
182
if ( debugLine != null && !"".equals( debugLine ) )
184
cli.createArg().setLine( debugLine );
192
jarFile = createJar( classPath );
194
catch ( IOException e )
196
throw new SurefireBooterForkException( "Error creating archive file", e );
199
cli.createArg().setValue( "-jar" );
201
cli.createArg().setValue( jarFile.getAbsolutePath() );
205
cli.createArg().setValue( "-classpath" );
207
cli.createArg().setValue( StringUtils.join( classPath.iterator(), File.pathSeparator ) );
209
cli.createArg().setValue( SurefireBooter.class.getName() );
212
cli.setWorkingDirectory( workingDirectory.getAbsolutePath() );
218
* Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry
219
* for all classpath elements.
221
* @param classPath List<String> of all classpath elements.
223
* @throws IOException
225
private File createJar( List classPath )
228
File file = File.createTempFile( "surefirebooter", ".jar" );
232
FileOutputStream fos = new FileOutputStream( file );
233
JarOutputStream jos = new JarOutputStream( fos );
234
jos.setLevel( JarOutputStream.STORED );
235
JarEntry je = new JarEntry( "META-INF/MANIFEST.MF" );
236
jos.putNextEntry( je );
238
Manifest man = new Manifest();
240
// we can't use StringUtils.join here since we need to add a '/' to
241
// the end of directory entries - otherwise the jvm will ignore them.
243
for ( Iterator it = classPath.iterator(); it.hasNext(); )
245
String el = (String) it.next();
246
// NOTE: if File points to a directory, this entry MUST end in '/'.
247
cp += UrlUtils.getURL( new File( el ) ).toExternalForm() + " ";
250
man.getMainAttributes().putValue("Manifest-Version", "1.0");
251
man.getMainAttributes().putValue("Class-Path", cp.trim());
252
man.getMainAttributes().putValue("Main-Class", SurefireBooter.class.getName());
260
public void setDebug( boolean debug )
265
public boolean isDebug()
270
public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
272
this.useManifestOnlyJar = useManifestOnlyJar;
275
public boolean isUseManifestOnlyJar()
277
return useManifestOnlyJar;