Browse Source

<junit> 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
master
Stefan Bodewig 24 years ago
parent
commit
30b8b73ffa
3 changed files with 48 additions and 9 deletions
  1. +3
    -0
      WHATSNEW
  2. +0
    -9
      build.xml
  3. +45
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java

+ 3
- 0
WHATSNEW View File

@@ -166,6 +166,9 @@ Fixed bugs:

* <ftp> closes remote connection when it's done.

* <junit> 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
===========================================



+ 0
- 9
build.xml View File

@@ -711,13 +711,4 @@
<antcall target="dist-lite"/>
</target>
<!--
===================================================================
Creates the Ant website
===================================================================
-->
<target name="website"
description="--> creates the Ant website">
<ant dir="webpage" target="docs" />
</target>
</project>

+ 45
- 0
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java View File

@@ -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.
*
* <p>Doesn't work for archives in JDK 1.1 as the URL returned by
* getResource doesn't contain the name of the archive.</p>
*/
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);
}
}

}

Loading…
Cancel
Save