From 0132faa5c6ae9e8720637905ac45411218bb31c0 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Mon, 6 Jan 2003 13:58:31 +0000 Subject: [PATCH] Check if loaderrefs are valid before casting them. Reported By: Jeroen Breedveld git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273762 13f79535-47bb-0310-9956-ffa450edef68 --- .../testcases/core/loaderref/loaderref.xml | 29 ++++++ .../testcases/core/loaderref/src/Task1.java | 5 + .../apache/tools/ant/taskdefs/Definer.java | 11 ++- .../org/apache/tools/ant/BuildFileTest.java | 97 +++++++++---------- 4 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 src/etc/testcases/core/loaderref/loaderref.xml create mode 100644 src/etc/testcases/core/loaderref/src/Task1.java diff --git a/src/etc/testcases/core/loaderref/loaderref.xml b/src/etc/testcases/core/loaderref/loaderref.xml new file mode 100644 index 000000000..942dc4d2e --- /dev/null +++ b/src/etc/testcases/core/loaderref/loaderref.xml @@ -0,0 +1,29 @@ + + + + + + + + + +This build file is intended to be used for testing Ant + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/core/loaderref/src/Task1.java b/src/etc/testcases/core/loaderref/src/Task1.java new file mode 100644 index 000000000..c5f232908 --- /dev/null +++ b/src/etc/testcases/core/loaderref/src/Task1.java @@ -0,0 +1,5 @@ +import org.apache.tools.ant.Task; + +public class Task1 extends Task { +} + diff --git a/src/main/org/apache/tools/ant/taskdefs/Definer.java b/src/main/org/apache/tools/ant/taskdefs/Definer.java index a4d8f176b..3a7095e17 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Definer.java +++ b/src/main/org/apache/tools/ant/taskdefs/Definer.java @@ -287,12 +287,17 @@ public abstract class Definer extends Task { if (loaderId != null) { Object reusedLoader = getProject().getReference(loaderId); if (reusedLoader != null) { + if (!(reusedLoader instanceof ClassLoader)) { + throw new BuildException("The specified loader id " + + loaderId + " does not reference a class loader"); + } + return (ClassLoader)reusedLoader; //if (reusedLoader instanceof AntClassLoader) { // return (AntClassLoader)reusedLoader; //} // In future the reference object may be the type - // if( reusedLoader instanceof Loader ) { + // if (reusedLoader instanceof Loader ) { // return ((Loader)reusedLoader).getLoader(project); // } } @@ -300,10 +305,10 @@ public abstract class Definer extends Task { ClassLoader al = null; - if( classpath ==null ) { + if (classpath == null) { // do we need to create another loader ? al=project.getCoreLoader(); - if( al != null ) { + if (al != null ) { return al; } } diff --git a/src/testcases/org/apache/tools/ant/BuildFileTest.java b/src/testcases/org/apache/tools/ant/BuildFileTest.java index 818d1926a..aacc739b5 100644 --- a/src/testcases/org/apache/tools/ant/BuildFileTest.java +++ b/src/testcases/org/apache/tools/ant/BuildFileTest.java @@ -60,41 +60,41 @@ import java.io.*; import java.net.URL; /** - * A BuildFileTest is a TestCase which executes targets from an Ant buildfile + * A BuildFileTest is a TestCase which executes targets from an Ant buildfile * for testing. - * - * This class provides a number of utility methods for particular build file - * tests which extend this class. - * + * + * This class provides a number of utility methods for particular build file + * tests which extend this class. + * * @author Nico Seessle * @author Conor MacNeill */ -public abstract class BuildFileTest extends TestCase { - +public abstract class BuildFileTest extends TestCase { + protected Project project; - + private StringBuffer logBuffer; private StringBuffer fullLogBuffer; private StringBuffer outBuffer; private StringBuffer errBuffer; private BuildException buildException; - + /** * Constructor for the BuildFileTest object * *@param name string to pass up to TestCase constructor - */ + */ public BuildFileTest(String name) { super(name); } - + /** - * run a target, expect for any build exception + * run a target, expect for any build exception * *@param target target to run *@param cause information string to reader of report - */ - protected void expectBuildException(String target, String cause) { + */ + protected void expectBuildException(String target, String cause) { expectSpecificBuildException(target, cause, null); } @@ -102,7 +102,7 @@ public abstract class BuildFileTest extends TestCase { * Assert that only the given message has been logged with a * priority >= INFO when running the given target. */ - protected void expectLog(String target, String log) { + protected void expectLog(String target, String log) { executeTarget(target); String realLog = getLog(); assertEquals(log, realLog); @@ -112,11 +112,11 @@ public abstract class BuildFileTest extends TestCase { * Assert that the given message has been logged with a priority * >= INFO when running the given target. */ - protected void expectLogContaining(String target, String log) { + protected void expectLogContaining(String target, String log) { executeTarget(target); String realLog = getLog(); assertTrue("expecting log to contain \""+log+"\" log was \"" - + realLog + "\"", + + realLog + "\"", realLog.indexOf(log) >= 0); } @@ -126,8 +126,8 @@ public abstract class BuildFileTest extends TestCase { * been called. * @pre logBuffer!=null * @return The log value - */ - protected String getLog() { + */ + protected String getLog() { return logBuffer.toString(); } @@ -135,7 +135,7 @@ public abstract class BuildFileTest extends TestCase { * Assert that the given message has been logged with a priority * >= DEBUG when running the given target. */ - protected void expectDebuglog(String target, String log) { + protected void expectDebuglog(String target, String log) { executeTarget(target); String realLog = getFullLog(); assertEquals(log, realLog); @@ -148,7 +148,7 @@ public abstract class BuildFileTest extends TestCase { * @pre fullLogBuffer!=null * @return The log value */ - protected String getFullLog() { + protected String getFullLog() { return fullLogBuffer.toString(); } @@ -158,8 +158,8 @@ public abstract class BuildFileTest extends TestCase { *@param target target to execute *@param output output to look for */ - - protected void expectOutput(String target, String output) { + + protected void expectOutput(String target, String output) { executeTarget(target); String realOutput = getOutput(); assertEquals(output, realOutput); @@ -172,8 +172,8 @@ public abstract class BuildFileTest extends TestCase { *@param output output to look for *@param error Description of Parameter */ - - protected void expectOutputAndError(String target, String output, String error) { + + protected void expectOutputAndError(String target, String output, String error) { executeTarget(target); String realOutput = getOutput(); assertEquals(output, realOutput); @@ -184,11 +184,11 @@ public abstract class BuildFileTest extends TestCase { protected String getOutput() { return cleanBuffer(outBuffer); } - + protected String getError() { return cleanBuffer(errBuffer); } - + protected BuildException getBuildException() { return buildException; } @@ -196,16 +196,16 @@ public abstract class BuildFileTest extends TestCase { private String cleanBuffer(StringBuffer buffer) { StringBuffer cleanedBuffer = new StringBuffer(); boolean cr = false; - for (int i = 0; i < buffer.length(); i++) { + for (int i = 0; i < buffer.length(); i++) { char ch = buffer.charAt(i); if (ch == '\r') { cr = true; continue; } - if (!cr) { + if (!cr) { cleanedBuffer.append(ch); - } else { + } else { if (ch == '\n') { cleanedBuffer.append(ch); } else { @@ -215,13 +215,13 @@ public abstract class BuildFileTest extends TestCase { } return cleanedBuffer.toString(); } - + /** * set up to run the named project * * @param filename name of project file to run - */ - protected void configureProject(String filename) throws BuildException { + */ + protected void configureProject(String filename) throws BuildException { logBuffer = new StringBuffer(); fullLogBuffer = new StringBuffer(); project = new Project(); @@ -230,16 +230,16 @@ public abstract class BuildFileTest extends TestCase { project.addBuildListener(new AntTestListener()); ProjectHelper.configureProject(project, new File(filename)); } - + /** * execute a target we have set up * @pre configureProject has been called * @param targetName target to run - */ - protected void executeTarget(String targetName) { + */ + protected void executeTarget(String targetName) { PrintStream sysOut = System.out; PrintStream sysErr = System.err; - try { + try { sysOut.flush(); sysErr.flush(); outBuffer = new StringBuffer(); @@ -252,13 +252,13 @@ public abstract class BuildFileTest extends TestCase { fullLogBuffer = new StringBuffer(); buildException = null; project.executeTarget(targetName); - } finally { + } finally { System.setOut(sysOut); System.setErr(sysErr); } - + } - + /** * Get the project which has been configured for a test. * @@ -277,14 +277,14 @@ public abstract class BuildFileTest extends TestCase { } /** - * run a target, wait for a build exception + * run a target, wait for a build exception * *@param target target to run *@param cause information string to reader of report *@param msg the message value of the build exception we are waiting for set to null for any build exception to be valid - */ - protected void expectSpecificBuildException(String target, String cause, String msg) { + */ + protected void expectSpecificBuildException(String target, String cause, String msg) { try { executeTarget(target); } catch (org.apache.tools.ant.BuildException ex) { @@ -298,17 +298,16 @@ public abstract class BuildFileTest extends TestCase { } fail("Should throw BuildException because: " + cause); } - + /** * run a target, expect an exception string * containing the substring we look for (case sensitive match) * *@param target target to run *@param cause information string to reader of report - *@param msg the message value of the build exception we are waiting for *@param contains substring of the build exception to look for */ - protected void expectBuildExceptionContaining(String target, String cause, String contains) { + protected void expectBuildExceptionContaining(String target, String cause, String contains) { try { executeTarget(target); } catch (org.apache.tools.ant.BuildException ex) { @@ -320,7 +319,7 @@ public abstract class BuildFileTest extends TestCase { } fail("Should throw BuildException because: " + cause); } - + /** * call a target, verify property is as expected @@ -399,7 +398,7 @@ public abstract class BuildFileTest extends TestCase { * an output stream which saves stuff to our buffer. */ private class AntOutputStream extends java.io.OutputStream { - public void write(int b) { + public void write(int b) { outBuffer.append((char)b); } } @@ -474,7 +473,7 @@ public abstract class BuildFileTest extends TestCase { logBuffer.append(event.getMessage()); } fullLogBuffer.append(event.getMessage()); - + } }