Skip to content

Commit c9ca84f

Browse files
committed
Support for fork mode in junitlauncher
1 parent 3f36f0b commit c9ca84f

13 files changed

Lines changed: 1457 additions & 381 deletions

File tree

src/etc/testcases/taskdefs/optional/junitlauncher.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
<path id="junit.engine.vintage.classpath">
3232
<fileset dir="../../../../../lib/optional" includes="junit-vintage-engine*.jar"/>
33+
<fileset dir="../../../../../lib/optional" includes="junit-*.jar"/>
34+
<fileset dir="../../../../../lib/optional" includes="hamcrest*.jar"/>
3335
</path>
3436

3537
<path id="junit.engine.jupiter.classpath">
@@ -109,5 +111,15 @@
109111
</testclasses>
110112
</junitlauncher>
111113
</target>
114+
115+
<target name="test-basic-fork" depends="init">
116+
<junitlauncher>
117+
<classpath refid="test.classpath"/>
118+
<test name="org.example.junitlauncher.vintage.JUnit4SampleTest" outputdir="${output.dir}">
119+
<fork dir="${basedir}"/>
120+
<listener type="legacy-xml" sendSysErr="true" sendSysOut="true"/>
121+
</test>
122+
</junitlauncher>
123+
</target>
112124
</project>
113125

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.tools.ant.taskdefs.optional.junitlauncher;
20+
21+
/**
22+
* Constants used within the junitlauncher task
23+
*/
24+
final class Constants {
25+
26+
static final int FORK_EXIT_CODE_SUCCESS = 0;
27+
static final int FORK_EXIT_CODE_EXCEPTION = 1;
28+
static final int FORK_EXIT_CODE_TESTS_FAILED = 2;
29+
static final int FORK_EXIT_CODE_TIMED_OUT = 3;
30+
31+
static final String ARG_PROPERTIES = "--properties";
32+
static final String ARG_LAUNCH_DEFINITION = "--launch-definition";
33+
34+
35+
static final String LD_XML_ELM_LAUNCH_DEF = "launch-definition";
36+
static final String LD_XML_ELM_TEST = "test";
37+
static final String LD_XML_ELM_TEST_CLASSES = "test-classes";
38+
static final String LD_XML_ATTR_HALT_ON_FAILURE = "haltOnFailure";
39+
static final String LD_XML_ATTR_OUTPUT_DIRECTORY = "outDir";
40+
static final String LD_XML_ATTR_INCLUDE_ENGINES = "includeEngines";
41+
static final String LD_XML_ATTR_EXCLUDE_ENGINES = "excludeEngines";
42+
static final String LD_XML_ATTR_CLASS_NAME = "classname";
43+
static final String LD_XML_ATTR_METHODS = "methods";
44+
static final String LD_XML_ATTR_PRINT_SUMMARY = "printSummary";
45+
static final String LD_XML_ELM_LISTENER = "listener";
46+
static final String LD_XML_ATTR_SEND_SYS_ERR = "sendSysErr";
47+
static final String LD_XML_ATTR_SEND_SYS_OUT = "sendSysOut";
48+
static final String LD_XML_ATTR_LISTENER_RESULT_FILE = "resultFile";
49+
50+
51+
private Constants() {
52+
53+
}
54+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.tools.ant.taskdefs.optional.junitlauncher;
20+
21+
import org.apache.tools.ant.BuildException;
22+
import org.apache.tools.ant.Project;
23+
import org.apache.tools.ant.Task;
24+
import org.apache.tools.ant.launch.AntMain;
25+
import org.apache.tools.ant.types.Commandline;
26+
import org.apache.tools.ant.types.CommandlineJava;
27+
import org.apache.tools.ant.types.Environment;
28+
import org.apache.tools.ant.types.Path;
29+
import org.apache.tools.ant.types.PropertySet;
30+
import org.apache.tools.ant.util.LoaderUtils;
31+
import org.junit.platform.commons.annotation.Testable;
32+
import org.junit.platform.engine.TestEngine;
33+
import org.junit.platform.launcher.core.LauncherFactory;
34+
35+
import java.io.File;
36+
37+
/**
38+
* Represents the {@code fork} element within test definitions of the
39+
* {@code junitlauncher} task
40+
*/
41+
public class ForkDefinition {
42+
43+
private boolean includeAntRuntimeLibraries = true;
44+
private boolean includeJunitPlatformLibraries = true;
45+
46+
private final CommandlineJava commandLineJava;
47+
private final Environment env = new Environment();
48+
49+
private String dir;
50+
private long timeout = -1;
51+
52+
ForkDefinition() {
53+
this.commandLineJava = new CommandlineJava();
54+
}
55+
56+
public void setDir(final String dir) {
57+
this.dir = dir;
58+
}
59+
60+
String getDir() {
61+
return this.dir;
62+
}
63+
64+
public void setTimeout(final long timeout) {
65+
this.timeout = timeout;
66+
}
67+
68+
long getTimeout() {
69+
return this.timeout;
70+
}
71+
72+
public Commandline.Argument createJvmArg() {
73+
return this.commandLineJava.createVmArgument();
74+
}
75+
76+
public void addConfiguredSysProperty(final Environment.Variable sysProp) {
77+
// validate that key/value are present
78+
sysProp.validate();
79+
this.commandLineJava.addSysproperty(sysProp);
80+
}
81+
82+
public void addConfiguredSysPropertySet(final PropertySet propertySet) {
83+
this.commandLineJava.addSyspropertyset(propertySet);
84+
}
85+
86+
public void addConfiguredEnv(final Environment.Variable var) {
87+
this.env.addVariable(var);
88+
}
89+
90+
public void addConfiguredModulePath(final Path modulePath) {
91+
this.commandLineJava.createModulepath(modulePath.getProject()).add(modulePath);
92+
}
93+
94+
public void addConfiguredUpgradeModulePath(final Path upgradeModulePath) {
95+
this.commandLineJava.createUpgrademodulepath(upgradeModulePath.getProject()).add(upgradeModulePath);
96+
}
97+
98+
Environment getEnv() {
99+
return this.env;
100+
}
101+
102+
/**
103+
* Generates a new {@link CommandlineJava} constructed out of the configurations set on this
104+
* {@link ForkDefinition}
105+
*
106+
* @param task The junitlaunchertask for which this is a fork definition
107+
* @return
108+
*/
109+
CommandlineJava generateCommandLine(final JUnitLauncherTask task) {
110+
final CommandlineJava cmdLine;
111+
try {
112+
cmdLine = (CommandlineJava) this.commandLineJava.clone();
113+
} catch (CloneNotSupportedException e) {
114+
throw new BuildException(e);
115+
}
116+
cmdLine.setClassname(StandaloneLauncher.class.getName());
117+
// VM arguments
118+
final Project project = task.getProject();
119+
final Path antRuntimeResourceSources = new Path(project);
120+
if (this.includeAntRuntimeLibraries) {
121+
addAntRuntimeResourceSource(antRuntimeResourceSources, task, toResourceName(AntMain.class));
122+
addAntRuntimeResourceSource(antRuntimeResourceSources, task, toResourceName(Task.class));
123+
addAntRuntimeResourceSource(antRuntimeResourceSources, task, toResourceName(JUnitLauncherTask.class));
124+
}
125+
126+
if (this.includeJunitPlatformLibraries) {
127+
// platform-engine
128+
addAntRuntimeResourceSource(antRuntimeResourceSources, task, toResourceName(TestEngine.class));
129+
// platform-launcher
130+
addAntRuntimeResourceSource(antRuntimeResourceSources, task, toResourceName(LauncherFactory.class));
131+
// platform-commons
132+
addAntRuntimeResourceSource(antRuntimeResourceSources, task, toResourceName(Testable.class));
133+
}
134+
final Path classPath = cmdLine.createClasspath(project);
135+
classPath.createPath().append(antRuntimeResourceSources);
136+
137+
return cmdLine;
138+
}
139+
140+
private static boolean addAntRuntimeResourceSource(final Path path, final JUnitLauncherTask task, final String resource) {
141+
final File f = LoaderUtils.getResourceSource(task.getClass().getClassLoader(), resource);
142+
if (f == null) {
143+
task.log("Could not locate source of resource " + resource);
144+
return false;
145+
}
146+
task.log("Found source " + f + " of resource " + resource);
147+
path.createPath().setLocation(f);
148+
return true;
149+
}
150+
151+
private static String toResourceName(final Class klass) {
152+
final String name = klass.getName();
153+
return name.replaceAll("\\.", "/") + ".class";
154+
}
155+
156+
}

0 commit comments

Comments
 (0)