bodewig 2003/05/23 07:15:43
Modified: . WHATSNEW src/main/org/apache/tools/ant/taskdefs/optional/junit FormatterElement.java JUnitTask.java Log: Load formatter from user specified classpath - even in the timeout case. PR: 19953 Submitted by: Mariano Benitez <mariano at fuegolabs dot com> Revision Changes Path 1.424 +4 -0 ant/WHATSNEW Index: WHATSNEW =================================================================== RCS file: /home/cvs/ant/WHATSNEW,v retrieving revision 1.423 retrieving revision 1.424 diff -u -r1.423 -r1.424 --- WHATSNEW 23 May 2003 13:40:34 -0000 1.423 +++ WHATSNEW 23 May 2003 14:15:41 -0000 1.424 @@ -132,6 +132,10 @@ * file names that include spaces need to be quoted inside the @argfile argument using <javadoc> and JDK 1.4. Bugzilla Report 16871. +* <junit> 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. 1.12 +20 -2 ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java Index: FormatterElement.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- FormatterElement.java 7 Mar 2003 11:23:06 -0000 1.11 +++ FormatterElement.java 23 May 2003 14:15:42 -0000 1.12 @@ -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 @@ 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 @@ 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); } 1.64 +37 -23 ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java Index: JUnitTask.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java,v retrieving revision 1.63 retrieving revision 1.64 diff -u -r1.63 -r1.64 --- JUnitTask.java 12 May 2003 14:00:09 -0000 1.63 +++ JUnitTask.java 23 May 2003 14:15:42 -0000 1.64 @@ -835,26 +835,8 @@ 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 @@ } else { fe.setOutput(getDefaultOutput()); } - runner.addFormatter(fe.createFormatter()); + runner.addFormatter(fe.createFormatter(cl)); } runner.run(); @@ -1013,10 +995,11 @@ */ 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 @@ } } + /** + * 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; + } }