summaryrefslogtreecommitdiffstats
path: root/maven-plugins/tags/maven-osgi-junit-plugin-1.0/src/main/java/org/apache/tuscany/maven/plugin/surefire/ForkConfiguration.java
blob: eb391970ca9c5082fa48cf48c74ae7cca473bbc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package org.apache.tuscany.maven.plugin.surefire;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

import org.apache.maven.surefire.booter.SurefireBooterForkException;
import org.apache.maven.surefire.booter.shade.org.codehaus.plexus.util.cli.Commandline;
import org.apache.maven.surefire.util.UrlUtils;
import org.codehaus.plexus.util.StringUtils;

/**
 * Derived from surefire-booter 2.4.3
 * Configuration for forking tests.
 *
 */
@SuppressWarnings("unchecked")
public class ForkConfiguration {
    public static final String FORK_ONCE = "once";

    public static final String FORK_ALWAYS = "always";

    public static final String FORK_NEVER = "never";

    private String forkMode;

    private boolean useSystemClassLoader;
    private boolean useManifestOnlyJar;

    private Properties systemProperties;

    private String jvmExecutable;

    private String argLine;

    private Map environmentVariables;

    private File workingDirectory;

    private boolean debug;

    private String debugLine;

    public void setForkMode(String forkMode) {
        if ("pertest".equalsIgnoreCase(forkMode)) {
            this.forkMode = FORK_ALWAYS;
        } else if ("none".equalsIgnoreCase(forkMode)) {
            this.forkMode = FORK_NEVER;
        } else if (forkMode.equals(FORK_NEVER) || forkMode.equals(FORK_ONCE) || forkMode.equals(FORK_ALWAYS)) {
            this.forkMode = forkMode;
        } else {
            throw new IllegalArgumentException("Fork mode " + forkMode + " is not a legal value");
        }
    }

    public boolean isForking() {
        return !FORK_NEVER.equals(forkMode);
    }

    public void setUseSystemClassLoader(boolean useSystemClassLoader) {
        this.useSystemClassLoader = useSystemClassLoader;
    }

    public boolean isUseSystemClassLoader() {
        return useSystemClassLoader;
    }

    public void setSystemProperties(Properties systemProperties) {
        this.systemProperties = (Properties)systemProperties.clone();
    }

    public void setJvmExecutable(String jvmExecutable) {
        this.jvmExecutable = jvmExecutable;
    }

    public void setArgLine(String argLine) {
        this.argLine = argLine;
    }

    public void setDebugLine(String debugLine) {
        this.debugLine = debugLine;
    }

    public void setEnvironmentVariables(Map environmentVariables) {
        this.environmentVariables = new HashMap(environmentVariables);
    }

    public void setWorkingDirectory(File workingDirectory) {
        this.workingDirectory = workingDirectory;
    }

    public String getForkMode() {
        return forkMode;
    }

    public Properties getSystemProperties() {
        return systemProperties;
    }

    /**
     * @throws SurefireBooterForkException
     * @deprecated use the 2-arg alternative.
     */
    public Commandline createCommandLine(List classPath) throws SurefireBooterForkException {
        return createCommandLine(classPath, false);
    }

    public Commandline createCommandLine(List classPath, boolean useJar) throws SurefireBooterForkException {
        Commandline cli = new Commandline();

        cli.setExecutable(jvmExecutable);

        if (argLine != null) {
            cli.createArg().setLine(argLine);
        }

        if (environmentVariables != null) {
            Iterator iter = environmentVariables.keySet().iterator();

            while (iter.hasNext()) {
                String key = (String)iter.next();

                String value = (String)environmentVariables.get(key);

                cli.addEnvironment(key, value);
            }
        }

        if (debugLine != null && !"".equals(debugLine)) {
            cli.createArg().setLine(debugLine);
        }

        if (useJar) {
            File jarFile;
            try {
                jarFile = createJar(classPath);
            } catch (IOException e) {
                throw new SurefireBooterForkException("Error creating archive file", e);
            }

            cli.createArg().setValue("-jar");

            cli.createArg().setValue(jarFile.getAbsolutePath());
        } else {
            cli.createArg().setValue("-classpath");

            cli.createArg().setValue(StringUtils.join(classPath.iterator(), File.pathSeparator));

            cli.createArg().setValue(OSGiSurefireBooter.class.getName());
        }

        cli.setWorkingDirectory(workingDirectory.getAbsolutePath());

        return cli;
    }

    /**
     * Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry
     * for all classpath elements.
     *
     * @param classPath List<String> of all classpath elements.
     * @return
     * @throws IOException
     */
    private File createJar(List classPath) throws IOException {
        File file = File.createTempFile("surefirebooter", ".jar");
        if (!debug) {
            file.deleteOnExit();
        }
        FileOutputStream fos = new FileOutputStream(file);
        JarOutputStream jos = new JarOutputStream(fos);
        jos.setLevel(JarOutputStream.STORED);
        JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
        jos.putNextEntry(je);

        Manifest man = new Manifest();

        // we can't use StringUtils.join here since we need to add a '/' to
        // the end of directory entries - otherwise the jvm will ignore them.
        String cp = "";
        for (Iterator it = classPath.iterator(); it.hasNext();) {
            String el = (String)it.next();
            // NOTE: if File points to a directory, this entry MUST end in '/'.
            cp += UrlUtils.getURL(new File(el)).toExternalForm() + " ";
        }

        man.getMainAttributes().putValue("Manifest-Version", "1.0");
        man.getMainAttributes().putValue("Class-Path", cp.trim());
        man.getMainAttributes().putValue("Main-Class", OSGiSurefireBooter.class.getName());

        man.write(jos);
        jos.close();

        return file;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public boolean isDebug() {
        return debug;
    }

    public void setUseManifestOnlyJar(boolean useManifestOnlyJar) {
        this.useManifestOnlyJar = useManifestOnlyJar;
    }

    public boolean isUseManifestOnlyJar() {
        return useManifestOnlyJar;
    }
}