|
| 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