diff --git a/WHATSNEW b/WHATSNEW index f0d9472af..19389b53f 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -132,6 +132,10 @@ Fixed bugs: * file names that include spaces need to be quoted inside the @argfile argument using and JDK 1.4. Bugzilla Report 16871. +* didn't work with custom formatters that were only available + on the user specified classpath when a timeout occured. Bugzilla + Report 19953. + Other changes: -------------- * Six new Clearcase tasks added. diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java index 6e5962abc..3c87031b0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001-2002 The Apache Software Foundation. All rights + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -57,6 +57,7 @@ package org.apache.tools.ant.taskdefs.optional.junit; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; +import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -172,14 +173,31 @@ public class FormatterElement { return useFile; } + /** + * @since Ant 1.2 + */ JUnitResultFormatter createFormatter() throws BuildException { + return createFormatter(null); + } + + /** + * @since Ant 1.6 + */ + JUnitResultFormatter createFormatter(ClassLoader loader) + throws BuildException { + if (classname == null) { throw new BuildException("you must specify type or classname"); } Class f = null; try { - f = Class.forName(classname); + if (loader == null) { + f = Class.forName(classname); + } else { + f = loader.loadClass(classname); + AntClassLoader.initializeClass(f); + } } catch (ClassNotFoundException e) { throw new BuildException(e); } 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 298cfb5c0..f1b63ad5d 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 @@ -835,26 +835,8 @@ public class JUnitTask extends Task { log("Using System properties " + System.getProperties(), Project.MSG_VERBOSE); Path userClasspath = commandline.getClasspath(); - Path classpath = userClasspath == null - ? null - : (Path) userClasspath.clone(); - if (classpath != null) { - if (includeAntRuntime) { - log("Implicitly adding " + antRuntimeClasses - + " to CLASSPATH", Project.MSG_VERBOSE); - classpath.append(antRuntimeClasses); - } - - cl = getProject().createClassLoader(classpath); - cl.setParentFirst(false); - cl.addJavaLibraries(); - log("Using CLASSPATH " + cl.getClasspath(), - Project.MSG_VERBOSE); - - // make sure the test will be accepted as a TestCase - cl.addSystemPackageRoot("junit"); - // will cause trouble in JDK 1.1 if omitted - cl.addSystemPackageRoot("org.apache.tools.ant"); + if (userClasspath != null) { + cl = createClassLoader(); cl.setThreadContextLoader(); } runner = new JUnitTestRunner(test, test.getHaltonerror(), @@ -880,7 +862,7 @@ public class JUnitTask extends Task { } else { fe.setOutput(getDefaultOutput()); } - runner.addFormatter(fe.createFormatter()); + runner.addFormatter(fe.createFormatter(cl)); } runner.run(); @@ -1013,10 +995,11 @@ public class JUnitTask extends Task { */ private void logTimeout(FormatterElement[] feArray, JUnitTest test) { + AntClassLoader cl = createClassLoader(); for (int i = 0; i < feArray.length; i++) { FormatterElement fe = feArray[i]; File outFile = getOutput(fe, test); - JUnitResultFormatter formatter = fe.createFormatter(); + JUnitResultFormatter formatter = fe.createFormatter(cl); if (outFile != null && formatter != null) { try { OutputStream out = new FileOutputStream(outFile); @@ -1040,4 +1023,35 @@ public class JUnitTask extends Task { } } + /** + * Creates and configures an AntClassLoader instance from the + * nested classpath element. + * + * @return null if there is no user-specified classpath. + * + * @since Ant 1.6 + */ + private AntClassLoader createClassLoader() { + AntClassLoader cl = null; + Path userClasspath = commandline.getClasspath(); + if (userClasspath != null) { + Path classpath = (Path) userClasspath.clone(); + if (includeAntRuntime) { + log("Implicitly adding " + antRuntimeClasses + + " to CLASSPATH", Project.MSG_VERBOSE); + classpath.append(antRuntimeClasses); + } + + cl = getProject().createClassLoader(classpath); + cl.setParentFirst(false); + cl.addJavaLibraries(); + log("Using CLASSPATH " + cl.getClasspath(), Project.MSG_VERBOSE); + + // make sure the test will be accepted as a TestCase + cl.addSystemPackageRoot("junit"); + // will cause trouble in JDK 1.1 if omitted + cl.addSystemPackageRoot("org.apache.tools.ant"); + } + return cl; + } }