diff --git a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitHelper.java b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitHelper.java index b9984cc2e..658f1aa5b 100644 --- a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitHelper.java +++ b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitHelper.java @@ -54,10 +54,15 @@ package org.apache.tools.ant.taskdefs.optional.junit; import java.lang.reflect.Method; +import java.io.File; +import java.net.URL; import junit.framework.Test; import junit.framework.TestSuite; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.Path; + /** * A set of helpers functions to deal with JUnit. * @@ -124,4 +129,51 @@ public final class JUnitHelper { } return null; } + + /** + * Search for the given resource and return the directory or archive + * that contains it. + * + *
Doesn't work for archives in JDK 1.1 as the URL returned by + * getResource doesn't contain the name of the archive.
+ * + * @param resource the resource to look for in the JVM classpath. + * @return the file or directory containing the resource or + * null if it does not know how to handle it. + */ + public static File getResourceEntry(String resource){ + URL url = JUnitHelper.class.getResource(resource); + if (url != null) { + // can't find the resource... + return null; + } + String u = url.toString(); + if (u.startsWith("jar:file:")) { + int pling = u.indexOf("!"); + String jarName = u.substring(9, pling); + return new File((new File(jarName)).getAbsolutePath()); + } else if (u.startsWith("file:")) { + int tail = u.indexOf(resource); + String dirName = u.substring(5, tail); + return new File((new File(dirName)).getAbsolutePath()); + } + // don't know how to handle it... + return null; + } + + /** + * Add the entry corresponding to a specific resource to the + * specified path instance. The entry can either be a directory + * or an archive. + * @param path the path to add the resource entry to. + * @param resource the resource to look for. + * @see #getResourceEntry(String) + */ + public static void addClasspathEntry(Path path, String resource){ + File f = getResourceEntry(resource); + if (f != null) { + path.createPathElement().setLocation(f); + } + } + } diff --git a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java index 8166e4ab2..571585684 100644 --- a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java +++ b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java @@ -53,12 +53,24 @@ */ package org.apache.tools.ant.taskdefs.optional.junit; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Properties; import java.util.Vector; -import org.apache.tools.ant.Task; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.optional.junit.formatter.Formatter; -import org.apache.tools.ant.taskdefs.optional.junit.remote.TestRunner; +import org.apache.tools.ant.taskdefs.Execute; +import org.apache.tools.ant.taskdefs.LogStreamHandler; +import org.apache.tools.ant.types.Commandline; +import org.apache.tools.ant.types.CommandlineJava; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.util.FileUtils; /** * @@ -81,10 +93,70 @@ public class JUnitTask extends Task { /** stop the test run if an error occurs */ private boolean haltOnError = false; + /** the command line to launch the TestRunner */ + private CommandlineJava cmd = new CommandlineJava(); + // task implementation public void execute() throws BuildException { + File tmp = configureTestRunner(); + Execute execute = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN)); + execute.setCommandline(cmd.getCommandline()); + execute.setAntRun(project); + + log("Executing: " + cmd.toString(), Project.MSG_VERBOSE); + int retVal; + try { + retVal = execute.execute(); + } catch (IOException e) { + throw new BuildException("Process fork failed.", e, location); + } finally { + tmp.delete(); + } + + } + /** + * Configure the runner with the appropriate configuration file. + * @return the reference to the temporary configuration file + * to be deleted once the TestRunner has ended. + */ + public File configureTestRunner() { + Properties props = new Properties(); + props.setProperty("debug", "true"); + props.setProperty("host", "127.0.0.1"); + props.setProperty("port", String.valueOf(port)); + StringBuffer classnames = new StringBuffer(); + //@fixme get all test classes to run... + final int testcount = 0; + for (int i = 0; i < testcount; i++) { + classnames.append("