From 30b8b73ffa9bac34e10528dc5bd7376887617435 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 12 Jul 2001 07:09:19 +0000 Subject: [PATCH] will now try to include the task-definition itself as well as the needed support classes (Ant and JUnit itself) in the classpath if running in forked mode - this means that you shouldn't have to use different classpath elements for "in VM" and "fork" mode any longer. Doesn't work for JDK 1.1 as the URL returned by Class.getResource doesn't contain the name of the JAR file there. PR: 1239 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269323 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 ++ build.xml | 9 ---- .../taskdefs/optional/junit/JUnitTask.java | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 7ca362571..5a0f1a1e3 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -166,6 +166,9 @@ Fixed bugs: * closes remote connection when it's done. +* tries to include all necessary classes for the task itself + to the classpath when running in fork mode - doesn't work for JDK 1.1 + Changes from Ant 1.2 to Ant 1.3 =========================================== diff --git a/build.xml b/build.xml index 09254e4ed..78eca5a96 100644 --- a/build.xml +++ b/build.xml @@ -711,13 +711,4 @@ - - - - diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java index 30773fdcd..9152ac752 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java @@ -79,6 +79,8 @@ import java.util.Properties; import java.util.Random; import java.util.Vector; +import java.net.URL; + /** * Ant task to run JUnit tests. * @@ -280,6 +282,17 @@ public class JUnitTask extends Task { commandline.setClassname("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"); } + /** + * Adds the jars or directories containing Ant, this task and + * JUnit to the classpath - this should make the forked JVM work + * without having to specify the directly. + */ + public void init() { + addClasspathEntry("/junit/framework/TestCase.class"); + addClasspathEntry("/org/apache/tools/ant/Task.class"); + addClasspathEntry("/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.class"); + } + /** * Runs the testcase. */ @@ -523,4 +536,36 @@ public class JUnitTask extends Task { return null; } + /** + * Search for the given resource and add the directory or archive + * that contains it to the classpath. + * + *

Doesn't work for archives in JDK 1.1 as the URL returned by + * getResource doesn't contain the name of the archive.

+ */ + protected void addClasspathEntry(String resource) { + URL url = getClass().getResource(resource); + if (url != null) { + String u = url.toString(); + if (u.startsWith("jar:file:")) { + int pling = u.indexOf("!"); + String jarName = u.substring(9, pling); + log("Implicitly adding "+jarName+" to classpath", + Project.MSG_DEBUG); + createClasspath().setLocation(new File((new File(jarName)).getAbsolutePath())); + } else if (u.startsWith("file:")) { + int tail = u.indexOf(resource); + String dirName = u.substring(5, tail); + log("Implicitly adding "+dirName+" to classpath", + Project.MSG_DEBUG); + createClasspath().setLocation(new File((new File(dirName)).getAbsolutePath())); + } else { + log("Don\'t know how to handle resource URL "+u, + Project.MSG_DEBUG); + } + } else { + log("Couldn\'t find "+resource, Project.MSG_DEBUG); + } + } + }