Browse Source

Taking advantage of Throwable.cause.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@808169 13f79535-47bb-0310-9956-ffa450edef68
master
Jesse N. Glick 16 years ago
parent
commit
11c355f54f
5 changed files with 25 additions and 82 deletions
  1. +4
    -61
      src/main/org/apache/tools/ant/BuildException.java
  2. +1
    -1
      src/main/org/apache/tools/ant/DefaultLogger.java
  3. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java
  4. +17
    -17
      src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java
  5. +2
    -2
      src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java

+ 4
- 61
src/main/org/apache/tools/ant/BuildException.java View File

@@ -17,9 +17,6 @@
*/
package org.apache.tools.ant;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
* Signals an error condition during a build
*/
@@ -27,9 +24,6 @@ public class BuildException extends RuntimeException {

private static final long serialVersionUID = -5419014565354664240L;

/** Exception that might have caused this one. */
private Throwable cause;

/** Location in the build file where the exception occurred */
private Location location = Location.UNKNOWN_LOCATION;

@@ -61,7 +55,7 @@ public class BuildException extends RuntimeException {
*/
public BuildException(String message, Throwable cause) {
super(message);
this.cause = cause;
initCause(cause);
}

/**
@@ -87,8 +81,7 @@ public class BuildException extends RuntimeException {
* Should not be <code>null</code>.
*/
public BuildException(Throwable cause) {
super(cause.toString());
this.cause = cause;
super(cause);
}

/**
@@ -124,19 +117,10 @@ public class BuildException extends RuntimeException {
*
* @return the nested exception, or <code>null</code> if no
* exception is associated with this one
* @deprecated Use {@link #getCause} instead.
*/
public Throwable getException() {
return cause;
}

/**
* Returns the nested exception, if any.
*
* @return the nested exception, or <code>null</code> if no
* exception is associated with this one
*/
public Throwable getCause() {
return getException();
return getCause();
}

/**
@@ -167,45 +151,4 @@ public class BuildException extends RuntimeException {
return location;
}

/**
* Prints the stack trace for this exception and any
* nested exception to <code>System.err</code>.
*/
public void printStackTrace() {
printStackTrace(System.err);
}

/**
* Prints the stack trace of this exception and any nested
* exception to the specified PrintStream.
*
* @param ps The PrintStream to print the stack trace to.
* Must not be <code>null</code>.
*/
public void printStackTrace(PrintStream ps) {
synchronized (ps) {
super.printStackTrace(ps);
if (cause != null) {
ps.println("--- Nested Exception ---");
cause.printStackTrace(ps);
}
}
}

/**
* Prints the stack trace of this exception and any nested
* exception to the specified PrintWriter.
*
* @param pw The PrintWriter to print the stack trace to.
* Must not be <code>null</code>.
*/
public void printStackTrace(PrintWriter pw) {
synchronized (pw) {
super.printStackTrace(pw);
if (cause != null) {
pw.println("--- Nested Exception ---");
cause.printStackTrace(pw);
}
}
}
}

+ 1
- 1
src/main/org/apache/tools/ant/DefaultLogger.java View File

@@ -132,7 +132,7 @@ public class DefaultLogger implements BuildLogger {

static void throwableMessage(StringBuffer m, Throwable error, boolean verbose) {
while (error instanceof BuildException) { // #43398
Throwable cause = ((BuildException) error).getCause();
Throwable cause = error.getCause();
if (cause == null) {
break;
}


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java View File

@@ -368,7 +368,7 @@ public abstract class AbstractCvsTask extends Task {
if (failOnError) {
throw(e);
}
Throwable t = e.getException();
Throwable t = e.getCause();
if (t == null) {
t = e;
}


+ 17
- 17
src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java View File

@@ -67,7 +67,7 @@ public class IntrospectionHelperTest extends TestCase {
ih.addText(p, this, "test2");
fail("test2 shouldn\'t be equal to test");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}

ih = IntrospectionHelper.getHelper(String.class);
@@ -166,14 +166,14 @@ public class IntrospectionHelperTest extends TestCase {
ih.createElement(p, this, "fourteen");
fail("fourteen throws NullPointerException");
} catch (BuildException be) {
assertTrue(be.getException() instanceof NullPointerException);
assertTrue(be.getCause() instanceof NullPointerException);
}

try {
ih.createElement(p, this, "fourteen");
fail("fifteen throws NullPointerException");
} catch (BuildException be) {
assertTrue(be.getException() instanceof NullPointerException);
assertTrue(be.getCause() instanceof NullPointerException);
}
}

@@ -322,97 +322,97 @@ public class IntrospectionHelperTest extends TestCase {
ih.setAttribute(p, this, "seven", "3");
fail("2 shouldn't be equals to three");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "eight", "2");
try {
ih.setAttribute(p, this, "eight", "3");
fail("2 shouldn't be equals to three - as int");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "nine", "2");
try {
ih.setAttribute(p, this, "nine", "3");
fail("2 shouldn't be equals to three - as Integer");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "ten", "2");
try {
ih.setAttribute(p, this, "ten", "3");
fail(projectBasedir+"2 shouldn't be equals to "+projectBasedir+"3");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "eleven", "2");
try {
ih.setAttribute(p, this, "eleven", "on");
fail("on shouldn't be false");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "twelve", "2");
try {
ih.setAttribute(p, this, "twelve", "on");
fail("on shouldn't be false");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project");
try {
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper");
fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
try {
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2");
fail("org.apache.tools.ant.Project2 doesn't exist");
} catch (BuildException be) {
assertTrue(be.getException() instanceof ClassNotFoundException);
assertTrue(be.getCause() instanceof ClassNotFoundException);
}
ih.setAttribute(p, this, "fourteen", "2");
try {
ih.setAttribute(p, this, "fourteen", "on");
fail("2 shouldn't be equals to three - as StringBuffer");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "fifteen", "abcd");
try {
ih.setAttribute(p, this, "fifteen", "on");
fail("o shouldn't be equal to a");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "sixteen", "abcd");
try {
ih.setAttribute(p, this, "sixteen", "on");
fail("o shouldn't be equal to a");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "seventeen", "17");
try {
ih.setAttribute(p, this, "seventeen", "3");
fail("17 shouldn't be equals to three");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "eightteen", "18");
try {
ih.setAttribute(p, this, "eightteen", "3");
fail("18 shouldn't be equals to three");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "nineteen", "19");
try {
ih.setAttribute(p, this, "nineteen", "3");
fail("19 shouldn't be equals to three");
} catch (BuildException be) {
assertTrue(be.getException() instanceof AssertionFailedError);
assertTrue(be.getCause() instanceof AssertionFailedError);
}
}



+ 2
- 2
src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java View File

@@ -70,7 +70,7 @@ public class SQLExecTest extends TestCase {
try {
sql.execute();
} catch (BuildException e){
assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1);
assertTrue(e.getCause().getMessage().indexOf("No suitable Driver") != -1);
}
assertTrue(SQLExec.getLoaderMap().containsKey(NULL_DRIVER));
assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER));
@@ -83,7 +83,7 @@ public class SQLExecTest extends TestCase {
try {
sql.execute();
} catch (BuildException e){
assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1);
assertTrue(e.getCause().getMessage().indexOf("No suitable Driver") != -1);
}
assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER));
assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER));


Loading…
Cancel
Save