diff --git a/src/java/org/apache/commons/lang/NotImplementedException.java b/src/java/org/apache/commons/lang/NotImplementedException.java index 23a2311eb..0b20ba320 100644 --- a/src/java/org/apache/commons/lang/NotImplementedException.java +++ b/src/java/org/apache/commons/lang/NotImplementedException.java @@ -19,9 +19,6 @@ import java.io.PrintStream; import java.io.PrintWriter; -import org.apache.commons.lang.exception.Nestable; -import org.apache.commons.lang.exception.NestableDelegate; - /** *

Thrown to indicate that a block of code has not been implemented. * This exception supplements UnsupportedOperationException @@ -29,9 +26,7 @@ * *

NotImplementedException represents the case where the * author has yet to implement the logic at this point in the program. - * This can act as an exception based TODO tag. - * Because this logic might be within a catch block, this exception - * suports exception chaining.

+ * This can act as an exception based TODO tag.

* *
  * public void foo() {
@@ -49,8 +44,7 @@
  * @since 2.0
  * @version $Id$
  */
-public class NotImplementedException
-        extends UnsupportedOperationException implements Nestable {
+public class NotImplementedException extends UnsupportedOperationException {
 
     private static final String DEFAULT_MESSAGE = "Code is not implemented";
 
@@ -61,17 +55,6 @@ public class NotImplementedException
      */
     private static final long serialVersionUID = -6894122266938754088L;
 
-    /**
-     * The exception helper to delegate nested exception handling to.
-     */
-    private NestableDelegate delegate = new NestableDelegate(this);
-
-    /**
-     * Holds the reference to the exception or error that caused
-     * this exception to be thrown.
-     */
-    private Throwable cause;
-
     //-----------------------------------------------------------------------
     /**
      * Constructs a new NotImplementedException with default message.
@@ -100,8 +83,7 @@ public NotImplementedException(String msg) {
      * @since 2.1
      */
     public NotImplementedException(Throwable cause) {
-        super(DEFAULT_MESSAGE);
-        this.cause = cause;
+        super(DEFAULT_MESSAGE, cause);
     }
 
     /**
@@ -113,8 +95,7 @@ public NotImplementedException(Throwable cause) {
      * @since 2.1
      */
     public NotImplementedException(String msg, Throwable cause) {
-        super(msg == null ? DEFAULT_MESSAGE : msg);
-        this.cause = cause;
+        super(msg == null ? DEFAULT_MESSAGE : msg, cause);
     }
 
     /**
@@ -127,174 +108,4 @@ public NotImplementedException(Class clazz) {
         super(clazz == null ? DEFAULT_MESSAGE : DEFAULT_MESSAGE + " in " + clazz);
     }
 
-    // -----------------------------------------------------------------------
-    /**
-     * Gets the root cause of this exception.
-     * @return the root cause of this exception.
-     * 
-     * @since 2.1
-     */
-    @Override
-    public Throwable getCause() {
-        return cause;
-    }
-
-    /**
-     * Gets the combined the error message of this and any nested errors.
-     *
-     * @return the error message
-     * @since 2.1
-     */
-    @Override
-    public String getMessage() {
-        if (super.getMessage() != null) {
-            return super.getMessage();
-        } else if (cause != null) {
-            return cause.toString();
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Returns the error message of the Throwable in the chain
-     * of Throwables at the specified index, numbered from 0.
-     *
-     * @param index  the index of the Throwable in the chain
-     * @return the error message, or null if the Throwable at the
-     *  specified index in the chain does not contain a message
-     * @throws IndexOutOfBoundsException if the index argument is
-     *  negative or not less than the count of Throwables in the chain
-     * @since 2.1
-     */
-    public String getMessage(int index) {
-        if (index == 0) {
-            return super.getMessage();
-        }
-        return delegate.getMessage(index);
-    }
-
-    /**
-     * Returns the error message of this and any nested Throwable objects.
-     * Each throwable returns a message, a null string is included in the array if
-     * there is no message for a particular Throwable.
-     *
-     * @return the error messages
-     * @since 2.1
-     */
-    public String[] getMessages() {
-        return delegate.getMessages();
-    }
-
-    /**
-     * Returns the Throwable in the chain by index.
-     *
-     * @param index  the index to retrieve
-     * @return the Throwable
-     * @throws IndexOutOfBoundsException if the index argument is
-     *  negative or not less than the count of Throwables in the chain
-     * @since 2.1
-     */
-    public Throwable getThrowable(int index) {
-        return delegate.getThrowable(index);
-    }
-
-    /**
-     * Returns the number of nested Throwables represented by
-     * this Nestable, including this Nestable.
-     *
-     * @return the throwable count
-     * @since 2.1
-     */
-    public int getThrowableCount() {
-        return delegate.getThrowableCount();
-    }
-
-    /**
-     * Returns this Nestable and any nested Throwables
-     * in an array of Throwables, one element for each
-     * Throwable.
-     *
-     * @return the Throwables
-     * @since 2.1
-     */
-    public Throwable[] getThrowables() {
-        return delegate.getThrowables();
-    }
-
-    /**
-     * Returns the index of the first occurrence of the specified type.
-     * If there is no match, -1 is returned.
-     *
-     * @param type  the type to search for
-     * @return index of the first occurrence of the type in the chain, or -1 if
-     *  the type is not found
-     * @since 2.1
-     */
-    public int indexOfThrowable(Class type) {
-        return delegate.indexOfThrowable(type, 0);
-    }
-
-    /**
-     * Returns the index of the first occurrence of the specified type starting
-     * from the specified index. If there is no match, -1 is returned.
-     *
-     * @param type  the type to search for
-     * @param fromIndex  the index of the starting position in the chain to be searched
-     * @return index of the first occurrence of the type in the chain, or -1 if
-     *  the type is not found
-     * @throws IndexOutOfBoundsException if the fromIndex argument
-     *  is negative or not less than the count of Throwables in the chain
-     * @since 2.1
-     */
-    public int indexOfThrowable(Class type, int fromIndex) {
-        return delegate.indexOfThrowable(type, fromIndex);
-    }
-
-    /**
-     * Prints the stack trace of this exception.
-     * Includes information from the exception, if any, which caused this exception.
-     * 
-     * @since 2.1
-     */
-    @Override
-    public void printStackTrace() {
-        delegate.printStackTrace();
-    }
-
-    /**
-     * Prints the stack trace of this exception to the specified stream.
-     * Includes information from the exception, if any, which caused this exception.
-     *
-     * @param out  the stream to write to
-     * @since 2.1
-     */
-    @Override
-    public void printStackTrace(PrintStream out) {
-        delegate.printStackTrace(out);
-    }
-
-    /**
-     * Prints the stack trace of this exception to the specified writer.
-     * Includes information from the exception, if any, which caused this exception.
-     *
-     * @param out  the writer to write to
-     * @since 2.1
-     */
-    @Override
-    public void printStackTrace(PrintWriter out) {
-        delegate.printStackTrace(out);
-    }
-
-    /**
-     * Prints the stack trace for this exception only (root cause not included)
-     * using the specified writer.
-     * 
-     * @param out  the writer to write to
-     * @since 2.1
-     */
-    public final void printPartialStackTrace(PrintWriter out) {
-        super.printStackTrace(out);
-    }
-
 }
diff --git a/src/java/org/apache/commons/lang/SerializationException.java b/src/java/org/apache/commons/lang/SerializationException.java
index b0052b5a5..70968b1b4 100644
--- a/src/java/org/apache/commons/lang/SerializationException.java
+++ b/src/java/org/apache/commons/lang/SerializationException.java
@@ -16,8 +16,6 @@
  */
 package org.apache.commons.lang;
 
-import org.apache.commons.lang.exception.NestableRuntimeException;
-
 /**
  * 

Exception thrown when the Serialization process fails.

* @@ -27,7 +25,7 @@ * @since 1.0 * @version $Id$ */ -public class SerializationException extends NestableRuntimeException { +public class SerializationException extends RuntimeException { /** * Required for serialization support. diff --git a/src/java/org/apache/commons/lang/StringEscapeUtils.java b/src/java/org/apache/commons/lang/StringEscapeUtils.java index 482711f04..148cd0166 100644 --- a/src/java/org/apache/commons/lang/StringEscapeUtils.java +++ b/src/java/org/apache/commons/lang/StringEscapeUtils.java @@ -21,8 +21,6 @@ import java.io.Writer; import java.util.Locale; -import org.apache.commons.lang.exception.NestableRuntimeException; - /** *

Escapes and unescapes Strings for * Java, Java Script, HTML, XML, and SQL.

@@ -331,7 +329,7 @@ public static void unescapeJava(Writer out, String str) throws IOException { inUnicode = false; hadSlash = false; } catch (NumberFormatException nfe) { - throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe); + throw new UnhandledException("Unable to parse unicode value: " + unicode, nfe); } } continue; diff --git a/src/java/org/apache/commons/lang/UnhandledException.java b/src/java/org/apache/commons/lang/UnhandledException.java index 2045499dc..0f47b1367 100644 --- a/src/java/org/apache/commons/lang/UnhandledException.java +++ b/src/java/org/apache/commons/lang/UnhandledException.java @@ -16,8 +16,6 @@ */ package org.apache.commons.lang; -import org.apache.commons.lang.exception.NestableRuntimeException; - /** *

Thrown when it is impossible or undesirable to consume or throw a checked exception.

* This exception supplements the standard exception classes by providing a more @@ -42,7 +40,7 @@ * @since 2.0 * @version $Id$ */ -public class UnhandledException extends NestableRuntimeException { +public class UnhandledException extends RuntimeException { /** * Required for serialization support. diff --git a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java index cb9deda01..2ffa7c93c 100644 --- a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java +++ b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java @@ -248,8 +248,7 @@ public static boolean isCauseMethodName(String methodName) { * *

The method searches for methods with specific names that return a * Throwable object. This will pick up most wrapping exceptions, - * including those from JDK 1.4, and - * {@link org.apache.commons.lang.exception.NestableException NestableException}. + * including those from JDK 1.4. * The method names can be added to using {@link #addCauseMethodName(String)}.

* *

The default list searched for are:

@@ -359,9 +358,7 @@ public static Throwable getRootCause(Throwable throwable) { * @return the wrapped exception, or null if not found */ private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) { - if (throwable instanceof Nestable) { - return ((Nestable) throwable).getCause(); - } else if (throwable instanceof SQLException) { + if (throwable instanceof SQLException) { return ((SQLException) throwable).getNextException(); } else if (throwable instanceof InvocationTargetException) { return ((InvocationTargetException) throwable).getTargetException(); @@ -457,9 +454,7 @@ public static boolean isNestedThrowable(Throwable throwable) { return false; } - if (throwable instanceof Nestable) { - return true; - } else if (throwable instanceof SQLException) { + if (throwable instanceof SQLException) { return true; } else if (throwable instanceof InvocationTargetException) { return true; @@ -920,10 +915,6 @@ public static String[] getStackFrames(Throwable throwable) { * *

The end of line is determined by the value of {@link SystemUtils#LINE_SEPARATOR}.

* - *

Functionality shared between the - * getStackFrames(Throwable) methods of this and the - * {@link org.apache.commons.lang.exception.NestableDelegate} classes.

- * * @param stackTrace a stack trace String * @return an array where each element is a line from the argument */ diff --git a/src/java/org/apache/commons/lang/exception/Nestable.java b/src/java/org/apache/commons/lang/exception/Nestable.java deleted file mode 100644 index da800e2c5..000000000 --- a/src/java/org/apache/commons/lang/exception/Nestable.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.PrintStream; -import java.io.PrintWriter; - -/** - * An interface to be implemented by {@link java.lang.Throwable} - * extensions which would like to be able to nest root exceptions - * inside themselves. - * - * @author Daniel L. Rall - * @author Kasper Nielsen - * @author Steven Caswell - * @author Pete Gieser - * @since 1.0 - * @version $Id$ - */ -public interface Nestable { - - /** - * Returns the reference to the exception or error that caused the - * exception implementing the Nestable to be thrown. - * - * @return throwable that caused the original exception - */ - public Throwable getCause(); - - /** - * Returns the error message of this and any nested - * Throwable. - * - * @return the error message - */ - public String getMessage(); - - /** - * Returns the error message of the Throwable in the chain - * of Throwables at the specified index, numbered from 0. - * - * @param index the index of the Throwable in the chain of - * Throwables - * @return the error message, or null if the Throwable at the - * specified index in the chain does not contain a message - * @throws IndexOutOfBoundsException if the index argument is - * negative or not less than the count of Throwables in the - * chain - */ - public String getMessage(int index); - - /** - * Returns the error message of this and any nested Throwables - * in an array of Strings, one element for each message. Any - * Throwable not containing a message is represented in the - * array by a null. This has the effect of cause the length of the returned - * array to be equal to the result of the {@link #getThrowableCount()} - * operation. - * - * @return the error messages - */ - public String[] getMessages(); - - /** - * Returns the Throwable in the chain of - * Throwables at the specified index, numbered from 0. - * - * @param index the index, numbered from 0, of the Throwable in - * the chain of Throwables - * @return the Throwable - * @throws IndexOutOfBoundsException if the index argument is - * negative or not less than the count of Throwables in the - * chain - */ - public Throwable getThrowable(int index); - - /** - * Returns the number of nested Throwables represented by - * this Nestable, including this Nestable. - * - * @return the throwable count - */ - public int getThrowableCount(); - - /** - * Returns this Nestable and any nested Throwables - * in an array of Throwables, one element for each - * Throwable. - * - * @return the Throwables - */ - public Throwable[] getThrowables(); - - /** - * Returns the index, numbered from 0, of the first occurrence of the - * specified type, or a subclass, in the chain of Throwables. - * The method returns -1 if the specified type is not found in the chain. - *

- * NOTE: From v2.1, we have clarified the Nestable interface - * such that this method matches subclasses. - * If you want to NOT match subclasses, please use - * {@link ExceptionUtils#indexOfThrowable(Throwable, Class)} - * (which is avaiable in all versions of lang). - * - * @param type the type to find, subclasses match, null returns -1 - * @return index of the first occurrence of the type in the chain, or -1 if - * the type is not found - */ - public int indexOfThrowable(Class type); - - /** - * Returns the index, numbered from 0, of the first Throwable - * that matches the specified type, or a subclass, in the chain of Throwables - * with an index greater than or equal to the specified index. - * The method returns -1 if the specified type is not found in the chain. - *

- * NOTE: From v2.1, we have clarified the Nestable interface - * such that this method matches subclasses. - * If you want to NOT match subclasses, please use - * {@link ExceptionUtils#indexOfThrowable(Throwable, Class, int)} - * (which is avaiable in all versions of lang). - * - * @param type the type to find, subclasses match, null returns -1 - * @param fromIndex the index, numbered from 0, of the starting position in - * the chain to be searched - * @return index of the first occurrence of the type in the chain, or -1 if - * the type is not found - * @throws IndexOutOfBoundsException if the fromIndex argument - * is negative or not less than the count of Throwables in the - * chain - */ - public int indexOfThrowable(Class type, int fromIndex); - - /** - * Prints the stack trace of this exception to the specified print - * writer. Includes information from the exception, if any, - * which caused this exception. - * - * @param out PrintWriter to use for output. - */ - public void printStackTrace(PrintWriter out); - - /** - * Prints the stack trace of this exception to the specified print - * stream. Includes information from the exception, if any, - * which caused this exception. - * - * @param out PrintStream to use for output. - */ - public void printStackTrace(PrintStream out); - - /** - * Prints the stack trace for this exception only--root cause not - * included--using the provided writer. Used by - * {@link org.apache.commons.lang.exception.NestableDelegate} to write - * individual stack traces to a buffer. The implementation of - * this method should call - * super.printStackTrace(out); in most cases. - * - * @param out The writer to use. - */ - public void printPartialStackTrace(PrintWriter out); - -} diff --git a/src/java/org/apache/commons/lang/exception/NestableDelegate.java b/src/java/org/apache/commons/lang/exception/NestableDelegate.java deleted file mode 100644 index ec3cce81e..000000000 --- a/src/java/org/apache/commons/lang/exception/NestableDelegate.java +++ /dev/null @@ -1,404 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.PrintStream; -import java.io.PrintWriter; -import java.io.Serializable; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -/** - *

A shared implementation of the nestable exception functionality.

- *

- * The code is shared between - * {@link org.apache.commons.lang.exception.NestableError NestableError}, - * {@link org.apache.commons.lang.exception.NestableException NestableException} and - * {@link org.apache.commons.lang.exception.NestableRuntimeException NestableRuntimeException}. - *

- * - * @author Rafal Krzewski - * @author Daniel L. Rall - * @author Kasper Nielsen - * @author Steven Caswell - * @author Sean C. Sullivan - * @author Stephen Colebourne - * @since 1.0 - * @version $Id$ - */ -public class NestableDelegate implements Serializable { - - /** - * Required for serialization support. - * - * @see java.io.Serializable - */ - private static final long serialVersionUID = 1L; - - /** - * Constructor error message. - */ - private transient static final String MUST_BE_THROWABLE = - "The Nestable implementation passed to the NestableDelegate(Nestable) " - + "constructor must extend java.lang.Throwable"; - - /** - * Holds the reference to the exception or error that we're - * wrapping (which must be a {@link - * org.apache.commons.lang.exception.Nestable} implementation). - */ - private final Throwable nestable; - - /** - * Whether to print the stack trace top-down. - * This public flag may be set by calling code, typically in initialisation. - * This exists for backwards compatability, setting it to false will return - * the library to v1.0 behaviour (but will affect all users of the library - * in the classloader). - * @since 2.0 - */ - public static boolean topDown = true; - - /** - * Whether to trim the repeated stack trace. - * This public flag may be set by calling code, typically in initialisation. - * This exists for backwards compatability, setting it to false will return - * the library to v1.0 behaviour (but will affect all users of the library - * in the classloader). - * @since 2.0 - */ - public static boolean trimStackFrames = true; - - /** - * Whether to match subclasses via indexOf. - * This public flag may be set by calling code, typically in initialisation. - * This exists for backwards compatability, setting it to false will return - * the library to v2.0 behaviour (but will affect all users of the library - * in the classloader). - * @since 2.1 - */ - public static boolean matchSubclasses = true; - - /** - * Constructs a new NestableDelegate instance to manage the - * specified Nestable. - * - * @param nestable the Nestable implementation (must extend - * {@link java.lang.Throwable}) - * @since 2.0 - */ - public NestableDelegate(Nestable nestable) { - if (nestable instanceof Throwable) { - this.nestable = (Throwable) nestable; - } else { - throw new IllegalArgumentException(MUST_BE_THROWABLE); - } - } - - /** - * Returns the error message of the Throwable in the chain of Throwables at the - * specified index, numbered from 0. - * - * @param index - * the index of the Throwable in the chain of Throwables - * @return the error message, or null if the Throwable at the specified index in the chain does not - * contain a message - * @throws IndexOutOfBoundsException - * if the index argument is negative or not less than the count of Throwables - * in the chain - * @since 2.0 - */ - public String getMessage(int index) { - Throwable t = this.getThrowable(index); - if (Nestable.class.isInstance(t)) { - return ((Nestable) t).getMessage(0); - } - return t.getMessage(); - } - - /** - * Returns the full message contained by the Nestable and any nested Throwables. - * - * @param baseMsg - * the base message to use when creating the full message. Should be generally be called via - * nestableHelper.getMessage(super.getMessage()), where super is an - * instance of {@link java.lang.Throwable}. - * @return The concatenated message for this and all nested Throwables - * @since 2.0 - */ - public String getMessage(String baseMsg) { - Throwable nestedCause = ExceptionUtils.getCause(this.nestable); - String causeMsg = nestedCause == null ? null : nestedCause.getMessage(); - if (nestedCause == null || causeMsg == null) { - return baseMsg; // may be null, which is a valid result - } - if (baseMsg == null) { - return causeMsg; - } - return baseMsg + ": " + causeMsg; - } - - /** - * Returns the error message of this and any nested Throwables in an array of Strings, one element - * for each message. Any Throwable not containing a message is represented in the array by a null. - * This has the effect of cause the length of the returned array to be equal to the result of the - * {@link #getThrowableCount()} operation. - * - * @return the error messages - * @since 2.0 - */ - public String[] getMessages() { - Throwable[] throwables = this.getThrowables(); - String[] msgs = new String[throwables.length]; - for (int i = 0; i < throwables.length; i++) { - msgs[i] = - (Nestable.class.isInstance(throwables[i]) - ? ((Nestable) throwables[i]).getMessage(0) - : throwables[i].getMessage()); - } - return msgs; - } - - /** - * Returns the Throwable in the chain of - * Throwables at the specified index, numbered from 0. - * - * @param index the index, numbered from 0, of the Throwable in - * the chain of Throwables - * @return the Throwable - * @throws IndexOutOfBoundsException if the index argument is - * negative or not less than the count of Throwables in the - * chain - * @since 2.0 - */ - public Throwable getThrowable(int index) { - if (index == 0) { - return this.nestable; - } - Throwable[] throwables = this.getThrowables(); - return throwables[index]; - } - - /** - * Returns the number of Throwables contained in the - * Nestable contained by this delegate. - * - * @return the throwable count - * @since 2.0 - */ - public int getThrowableCount() { - return ExceptionUtils.getThrowableCount(this.nestable); - } - - /** - * Returns this delegate's Nestable and any nested - * Throwables in an array of Throwables, one - * element for each Throwable. - * - * @return the Throwables - * @since 2.0 - */ - public Throwable[] getThrowables() { - return ExceptionUtils.getThrowables(this.nestable); - } - - /** - * Returns the index, numbered from 0, of the first Throwable - * that matches the specified type, or a subclass, in the chain of Throwables - * with an index greater than or equal to the specified index. - * The method returns -1 if the specified type is not found in the chain. - *

- * NOTE: From v2.1, we have clarified the Nestable interface - * such that this method matches subclasses. - * If you want to NOT match subclasses, please use - * {@link ExceptionUtils#indexOfThrowable(Throwable, Class, int)} - * (which is avaiable in all versions of lang). - * An alternative is to use the public static flag {@link #matchSubclasses} - * on NestableDelegate, however this is not recommended. - * - * @param type the type to find, subclasses match, null returns -1 - * @param fromIndex the index, numbered from 0, of the starting position in - * the chain to be searched - * @return index of the first occurrence of the type in the chain, or -1 if - * the type is not found - * @throws IndexOutOfBoundsException if the fromIndex argument - * is negative or not less than the count of Throwables in the - * chain - * @since 2.0 - */ - public int indexOfThrowable(Class type, int fromIndex) { - if (type == null) { - return -1; - } - if (fromIndex < 0) { - throw new IndexOutOfBoundsException("The start index was out of bounds: " + fromIndex); - } - Throwable[] throwables = ExceptionUtils.getThrowables(this.nestable); - if (fromIndex >= throwables.length) { - throw new IndexOutOfBoundsException("The start index was out of bounds: " - + fromIndex + " >= " + throwables.length); - } - if (matchSubclasses) { - for (int i = fromIndex; i < throwables.length; i++) { - if (type.isAssignableFrom(throwables[i].getClass())) { - return i; - } - } - } else { - for (int i = fromIndex; i < throwables.length; i++) { - if (type.equals(throwables[i].getClass())) { - return i; - } - } - } - return -1; - } - - /** - * Prints the stack trace of this exception the the standar error - * stream. - */ - public void printStackTrace() { - printStackTrace(System.err); - } - - /** - * Prints the stack trace of this exception to the specified - * stream. - * - * @param out PrintStream to use for output. - * @see #printStackTrace(PrintWriter) - */ - public void printStackTrace(PrintStream out) { - synchronized (out) { - PrintWriter pw = new PrintWriter(out, false); - printStackTrace(pw); - // Flush the PrintWriter before it's GC'ed. - pw.flush(); - } - } - - /** - * Prints the stack trace of this exception to the specified - * writer. If the Throwable class has a getCause - * method (i.e. running on jre1.4 or higher), this method just - * uses Throwable's printStackTrace() method. Otherwise, generates - * the stack-trace, by taking into account the 'topDown' and - * 'trimStackFrames' parameters. The topDown and trimStackFrames - * are set to 'true' by default (produces jre1.4-like stack trace). - * - * @param out PrintWriter to use for output. - */ - public void printStackTrace(PrintWriter out) { - Throwable throwable = this.nestable; - // if running on jre1.4 or higher, use default printStackTrace - if (ExceptionUtils.isThrowableNested()) { - if (throwable instanceof Nestable) { - ((Nestable)throwable).printPartialStackTrace(out); - } else { - throwable.printStackTrace(out); - } - return; - } - - // generating the nested stack trace - List stacks = new ArrayList(); - while (throwable != null) { - String[] st = getStackFrames(throwable); - stacks.add(st); - throwable = ExceptionUtils.getCause(throwable); - } - - // If NOT topDown, reverse the stack - String separatorLine = "Caused by: "; - if (!topDown) { - separatorLine = "Rethrown as: "; - Collections.reverse(stacks); - } - - // Remove the repeated lines in the stack - if (trimStackFrames) { - trimStackFrames(stacks); - } - - synchronized (out) { - for (Iterator iter=stacks.iterator(); iter.hasNext();) { - String[] st = (String[]) iter.next(); - for (int i=0, len=st.length; i < len; i++) { - out.println(st[i]); - } - if (iter.hasNext()) { - out.print(separatorLine); - } - } - } - } - - /** - * Captures the stack trace associated with the specified - * Throwable object, decomposing it into a list of - * stack frames. - * - * @param t The Throwable. - * @return An array of strings describing each stack frame. - * @since 2.0 - */ - protected String[] getStackFrames(Throwable t) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw, true); - - // Avoid infinite loop between decompose() and printStackTrace(). - if (t instanceof Nestable) { - ((Nestable) t).printPartialStackTrace(pw); - } else { - t.printStackTrace(pw); - } - return ExceptionUtils.getStackFrames(sw.getBuffer().toString()); - } - - /** - * Trims the stack frames. The first set is left untouched. The rest - * of the frames are truncated from the bottom by comparing with - * one just on top. - * - * @param stacks The list containing String[] elements - * @since 2.0 - */ - protected void trimStackFrames(List stacks) { - for (int size=stacks.size(), i=size-1; i > 0; i--) { - String[] curr = (String[]) stacks.get(i); - String[] next = (String[]) stacks.get(i-1); - - List currList = new ArrayList(Arrays.asList(curr)); - List nextList = new ArrayList(Arrays.asList(next)); - ExceptionUtils.removeCommonFrames(currList, nextList); - - int trimmed = curr.length - currList.size(); - if (trimmed > 0) { - currList.add("\t... "+trimmed+" more"); - stacks.set( - i, - currList.toArray(new String[currList.size()]) - ); - } - } - } -} diff --git a/src/java/org/apache/commons/lang/exception/NestableError.java b/src/java/org/apache/commons/lang/exception/NestableError.java deleted file mode 100644 index 0f826e7fa..000000000 --- a/src/java/org/apache/commons/lang/exception/NestableError.java +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.PrintStream; -import java.io.PrintWriter; - -/** - * The base class of all errors which can contain other exceptions. - * - * @author Daniel L. Rall - * @see org.apache.commons.lang.exception.NestableException - * @since 1.0 - * @version $Id$ - */ -public class NestableError extends Error implements Nestable { - - /** - * Required for serialization support. - * - * @see java.io.Serializable - */ - private static final long serialVersionUID = 1L; - - /** - * The helper instance which contains much of the code which we - * delegate to. - */ - // TODO make final - protected NestableDelegate delegate = new NestableDelegate(this); - - /** - * Holds the reference to the exception or error that caused - * this exception to be thrown. - */ - private final Throwable cause; - - /** - * Constructs a new NestableError without specified - * detail message. - */ - public NestableError() { - super(); - this.cause = null; - } - - /** - * Constructs a new NestableError with specified - * detail message. - * - * @param msg The error message. - */ - public NestableError(String msg) { - super(msg); - this.cause = null; - } - - /** - * Constructs a new NestableError with specified - * nested Throwable. - * - * @param cause the exception or error that caused this exception to be - * thrown - */ - public NestableError(Throwable cause) { - super(); - this.cause = cause; - } - - /** - * Constructs a new NestableError with specified - * detail message and nested Throwable. - * - * @param msg the error message - * @param cause the exception or error that caused this exception to be - * thrown - */ - public NestableError(String msg, Throwable cause) { - super(msg); - this.cause = cause; - } - - /** - * {@inheritDoc} - */ - @Override - public Throwable getCause() { - return cause; - } - - /** - * Returns the detail message string of this throwable. If it was - * created with a null message, returns the following: - * (cause==null ? null : cause.toString()). - * - * @return String message string of the throwable - */ - @Override - public String getMessage() { - if (super.getMessage() != null) { - return super.getMessage(); - } else if (cause != null) { - return cause.toString(); - } else { - return null; - } - } - - /** - * {@inheritDoc} - */ - public String getMessage(int index) { - if (index == 0) { - return super.getMessage(); - } - return delegate.getMessage(index); - } - - /** - * {@inheritDoc} - */ - public String[] getMessages() { - return delegate.getMessages(); - } - - /** - * {@inheritDoc} - */ - public Throwable getThrowable(int index) { - return delegate.getThrowable(index); - } - - /** - * {@inheritDoc} - */ - public int getThrowableCount() { - return delegate.getThrowableCount(); - } - - /** - * {@inheritDoc} - */ - public Throwable[] getThrowables() { - return delegate.getThrowables(); - } - - /** - * {@inheritDoc} - */ - public int indexOfThrowable(Class type) { - return delegate.indexOfThrowable(type, 0); - } - - /** - * {@inheritDoc} - */ - public int indexOfThrowable(Class type, int fromIndex) { - return delegate.indexOfThrowable(type, fromIndex); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace() { - delegate.printStackTrace(); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace(PrintStream out) { - delegate.printStackTrace(out); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace(PrintWriter out) { - delegate.printStackTrace(out); - } - - /** - * {@inheritDoc} - */ - public final void printPartialStackTrace(PrintWriter out) { - super.printStackTrace(out); - } -} diff --git a/src/java/org/apache/commons/lang/exception/NestableException.java b/src/java/org/apache/commons/lang/exception/NestableException.java deleted file mode 100644 index e4680717c..000000000 --- a/src/java/org/apache/commons/lang/exception/NestableException.java +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.PrintStream; -import java.io.PrintWriter; - -/** - * The base class of all exceptions which can contain other exceptions. - * - * It is intended to ease the debugging by carrying on the information - * about the exception which was caught and provoked throwing the - * current exception. Catching and rethrowing may occur multiple - * times, and provided that all exceptions except the first one - * are descendants of NestedException, when the - * exception is finally printed out using any of the - * printStackTrace() methods, the stack trace will contain - * the information about all exceptions thrown and caught on - * the way. - *

Running the following program - *

- *  1 import org.apache.commons.lang.exception.NestableException;
- *  2
- *  3 public class Test {
- *  4     public static void main( String[] args ) {
- *  5         try {
- *  6             a();
- *  7         } catch(Exception e) {
- *  8             e.printStackTrace();
- *  9         }
- * 10      }
- * 11
- * 12      public static void a() throws Exception {
- * 13          try {
- * 14              b();
- * 15          } catch(Exception e) {
- * 16              throw new NestableException("foo", e);
- * 17          }
- * 18      }
- * 19
- * 20      public static void b() throws Exception {
- * 21          try {
- * 22              c();
- * 23          } catch(Exception e) {
- * 24              throw new NestableException("bar", e);
- * 25          }
- * 26      }
- * 27
- * 28      public static void c() throws Exception {
- * 29          throw new Exception("baz");
- * 30      }
- * 31 }
- * 
- *

Yields the following stack trace: - *

- * org.apache.commons.lang.exception.NestableException: foo
- *         at Test.a(Test.java:16)
- *         at Test.main(Test.java:6)
- * Caused by: org.apache.commons.lang.exception.NestableException: bar
- *         at Test.b(Test.java:24)
- *         at Test.a(Test.java:14)
- *         ... 1 more
- * Caused by: java.lang.Exception: baz
- *         at Test.c(Test.java:29)
- *         at Test.b(Test.java:22)
- *         ... 2 more
- * 

- * - * @author Rafal Krzewski - * @author Daniel L. Rall - * @author Kasper Nielsen - * @author Steven Caswell - * @since 1.0 - * @version $Id$ - */ -public class NestableException extends Exception implements Nestable { - - /** - * Required for serialization support. - * - * @see java.io.Serializable - */ - private static final long serialVersionUID = 1L; - - /** - * The helper instance which contains much of the code which we - * delegate to. - */ - // TODO make final - protected NestableDelegate delegate = new NestableDelegate(this); - - /** - * Holds the reference to the exception or error that caused - * this exception to be thrown. - */ - private final Throwable cause; - - /** - * Constructs a new NestableException without specified - * detail message. - */ - public NestableException() { - super(); - this.cause = null; - } - - /** - * Constructs a new NestableException with specified - * detail message. - * - * @param msg The error message. - */ - public NestableException(String msg) { - super(msg); - this.cause = null; - } - - /** - * Constructs a new NestableException with specified - * nested Throwable. - * - * @param cause the exception or error that caused this exception to be - * thrown - */ - public NestableException(Throwable cause) { - super(); - this.cause = cause; - } - - /** - * Constructs a new NestableException with specified - * detail message and nested Throwable. - * - * @param msg the error message - * @param cause the exception or error that caused this exception to be - * thrown - */ - public NestableException(String msg, Throwable cause) { - super(msg); - this.cause = cause; - } - - /** - * {@inheritDoc} - */ - @Override - public Throwable getCause() { - return cause; - } - - /** - * Returns the detail message string of this throwable. If it was - * created with a null message, returns the following: - * (cause==null ? null : cause.toString()). - * - * @return String message string of the throwable - */ - @Override - public String getMessage() { - if (super.getMessage() != null) { - return super.getMessage(); - } else if (cause != null) { - return cause.toString(); - } else { - return null; - } - } - - /** - * {@inheritDoc} - */ - public String getMessage(int index) { - if (index == 0) { - return super.getMessage(); - } - return delegate.getMessage(index); - } - - /** - * {@inheritDoc} - */ - public String[] getMessages() { - return delegate.getMessages(); - } - - /** - * {@inheritDoc} - */ - public Throwable getThrowable(int index) { - return delegate.getThrowable(index); - } - - /** - * {@inheritDoc} - */ - public int getThrowableCount() { - return delegate.getThrowableCount(); - } - - /** - * {@inheritDoc} - */ - public Throwable[] getThrowables() { - return delegate.getThrowables(); - } - - /** - * {@inheritDoc} - */ - public int indexOfThrowable(Class type) { - return delegate.indexOfThrowable(type, 0); - } - - /** - * {@inheritDoc} - */ - public int indexOfThrowable(Class type, int fromIndex) { - return delegate.indexOfThrowable(type, fromIndex); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace() { - delegate.printStackTrace(); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace(PrintStream out) { - delegate.printStackTrace(out); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace(PrintWriter out) { - delegate.printStackTrace(out); - } - - /** - * {@inheritDoc} - */ - public final void printPartialStackTrace(PrintWriter out) { - super.printStackTrace(out); - } - -} diff --git a/src/java/org/apache/commons/lang/exception/NestableRuntimeException.java b/src/java/org/apache/commons/lang/exception/NestableRuntimeException.java deleted file mode 100644 index f4dc4fd4b..000000000 --- a/src/java/org/apache/commons/lang/exception/NestableRuntimeException.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.PrintStream; -import java.io.PrintWriter; - -/** - * The base class of all runtime exceptions which can contain other - * exceptions. - * - * @see org.apache.commons.lang.exception.NestableException - * @author Rafal Krzewski - * @author Daniel L. Rall - * @author Kasper Nielsen - * @author Steven Caswell - * @since 1.0 - * @version $Id$ - */ -public class NestableRuntimeException extends RuntimeException implements Nestable { - - /** - * Required for serialization support. - * - * @see java.io.Serializable - */ - private static final long serialVersionUID = 1L; - - /** - * The helper instance which contains much of the code which we - * delegate to. - */ - // TODO make final - protected NestableDelegate delegate = new NestableDelegate(this); - - /** - * Holds the reference to the exception or error that caused - * this exception to be thrown. - */ - private final Throwable cause; - - /** - * Constructs a new NestableRuntimeException without specified - * detail message. - */ - public NestableRuntimeException() { - super(); - this.cause = null; - } - - /** - * Constructs a new NestableRuntimeException with specified - * detail message. - * - * @param msg the error message - */ - public NestableRuntimeException(String msg) { - super(msg); - this.cause = null; - } - - /** - * Constructs a new NestableRuntimeException with specified - * nested Throwable. - * - * @param cause the exception or error that caused this exception to be - * thrown - */ - public NestableRuntimeException(Throwable cause) { - super(); - this.cause = cause; - } - - /** - * Constructs a new NestableRuntimeException with specified - * detail message and nested Throwable. - * - * @param msg the error message - * @param cause the exception or error that caused this exception to be - * thrown - */ - public NestableRuntimeException(String msg, Throwable cause) { - super(msg); - this.cause = cause; - } - - /** - * {@inheritDoc} - */ - @Override - public Throwable getCause() { - return cause; - } - - /** - * Returns the detail message string of this throwable. If it was - * created with a null message, returns the following: - * (cause==null ? null : cause.toString()). - * - * @return String message string of the throwable - */ - @Override - public String getMessage() { - if (super.getMessage() != null) { - return super.getMessage(); - } else if (cause != null) { - return cause.toString(); - } else { - return null; - } - } - - /** - * {@inheritDoc} - */ - public String getMessage(int index) { - if (index == 0) { - return super.getMessage(); - } - return delegate.getMessage(index); - } - - /** - * {@inheritDoc} - */ - public String[] getMessages() { - return delegate.getMessages(); - } - - /** - * {@inheritDoc} - */ - public Throwable getThrowable(int index) { - return delegate.getThrowable(index); - } - - /** - * {@inheritDoc} - */ - public int getThrowableCount() { - return delegate.getThrowableCount(); - } - - /** - * {@inheritDoc} - */ - public Throwable[] getThrowables() { - return delegate.getThrowables(); - } - - /** - * {@inheritDoc} - */ - public int indexOfThrowable(Class type) { - return delegate.indexOfThrowable(type, 0); - } - - /** - * {@inheritDoc} - */ - public int indexOfThrowable(Class type, int fromIndex) { - return delegate.indexOfThrowable(type, fromIndex); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace() { - delegate.printStackTrace(); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace(PrintStream out) { - delegate.printStackTrace(out); - } - - /** - * {@inheritDoc} - */ - @Override - public void printStackTrace(PrintWriter out) { - delegate.printStackTrace(out); - } - - /** - * {@inheritDoc} - */ - public final void printPartialStackTrace(PrintWriter out) { - super.printStackTrace(out); - } - -} diff --git a/src/test/org/apache/commons/lang/NotImplementedExceptionTest.java b/src/test/org/apache/commons/lang/NotImplementedExceptionTest.java index 34f435184..8e3171d21 100644 --- a/src/test/org/apache/commons/lang/NotImplementedExceptionTest.java +++ b/src/test/org/apache/commons/lang/NotImplementedExceptionTest.java @@ -27,8 +27,6 @@ import junit.framework.TestSuite; import junit.textui.TestRunner; -import org.apache.commons.lang.exception.NestableException; - /** * JUnit tests. * @@ -112,59 +110,8 @@ public void testConstructor_Class2() { assertEquals("Code is not implemented in class java.lang.String", ex.getMessage()); } - public void testGetMessage_Indexed() throws Exception { - if (SystemUtils.isJavaVersionAtLeast(1.4f)) { - Exception ex1 = new Exception("nested 2"); - Constructor con = Exception.class.getConstructor(new Class[] {String.class, Throwable.class}); - Exception ex2 = (Exception) con.newInstance(new Object[] {"nested 1", ex1}); - NotImplementedException ex = new NotImplementedException(ex2); - assertEquals("Code is not implemented", ex.getMessage()); - assertEquals("Code is not implemented", ex.getMessage(0)); - assertEquals("nested 1", ex.getMessage(1)); - assertEquals("nested 2", ex.getMessage(2)); - - String[] messages = ex.getMessages(); - assertEquals(3, messages.length); - assertEquals("Code is not implemented", messages[0]); - assertEquals("nested 1", messages[1]); - assertEquals("nested 2", messages[2]); - } - } - - public void testGetThrowable() { - NotImplementedException ex = new NotImplementedException(new NestableException("nested 1", new NestableException("nested 2"))); - - assertEquals(3, ex.getThrowableCount()); - - assertEquals(NotImplementedException.class, ex.getThrowable(0).getClass()); - assertEquals("Code is not implemented", ex.getThrowable(0).getMessage()); - assertEquals(NestableException.class, ex.getThrowable(1).getClass()); - assertEquals("nested 1", ex.getThrowable(1).getMessage()); - assertEquals(NestableException.class, ex.getThrowable(2).getClass()); - assertEquals("nested 2", ex.getThrowable(2).getMessage()); - - assertEquals(3, ex.getThrowables().length); - assertEquals(NotImplementedException.class, ex.getThrowables()[0].getClass()); - assertEquals("Code is not implemented", ex.getThrowables()[0].getMessage()); - assertEquals(NestableException.class, ex.getThrowables()[1].getClass()); - assertEquals("nested 1", ex.getThrowables()[1].getMessage()); - assertEquals(NestableException.class, ex.getThrowables()[2].getClass()); - assertEquals("nested 2", ex.getThrowables()[2].getMessage()); - } - - public void testIndexOfThrowable() { - NotImplementedException ex = new NotImplementedException(new NestableException("nested 1", new NestableException("nested 2"))); - assertEquals(0, ex.indexOfThrowable(NotImplementedException.class)); - assertEquals(1, ex.indexOfThrowable(NestableException.class)); - } - - public void testIndexOfThrowable_Index() { - NotImplementedException ex = new NotImplementedException(new NestableException("nested 1", new NestableException("nested 2"))); - assertEquals(1, ex.indexOfThrowable(NestableException.class, 1)); - } - public void testPrintStackTrace() { - NotImplementedException ex = new NotImplementedException(new NestableException("nested 1", new NestableException("nested 2"))); + NotImplementedException ex = new NotImplementedException(new Exception("nested 1", new RuntimeException("nested 2"))); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); PrintStream errStream = System.err; @@ -175,7 +122,7 @@ public void testPrintStackTrace() { } public void testPrintStackTrace_Stream() { - NotImplementedException ex = new NotImplementedException(new NestableException("nested 1", new NestableException("nested 2"))); + NotImplementedException ex = new NotImplementedException(new Exception("nested 1", new RuntimeException("nested 2"))); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); ex.printStackTrace(ps); @@ -183,18 +130,11 @@ public void testPrintStackTrace_Stream() { } public void testPrintStackTrace_Writer() { - NotImplementedException ex = new NotImplementedException(new NestableException("nested 1", new NestableException("nested 2"))); + NotImplementedException ex = new NotImplementedException(new Exception("nested 1", new RuntimeException("nested 2"))); StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); ex.printStackTrace(writer); assertTrue(stringWriter.toString().length() > 0); } - public void testPrintPartialStackTrace_Writer() { - NotImplementedException ex = new NotImplementedException(new NestableException("nested 1", new NestableException("nested 2"))); - StringWriter stringWriter = new StringWriter(); - PrintWriter writer = new PrintWriter(stringWriter); - ex.printPartialStackTrace(writer); - assertTrue(stringWriter.toString().length() > 0); - } } diff --git a/src/test/org/apache/commons/lang/UnhandledExceptionTest.java b/src/test/org/apache/commons/lang/UnhandledExceptionTest.java index f6b681880..ee3ab22fe 100644 --- a/src/test/org/apache/commons/lang/UnhandledExceptionTest.java +++ b/src/test/org/apache/commons/lang/UnhandledExceptionTest.java @@ -21,8 +21,6 @@ import junit.framework.TestSuite; import junit.textui.TestRunner; -import org.apache.commons.lang.exception.Nestable; - /** * JUnit tests. * @@ -59,14 +57,14 @@ public void testConstructor_stringAndThrowable_nullInput() { public void testGetCause() { final Throwable t = new NullPointerException(); - final Nestable n = new UnhandledException(t); + final Throwable n = new UnhandledException(t); assertEquals(t, n.getCause()); } public void testGetCauseAndGetMessage() { final Throwable t = new NullPointerException(); final String msg = "nullArg"; - final Nestable n = new UnhandledException(msg, t); + final Throwable n = new UnhandledException(msg, t); assertEquals(t, n.getCause()); assertEquals(msg, n.getMessage()); } diff --git a/src/test/org/apache/commons/lang/exception/AbstractNestableTest.java b/src/test/org/apache/commons/lang/exception/AbstractNestableTest.java deleted file mode 100644 index 25de08664..000000000 --- a/src/test/org/apache/commons/lang/exception/AbstractNestableTest.java +++ /dev/null @@ -1,685 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.io.PrintWriter; - -import junit.framework.TestCase; -/** - * Tests implementations of the org.apache.commons.lang.exception.Nestable - * interface. - * - * @author Steven Caswell - * @version $Id$ - */ -public abstract class AbstractNestableTest extends TestCase -{ - - /** - * Constructs an instance of - * AbstractNestableTest. - * - * @param name the test name - */ - public AbstractNestableTest(String name) - { - super(name); - } - - /** - * Tests the getCause() operation. - */ - public void testGetCause() - { - Nestable ne1 = getNestable(); - assertNull("nestable exception() cause is null", ne1.getCause()); - - Nestable ne2 = getNestable("ne2"); - assertNull("nestable exception(\"ne2\") cause is null", ne2.getCause()); - - Nestable ne3 = getNestable(getThrowable("ne3 exception")); - assertNotNull("nestable exception(Throwable(\"ne3 exception\") cause is not null", - ne3.getCause()); - assertTrue("nestable exception(Throwable(\"ne3 exception\") cause message == ne3 exception", - ne3.getCause().getMessage().equals("ne3 exception")); - - Nestable ne4 = getNestable("ne4", getThrowable("ne4 exception")); - assertNotNull("nestable exception(\"ne4\", Throwable(\"ne4 exception\") cause is not null", - ne4.getCause()); - - Nestable ne5 = getNestable("ne5", (Throwable) null); - assertNull("nestable exception(\"ne5\", null) cause is null", - ne5.getCause()); - - Nestable ne6 = getNestable(null, getThrowable("ne6 exception")); - assertNotNull("nestable exception(null, Throwable(\"ne6 exception\") cause is not null", - ne6.getCause()); - } - - /** - * Tests the getThrowableCount() operation. - */ - public void testGetThrowableCount() - { - Nestable ne1 = getNestable(); - assertEquals("ne1 throwable count", 1, ne1.getThrowableCount()); - - Nestable ne2 = getNestable("ne2"); - assertEquals("ne2 throwable count", 1, ne2.getThrowableCount()); - - Nestable ne3 = getNestable(getThrowable("ne3 exception")); - assertEquals("ne3 throwable count", 2, ne3.getThrowableCount()); - - Nestable ne4 = getNestable("ne4", getThrowable("ne4 exception")); - assertEquals("ne4 throwable count", 2, ne4.getThrowableCount()); - - Nestable ne5 = getNestable("ne5", (Throwable) null); - assertEquals("ne 5 throwable count", 1, ne5.getThrowableCount()); - - Nestable ne6 = getNestable(null, getThrowable("ne6 exception")); - assertEquals("ne 6 throwable count", 2, ne6.getThrowableCount()); - - Nestable ne7 = getNestable("ne7o", getNestable("ne7i", getThrowable("ne7 exception"))); - assertEquals("ne 7 throwable count", 3, ne7.getThrowableCount()); - - Nestable ne8 = getNestable("level 1", getNestable("level 2", getNestable(getNestable("level 4", getThrowable("level 5"))))); - assertEquals("ne 8 throwable count", 5, ne8.getThrowableCount()); - } - - /** - * Tests the getMessage() operation. - */ - public void testGetMessage() - { - Nestable ne1 = getNestable(); - assertNull("nestable exception() message is null", ne1.getMessage()); - - Nestable ne2 = getNestable("ne2"); - assertNotNull("nestable exception(\"ne2\") message is not null", ne2.getMessage()); - assertEquals("nestable exception(\"ne2\") message == ne2", ne2.getMessage(), "ne2"); - - Nestable ne3 = getNestable(getThrowable("ne3 exception")); - assertNotNull("nestable exception(Throwable(\"ne3 exception\") message is not null", - ne3.getMessage()); - assertEquals("nestable exception(Throwable(\"ne3 exception\") message equals cause.toString()", - ne3.getMessage(), ne3.getCause().toString()); - - Nestable ne4 = getNestable("ne4", getThrowable("ne4 exception")); - assertNotNull("nestable exception(\"ne4\", Throwable(\"ne4 exception\") message is not null", - ne4.getMessage()); - assertEquals("nestable exception(\"ne4\", Throwable(\"ne4 exception\") message == ne4", - ne4.getMessage(), "ne4"); - - Nestable ne5 = getNestable("ne5", (Throwable) null); - assertNotNull("nestable exception(\"ne5\", null) message is not null", - ne5.getMessage()); - assertEquals("nestable exception(\"ne5\", null) message == ne5", - ne5.getMessage(), "ne5"); - - Throwable t6 = getThrowable("ne6 exception"); - Nestable ne6 = getNestable(null, t6); - assertNotNull("nestable exception(null, Throwable(\"ne6 exception\") message is not null", - ne6.getMessage()); - assertEquals("nestable exception(null, Throwable(\"ne6 exception\") message equals cause.toString()", - ne6.getMessage(), ne6.getCause().toString()); - - Nestable ne7 = getNestable("ne7o", getNestable("ne7i", getThrowable("ne7 exception"))); - assertEquals("nestable exception(\"ne7o\", getNestable(\"ne7i\", Throwable(\"ne7 exception\"))) message is ne7o: ne7i: ne7 exception", - ne7.getMessage(), "ne7o"); - - Nestable ne8 = getNestable(); - assertNull("nestable exception() message is null", - ne8.getMessage()); - - } - - /** - * Tests the getMessage(int) operation. - */ - public void testGetMessageI() - { - String[] msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - Nestable ne = getNestable(msgs[0], getNestable(msgs[1], getNestable(getNestable(msgs[3], getThrowable(msgs[4]))))); - for(int i = 0; i < msgs.length; i++) - { - assertEquals("message " + i, msgs[i], ne.getMessage(i)); - } - - // Test for index out of bounds - try - { - @SuppressWarnings("unused") - String msg = ne.getMessage(-1); - fail("getMessage(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - try - { - @SuppressWarnings("unused") - String msg = ne.getMessage(msgs.length + 100); - fail("getMessage(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - } - - /** - * Tests the getMessages() operation. - */ - public void testGetMessages() - { - String[] msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - Nestable ne = getNestable(msgs[0], getNestable(msgs[1], getNestable(getNestable(msgs[3], getThrowable(msgs[4]))))); - String[] nMsgs = ne.getMessages(); - assertEquals("messages length", msgs.length, nMsgs.length); - for(int i = 0; i < nMsgs.length; i++) - { - assertEquals("message " + i, msgs[i], nMsgs[i]); - } - } - - /** - * Tests the getThrowable(int) operation. - */ - public void testGetThrowableI() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = getTester1Class(); - throwables[1] = getThrowableClass(); - n = getTester1(getThrowable(msgs[1])); - doNestableExceptionGetThrowableI(n, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = getTester1Class(); - throwables[1] = getTester2Class(); - throwables[2] = getTester1Class(); - throwables[3] = getTester2Class(); - throwables[4] = getThrowableClass(); - n = getTester1(msgs[0], getTester2(msgs[1], getTester1(getTester2(msgs[3], getThrowable(msgs[4]))))); - doNestableExceptionGetThrowableI(n, throwables, msgs); - } - - private void doNestableExceptionGetThrowableI(Nestable n, Class[] classes, String[] msgs) - { - Throwable t = null; - String msg = null; - - for(int i = 0; i < classes.length; i++) - { - t = n.getThrowable(i); - assertEquals("throwable class", classes[i], t.getClass()); - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - - // Test for index out of bounds - try - { - t = n.getThrowable(-1); - fail("getThrowable(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - try - { - t = n.getThrowable(999); - fail("getThrowable(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - } - - /** - * Tests the getThrowables() operation. - */ - public void testGetThrowables() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = getTester1Class(); - throwables[1] = getThrowableClass(); - n = getTester1(getThrowable(msgs[1])); - doNestableExceptionGetThrowables(n, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = getTester1Class(); - throwables[1] = getTester2Class(); - throwables[2] = getTester1Class(); - throwables[3] = getTester2Class(); - throwables[4] = getThrowableClass(); - n = getTester1(msgs[0], getTester2(msgs[1], getTester1(getTester2(msgs[3], getThrowable(msgs[4]))))); - doNestableExceptionGetThrowables(n, throwables, msgs); - } - - private void doNestableExceptionGetThrowables(Nestable n, Class[] classes, String[] msgs) - { - String msg = null; - - Throwable throwables[] = n.getThrowables(); - assertEquals("throwables length", classes.length, throwables.length); - for(int i = 0; i < classes.length; i++) - { - assertEquals("throwable class", classes[i], throwables[i].getClass()); - Throwable t = throwables[i]; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - } - - /** - * Tests the indexOfThrowable() operation. - */ - public void testIndexOfThrowable() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = getTester1Class(); - throwables[1] = getTester2Class(); - throwables[2] = getTester1Class(); - throwables[3] = getTester2Class(); - throwables[4] = getThrowableClass(); - int[] indexes = {0, 1, 0, 1, 4}; - n = getTester1(msgs[0], getTester2(msgs[1], getTester1(getTester2(msgs[3], getThrowable(msgs[4]))))); - for(int i = 0; i < throwables.length; i++) - { - doNestableExceptionIndexOfThrowable(n, throwables[i], indexes[i], msgs[indexes[i]]); - } - doNestableExceptionIndexOfThrowable(n, getBaseThrowableClass(), 0, msgs[0]); - doNestableExceptionIndexOfThrowable(n, java.util.Date.class, -1, null); - doNestableExceptionIndexOfThrowable(n, null, -1, null); - } - - private void doNestableExceptionIndexOfThrowable(Nestable n, Class type, int expectedIndex, String expectedMsg) - { - Throwable t = null; - - int index = n.indexOfThrowable(type); - assertEquals("index of throwable " + (type == null ? "null" : type.getName()), expectedIndex, index); - if(expectedIndex > -1) - { - t = n.getThrowable(index); - if(expectedMsg != null) - { - String msg = null; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("message of indexed throwable", expectedMsg, msg); - } - } - } - - /** - * Tests the indexOfThrowable(int) operation. - */ - public void testIndexOfThrowableI() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = getTester1Class(); - throwables[1] = getTester2Class(); - throwables[2] = getTester1Class(); - throwables[3] = getTester2Class(); - throwables[4] = getThrowableClass(); - int[] indexes = {0, 1, 0, 1, 4}; - n = getTester1(msgs[0], getTester2(msgs[1], getTester1(getTester2(msgs[3], getThrowable(msgs[4]))))); - for(int i = 0; i < throwables.length; i++) - { - doNestableExceptionIndexOfThrowableI(n, throwables[i], 0, indexes[i], msgs[indexes[i]]); - } - doNestableExceptionIndexOfThrowableI(n, getTester2Class(), 2, 3, msgs[3]); - doNestableExceptionIndexOfThrowableI(n, getTester1Class(), 1, 2, msgs[2]); - doNestableExceptionIndexOfThrowableI(n, getTester1Class(), 3, -1, null); - doNestableExceptionIndexOfThrowableI(n, getTester1Class(), 4, -1, null); - doNestableExceptionIndexOfThrowableI(n, getThrowableClass(), 2, 4, msgs[4]); - doNestableExceptionIndexOfThrowableI(n, java.util.Date.class, 0, -1, null); - doNestableExceptionIndexOfThrowableI(n, null, 0, -1, null); - - // Test for index out of bounds - try - { - @SuppressWarnings("unused") - int index = n.indexOfThrowable(getTester1Class(), -1); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - try - { - @SuppressWarnings("unused") - int index = n.indexOfThrowable(getTester1Class(), 5); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - - } - - private void doNestableExceptionIndexOfThrowableI(Nestable n, Class type, int fromIndex, int expectedIndex, String expectedMsg) - { - Throwable t = null; - - int index = n.indexOfThrowable(type, fromIndex); - assertEquals("index of throwable " + (type == null ? "null" : type.getName()), expectedIndex, index); - if(expectedIndex > -1) - { - t = n.getThrowable(index); - if(expectedMsg != null) - { - String msg = null; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("message of indexed throwable", expectedMsg, msg); - } - } - - } - - /** - * Tests the printPartialStackTrace() operation. - */ - public void testPrintPartialStackTrace() - { - Nestable ne9 = getNestable("ne9", getThrowable("ne9 exception")); - ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); - PrintStream ps2 = new PrintStream(baos2); - PrintWriter pw2 = new PrintWriter(ps2, true); - ne9.printPartialStackTrace(pw2); - String stack2 = baos2.toString(); - String startsWith = ne9.getClass().getName() + ": ne9"; - assertTrue("stack trace startsWith == " + startsWith, - stack2.startsWith(startsWith)); - assertEquals("stack trace indexOf rethrown == -1", - stack2.indexOf("rethrown"), -1); - } - - /** - * Tests the printStackTrace() operation. - */ - public void testPrintStackTrace() - { - Nestable ne8 = getNestable("ne8", getThrowable("ne8 exception")); - ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); - PrintStream ps1 = new PrintStream(baos1); - PrintWriter pw1 = new PrintWriter(ps1, true); - ne8.printStackTrace(pw1); - String stack1 = baos1.toString(); - String startsWith = ne8.getClass().getName() + ": ne8"; - assertTrue("stack trace startsWith == " + startsWith, - stack1.startsWith(startsWith)); - String indexOf = getThrowableClass().getName() + ": ne8 exception"; - assertTrue("stack trace indexOf " + indexOf + " > -1", - stack1.indexOf(indexOf) > -1); - } - - /** - * Returns an instance of the Nestable implementation being - * tested. - * - * @return the instance - */ - public abstract Nestable getNestable(); - - /** - * Returns an instance of the Nestable implementation being - * tested. - * - * @param n Nestable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getNestable(Nestable n); - - /** - * Returns an instance of the Nestable implementation being - * tested. - * - * @param msg String argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getNestable(String msg); - - /** - * Returns an instance of the Nestable implementation being - * tested. - * - * @param msg String argument to be provided to the instance - * constructor - * @param n Nestable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getNestable(String msg, Nestable n); - - /** - * Returns an instance of the Nestable implementation being - * tested. - * - * @param msg String argument to be provided to the instance - * constructor - * @param t Throwable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getNestable(String msg, Throwable t); - - /** - * Returns an instance of the Nestable implementation being - * tested. - * - * @param t Throwable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getNestable(Throwable t); - - /** - * Returns an instance of a Throwable to be used in - * constructing instances of the Nestable implementation being - * tested. - * - * @param msg String argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Throwable getThrowable(String msg); - - /** - * Returns an instance of one tester Nestable implementation. - * - * @param n Nestable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getTester1(Nestable n); - - /** - * Returns an instance of one tester Nestable implementation. - * - * @param t Throwable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getTester1(Throwable t); - - /** - * Returns an instance of one tester Nestable implementation. - * - * @param msg String argument to be provided to the instance - * constructor - * @param n Nestable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getTester1(String msg, Nestable n); - - /** - * Returns an instance of one tester Nestable implementation. - * - * @param msg String argument to be provided to the instance - * constructor - * @param t Throwable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getTester1(String msg, Throwable t); - - /** - * Returns an instance of a second tester Nestable - * implementation. - * - * @param msg String argument to be provided to the instance - * constructor - * @param n Nestable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getTester2(String msg, Nestable n); - - /** - * Returns an instance of a second tester Nestable - * implementation. - * - * @param msg String argument to be provided to the instance - * constructor - * @param t Throwable argument to be provided to the instance - * constructor - * @return the instance - */ - public abstract Nestable getTester2(String msg, Throwable t); - - /** - * Returns the class of the first tester Nestable - * implementation. - * - * @return the class - */ - public abstract Class getTester1Class(); - - /** - * Returns the class of the second tester Nestable - * implementation. - * - * @return the class - */ - public abstract Class getTester2Class(); - - /** - * Returns the class of the Throwable used in constructing - * instances of the Nestable implementation being tested. - * - * @return the class - */ - public abstract Class getThrowableClass(); - - /** - * Returns the base class being used, typically Error, Eception or RuntimeException. - * - * @return the class - */ - public abstract Class getBaseThrowableClass(); - -} - diff --git a/src/test/org/apache/commons/lang/exception/ExceptionTestSuite.java b/src/test/org/apache/commons/lang/exception/ExceptionTestSuite.java index 790593ebb..96d023018 100644 --- a/src/test/org/apache/commons/lang/exception/ExceptionTestSuite.java +++ b/src/test/org/apache/commons/lang/exception/ExceptionTestSuite.java @@ -47,10 +47,6 @@ public static void main(String[] args) public static Test suite() { TestSuite suite = new TestSuite(); - suite.addTest(NestableDelegateTest.suite()); - suite.addTest(NestableErrorTest.suite()); - suite.addTest(NestableExceptionTest.suite()); - suite.addTest(NestableRuntimeExceptionTest.suite()); suite.addTest(ExceptionUtilsTest.suite()); return suite; } diff --git a/src/test/org/apache/commons/lang/exception/ExceptionUtilsTest.java b/src/test/org/apache/commons/lang/exception/ExceptionUtilsTest.java index 690272feb..f2ceb175d 100644 --- a/src/test/org/apache/commons/lang/exception/ExceptionUtilsTest.java +++ b/src/test/org/apache/commons/lang/exception/ExceptionUtilsTest.java @@ -173,11 +173,6 @@ public void testGetCause_ThrowableArray() { assertSame(null, ExceptionUtils.getCause(null, null)); assertSame(null, ExceptionUtils.getCause(null, new String[0])); - // match because known type - assertSame(withoutCause, ExceptionUtils.getCause(nested, null)); - assertSame(withoutCause, ExceptionUtils.getCause(nested, new String[0])); - assertSame(withoutCause, ExceptionUtils.getCause(nested, new String[] {"getCause"})); - // not known type, so match on supplied method names assertSame(nested, ExceptionUtils.getCause(withCause, null)); // default names assertSame(null, ExceptionUtils.getCause(withCause, new String[0])); @@ -601,4 +596,15 @@ public void getTargetException() { } } + // Temporary classes to allow the nested exception code to be removed + // prior to a rewrite of this test class. + private static class NestableRuntimeException extends RuntimeException { + public NestableRuntimeException() { super(); } + public NestableRuntimeException(Throwable t) { super(t); } + } + private static class NestableException extends Exception { + public NestableException() { super(); } + public NestableException(Throwable t) { super(t); } + } + } diff --git a/src/test/org/apache/commons/lang/exception/NestableDelegateTest.java b/src/test/org/apache/commons/lang/exception/NestableDelegateTest.java deleted file mode 100644 index 150b23254..000000000 --- a/src/test/org/apache/commons/lang/exception/NestableDelegateTest.java +++ /dev/null @@ -1,1196 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.ByteArrayOutputStream; -import java.io.EOFException; -import java.io.IOException; -import java.io.PrintStream; -import java.io.PrintWriter; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; -import junit.textui.TestRunner; - -/** - * Tests the org.apache.commons.lang.exception.NestableDelegate class. - * - * @author Steven Caswell - * @author Daniel L. Rall - * @version $Id$ - */ -public class NestableDelegateTest extends TestCase { - private static final String CONSTRUCTOR_FAILED_MSG = - "The Nestable implementation passed to the NestableDelegate(Nestable) constructor must extend java.lang.Throwable"; - - private static final String PARTIAL_STACK_TRACE = - "ThrowableNestedNestable partial stack trace place-holder"; - - protected String lineSeparator; - - /** - * Construct a new instance of NestableDelegateTest with the specified name - */ - public NestableDelegateTest(String name) - { - super(name); - } - - /** - * Set up instance variables required by this test case. - */ - @Override - public void setUp() - { - lineSeparator = System.getProperty("line.separator"); - } - - public static Test suite() - { - return new TestSuite(NestableDelegateTest.class); - } - - /** - * Tear down instance variables required by this test case. - */ - @Override - public void tearDown() - { - lineSeparator = null; - } - - /** - * Test the implementation - */ - public void testNestableDelegateConstructor() - { - String msg = null; - boolean constructorFailed = false; - try - { - NestableDelegate nonThrowableCause = new NestableDelegate(new NonThrowableNestable()); - } - catch(IllegalArgumentException iae) - { - constructorFailed = true; - msg = iae.getMessage(); - } - assertTrue("nestable delegate constructor with non-throwable cause failed == true", constructorFailed); - assertTrue("constructor failed exception msg == " + CONSTRUCTOR_FAILED_MSG, - msg.equals(CONSTRUCTOR_FAILED_MSG)); - - constructorFailed = false; - try - { - NestableDelegate nd1 = new NestableDelegate(new ThrowableNestable()); - } - catch(IllegalArgumentException iae) - { - constructorFailed = true; - } - assertTrue("nestable delegate constructor with throwable cause failed == false", !constructorFailed); - } - - public void testNestableDelegateGetMessage() - { - Nestable ne1 = new ThrowableNestable(); - assertTrue("ThrowableNestable ne1 getMessage() == ThrowableNestable exception", - ne1.getMessage().equals("ThrowableNestable exception")); - NestableDelegate nd1 = new NestableDelegate(ne1); - assertTrue("nd1 getMessage() == " + ne1.getCause().getMessage(), - nd1.getMessage("base").equals("base: " + ne1.getCause().getMessage())); - - Nestable ne2 = new ThrowableNestedNestable(new Exception("nested exception 2")); - NestableDelegate nd2 = new NestableDelegate(ne2); - assertTrue("nd2 getMessage() == base: " + ne2.getCause().getMessage(), - nd2.getMessage("base").equals("base: " + ne2.getCause().getMessage())); - } - - public void testNestableDelegateGetThrowableCount() - { - Nestable n = null; - NestableDelegate d = null; - - n = new NestableDelegateTester1(); - d = new NestableDelegate(n); - doNestableDelegateGetThrowableCount(d, 1); - - n = new NestableDelegateTester1("level 1"); - d = new NestableDelegate(n); - doNestableDelegateGetThrowableCount(d, 1); - - n = new NestableDelegateTester1(new Exception()); - d = new NestableDelegate(n); - doNestableDelegateGetThrowableCount(d, 2); - - n = new NestableDelegateTester1(new Exception("level 2")); - d = new NestableDelegate(n); - doNestableDelegateGetThrowableCount(d, 2); - - n = new NestableDelegateTester1("level 1", - new NestableDelegateTester2("level 2", - new NestableDelegateTester1( - new NestableDelegateTester2("level 4", - new Exception("level 5") - ) - ) - ) - ); - d = new NestableDelegate(n); - doNestableDelegateGetThrowableCount(d, 5); - } - - private void doNestableDelegateGetThrowableCount(NestableDelegate d, int len) - { - // Compare the lengths - assertEquals("delegate length", len, d.getThrowableCount()); - } - - public void testNestableDelegateGetMessages() - { - Nestable n = null; - NestableDelegate d = null; - String msgs[] = null; - - msgs = new String[1]; - n = new NestableDelegateTester1(); - d = new NestableDelegate(n); - doNestableDelegateGetMessages(d, msgs); - - msgs = new String[1]; - msgs[0] = "level 1"; - n = new NestableDelegateTester1(msgs[0]); - d = new NestableDelegate(n); - doNestableDelegateGetMessages(d, msgs); - - msgs = new String[2]; - n = new NestableDelegateTester1(new Exception()); - d = new NestableDelegate(n); - doNestableDelegateGetMessages(d, msgs); - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - n = new NestableDelegateTester1(new Exception(msgs[1])); - d = new NestableDelegate(n); - doNestableDelegateGetMessages(d, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - n = new NestableDelegateTester1(msgs[0], - new NestableDelegateTester2(msgs[1], - new NestableDelegateTester1( - new NestableDelegateTester2(msgs[3], - new Exception(msgs[4]) - ) - ) - ) - ); - d = new NestableDelegate(n); - doNestableDelegateGetMessages(d, msgs); - } - - private void doNestableDelegateGetMessages(NestableDelegate d, String[] nMsgs) - { - // Compare the messages - String[] dMsgs = d.getMessages(); - assertEquals("messages length", nMsgs.length, dMsgs.length); - for(int i = 0; i < nMsgs.length; i++) - { - assertEquals("message " + i, nMsgs[i], dMsgs[i]); - } - } - - public void testGetMessageString() - { - NestableDelegateTester1 ndt1 = new NestableDelegateTester1 (new NullPointerException ()); - NestableDelegate nd = new NestableDelegate (ndt1); - assertNull (nd.getMessage((String)null)); - - ndt1 = new NestableDelegateTester1 (new NullPointerException ("null pointer")); - nd = new NestableDelegate (ndt1); - assertNotNull(nd.getMessage((String)null)); - - ndt1 = new NestableDelegateTester1 (); - nd = new NestableDelegate (ndt1); - assertNull(nd.getMessage((String)null)); - - ndt1 = new NestableDelegateTester1 ("root"); - nd = new NestableDelegate (ndt1); - assertNull(nd.getMessage((String)null)); - } - - public void testNestableDelegateGetMessageN() - { - Nestable n = null; - NestableDelegate d = null; - String[] msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - n = new NestableDelegateTester1(msgs[0], - new NestableDelegateTester2(msgs[1], - new NestableDelegateTester1( - new NestableDelegateTester2(msgs[3], - new Exception(msgs[4]) - ) - ) - ) - ); - d = new NestableDelegate(n); - for(int i = 0; i < msgs.length; i++) - { - assertEquals("message " + i, msgs[i], d.getMessage(i)); - } - - // Test for index out of bounds - try - { - String msg = d.getMessage(-1); - fail("getMessage(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - try - { - String msg = d.getMessage(msgs.length + 100); - fail("getMessage(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - } - - public void testNestableDelegateGetThrowableN() - { - Nestable n = null; - NestableDelegate d = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = NestableDelegateTester1.class; - throwables[1] = Exception.class; - n = new NestableDelegateTester1(new Exception(msgs[1])); - d = new NestableDelegate(n); - doNestableDelegateGetThrowableN(d, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableDelegateTester1.class; - throwables[1] = NestableDelegateTester2.class; - throwables[2] = NestableDelegateTester1.class; - throwables[3] = NestableDelegateTester2.class; - throwables[4] = Exception.class; - n = new NestableDelegateTester1(msgs[0], - new NestableDelegateTester2(msgs[1], - new NestableDelegateTester1( - new NestableDelegateTester2(msgs[3], - new Exception(msgs[4]) - ) - ) - ) - ); - d = new NestableDelegate(n); - doNestableDelegateGetThrowableN(d, throwables, msgs); - } - - private void doNestableDelegateGetThrowableN(NestableDelegate d, Class[] classes, String[] msgs) - { - Throwable t = null; - String msg = null; - - for(int i = 0; i < classes.length; i++) - { - t = d.getThrowable(i); - assertEquals("throwable class", classes[i], t.getClass()); - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - - // Test for index out of bounds - try - { - t = d.getThrowable(-1); - fail("getThrowable(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - try - { - t = d.getThrowable(999); - fail("getThrowable(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - } - - public void testNestableDelegateGetThrowables() - { - Nestable n = null; - NestableDelegate d = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = NestableDelegateTester1.class; - throwables[1] = Exception.class; - n = new NestableDelegateTester1(new Exception(msgs[1])); - d = new NestableDelegate(n); - doNestableDelegateGetThrowables(d, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableDelegateTester1.class; - throwables[1] = NestableDelegateTester2.class; - throwables[2] = NestableDelegateTester1.class; - throwables[3] = NestableDelegateTester2.class; - throwables[4] = Exception.class; - n = new NestableDelegateTester1(msgs[0], - new NestableDelegateTester2(msgs[1], - new NestableDelegateTester1( - new NestableDelegateTester2(msgs[3], - new Exception(msgs[4]) - ) - ) - ) - ); - d = new NestableDelegate(n); - doNestableDelegateGetThrowables(d, throwables, msgs); - } - - private void doNestableDelegateGetThrowables(NestableDelegate d, Class[] classes, String[] msgs) - { - Throwable[] throwables = null; - String msg = null; - - throwables = d.getThrowables(); - assertEquals("throwables length", classes.length, throwables.length); - for(int i = 0; i < classes.length; i++) - { - assertEquals("throwable class", classes[i], throwables[i].getClass()); - Throwable t = throwables[i]; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - } - - public void testIndexOfThrowable() - { - Nestable n = null; - NestableDelegate d = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableDelegateTester1.class; - throwables[1] = NestableDelegateTester2.class; - throwables[2] = NestableDelegateTester1.class; - throwables[3] = NestableDelegateTester2.class; - throwables[4] = EOFException.class; - int[] indexes = {0, 1, 0, 1, 4}; - n = new NestableDelegateTester1(msgs[0], - new NestableDelegateTester2(msgs[1], - new NestableDelegateTester1( - new NestableDelegateTester2(msgs[3], - new EOFException(msgs[4]) - ) - ) - ) - ); - d = new NestableDelegate(n); - for(int i = 0; i < throwables.length; i++) - { - doNestableDelegateIndexOfThrowable(d, throwables[i], 0, indexes[i], msgs[indexes[i]]); - } - doNestableDelegateIndexOfThrowable(d, NestableDelegateTester2.class, 2, 3, msgs[3]); - doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 1, 2, msgs[2]); - doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 3, -1, null); - doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 4, -1, null); - doNestableDelegateIndexOfThrowable(d, EOFException.class, 2, 4, msgs[4]); - doNestableDelegateIndexOfThrowable(d, IOException.class, 2, 4, msgs[4]); - doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 2, msgs[2]); - doNestableDelegateIndexOfThrowable(d, Exception.class, 0, 0, msgs[0]); - doNestableDelegateIndexOfThrowable(d, java.util.Date.class, 0, -1, null); - doNestableDelegateIndexOfThrowable(d, null, 0, -1, null); - - // Test for index out of bounds - try - { - int index = d.indexOfThrowable(NestableDelegateTester1.class, -1); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - try - { - int index = d.indexOfThrowable(NestableDelegateTester1.class, 5); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - } - - private void doNestableDelegateIndexOfThrowable(NestableDelegate d, Class type, int fromIndex, int expectedIndex, String expectedMsg) - { - Throwable t = null; - - int index = d.indexOfThrowable(type, fromIndex); - assertEquals("index of throwable " + (type == null ? "null" : type.getName()), expectedIndex, index); - if(expectedIndex > -1) - { - t = d.getThrowable(index); - if(expectedMsg != null) - { - String msg = null; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("message of indexed throwable", expectedMsg, msg); - } - } - } - - public void testNestableDelegetePrintStackTrace() - { - int lineSepLen = lineSeparator.length(); - int partialStackTraceLen = PARTIAL_STACK_TRACE.length(); - Nestable ne3 = new ThrowableNestedNestable(new Exception("nested exception 3")); - NestableDelegate nd3 = new NestableDelegate(ne3); - - ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); - PrintStream ps1 = new PrintStream(baos1); - nd3.printStackTrace(ps1); - String stack1 = baos1.toString(); - assertTrue("stack trace startsWith", stack1.startsWith(PARTIAL_STACK_TRACE)); - - Nestable n = new NestableDelegateTester1("level 1", - new NestableDelegateTester2("level 2", - new NestableDelegateTester1( - new NestableDelegateTester2("level 4", - new Exception("level 5") - ) - ) - ) - ); - NestableDelegate d = new NestableDelegate(n); - - // Only testing the flags for jdk1.3 and below - if (!ExceptionUtils.isThrowableNested()) { - NestableDelegate.topDown = true; NestableDelegate.trimStackFrames = true; - checkStackTrace(d, true, true, NestableDelegateTester1.class.getName()+": level 1", 24); - NestableDelegate.topDown = true; NestableDelegate.trimStackFrames = false; - checkStackTrace(d, true, false, NestableDelegateTester1.class.getName()+": level 1", 80); - NestableDelegate.topDown = false; NestableDelegate.trimStackFrames = true; - checkStackTrace(d, false, true, "java.lang.Exception: level 5", 24); - NestableDelegate.topDown = false; NestableDelegate.trimStackFrames = false; - checkStackTrace(d, false, false, "java.lang.Exception: level 5", 80); - NestableDelegate.topDown = true; NestableDelegate.trimStackFrames = true; - } - } - private void checkStackTrace(NestableDelegate d, boolean topDown, boolean trimStackFrames, - String startsWith, int expCount) { - ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); - PrintStream ps1 = new PrintStream(baos1); - d.printStackTrace(ps1); - String stack1 = baos1.toString(); - int actCount = countLines(stack1); - assertTrue("topDown: "+topDown+", trimStackFrames: "+trimStackFrames+" startsWith", - stack1.startsWith(startsWith)); - // test is unreliable, as count varies depending on JUnit version/where main method is -// assertEquals("topDown: "+topDown+", trimStackFrames: "+trimStackFrames+" lineCount", -// expCount, actCount); - } - private int countLines(String s) { - if (s == null) return 0; - - int i = 0, ndx = -1; - while ((ndx = s.indexOf("\n", ndx+1)) != -1) { - i++; - } - return i; - } - - public static void main(String args[]) - { - TestRunner.run(suite()); - } -} - -/** - * Nestable and Throwable class which can be passed to the NestableDelegate - * constructor. Used for testing various methods which iterate through the - * nested causes. - */ -class NestableDelegateTester1 extends Exception implements Nestable -{ - private Throwable cause = null; - - public NestableDelegateTester1() - { - super(); - } - - public NestableDelegateTester1(String reason, Throwable cause) - { - super(reason); - this.cause = cause; - } - - public NestableDelegateTester1(String reason) - { - super(reason); - } - - public NestableDelegateTester1(Throwable cause) - { - super(); - this.cause = cause; - } - - /** - * @see Nestable#getThrowables() - * Returns zero-length Throwable array for this test. - */ - public Throwable[] getThrowables() - { - return new Throwable[0]; - } - - /** - * @see Nestable#getMessages() - * Returns zero-length String array for this test. - */ - public String[] getMessages() - { - return new String[0]; - } - - /** - * @see Nestable#indexOfThrowable(Class) - * Returns -1 for this test. - */ - public int indexOfThrowable(Class type) - { - return -1; - } - - /** - * @see Nestable#getThrowable(int) - * Returns null for this test. - */ - public Throwable getThrowable(int index) - { - return null; - } - - /** - * @see Nestable#getThrowableCount() - * Returns 1 for this test. - */ - public int getThrowableCount() - { - return 1; - } - - /** - * @see Nestable#getCause() - */ - @Override - public Throwable getCause() - { - return cause; - } - - /** - * Empty method to satisfy the implemented interface. Does nothing - * in this test. - * - * @param out The writer to use. - */ - public void printPartialStackTrace(PrintWriter out) - { - super.printStackTrace(out); - } - - /** - * @see Nestable#getMessage(int) - */ - public String getMessage(int index) - { - if(index == 0) - { - return super.getMessage(); - } - else - { - return ""; - } - } - - /** - * @see Nestable#indexOfThrowable(Class, int) - * Returns -1 for this test. - */ - public int indexOfThrowable(Class type, int fromIndex) - { - return -1; - } - -} - -/** - * Nestable and Throwable class which can be passed to the NestableDelegate - * constructor. Used for testing various methods which iterate through the - * nested causes. - */ -class NestableDelegateTester2 extends Throwable implements Nestable -{ - private Throwable cause = null; - - public NestableDelegateTester2() - { - super(); - } - - public NestableDelegateTester2(String reason, Throwable cause) - { - super(reason); - this.cause = cause; - } - - public NestableDelegateTester2(String reason) - { - super(reason); - } - - public NestableDelegateTester2(Throwable cause) - { - super(); - this.cause = cause; - } - - /** - * @see Nestable#getThrowables() - * Returns zero-length Throwable array for this test. - */ - public Throwable[] getThrowables() - { - return new Throwable[0]; - } - - /** - * @see Nestable#getMessages() - * Returns zero-length String array for this test. - */ - public String[] getMessages() - { - return new String[0]; - } - - /** - * @see Nestable#indexOfThrowable(Class) - * Returns -1 for this test. - */ - public int indexOfThrowable(Class type) - { - return -1; - } - - /** - * @see Nestable#getThrowable(int) - * Returns null for this test. - */ - public Throwable getThrowable(int index) - { - return null; - } - - /** - * @see Nestable#getThrowableCount() - * Returns 1 for this test. - * - * @return 1 - */ - public int getThrowableCount() - { - return 1; - } - - /** - * @see Nestable#getCause() - */ - @Override - public Throwable getCause() - { - return cause; - } - - /** - * Empty method to satisfy the implemented interface. Does nothing - * in this test. - * - * @param out The writer to use. - */ - public void printPartialStackTrace(PrintWriter out) - { - super.printStackTrace(out); - } - - /** - * @see Nestable#getMessage(int) - */ - public String getMessage(int index) - { - if(index == 0) - { - return super.getMessage(); - } - else - { - return ""; - } - } - - /** - * @see Nestable#indexOfThrowable(Class, int) - * Returns -1 for this test. - */ - public int indexOfThrowable(Class type, int fromIndex) - { - return -1; - } - -} - -/** - * Used to test that the constructor passes when passed a throwable cause - * And, the NestableDelegate.getMessage() returns the message from underlying - * nestable (which also has to be a Throwable). - */ -class ThrowableNestable extends Throwable implements Nestable -{ - private Throwable cause = new Exception("ThrowableNestable cause"); - - /** - * @see Nestable#getThrowableCount() - * Returns 1 for this test. - */ - public int getThrowableCount() - { - return 1; - } - - /** - * @see Nestable#getMessage() - * Returns the hard-coded string "ThrowableNestable exception" for this - * test. - */ - @Override - public String getMessage() - { - return "ThrowableNestable exception"; - } - - /** - * @see Nestable#getMessage(int) - * Returns the hard-coded string "ThrowableNestable exception" for this - * test. - */ - public String getMessage(int index) - { - return getMessage(); - } - - /** - * @see Nestable#getMessages() - * Returns single-element string array with "ThrowableNestable exception". - */ - public String[] getMessages() - { - String msgs[] = new String[1]; - msgs[0] = getMessage(); - return msgs; - } - - /** - * @see Nestable#getCause() - */ - @Override - public Throwable getCause() - { - return cause; - } - - /** - * @see Nestable#printStackTrace(PrintWriter) - * Empty method to satisfy the implemented interface. Does nothing - * in this test. - */ - @Override - public void printStackTrace(PrintWriter out) - { - } - - /** - * @see Nestable#printPartialStackTrace(PrintWriter) - * Empty method to satisfy the implemented interface. Does nothing - * in this test. - */ - public void printPartialStackTrace(PrintWriter out) - { - } - - /** - * @see Nestable#getThrowable(int) - */ - public Throwable getThrowable(int index) - { - return cause; - } - - /** - * @see Nestable#getThrowables() - */ - public Throwable[] getThrowables() - { - Throwable throwables[] = new Throwable[1]; - throwables[0] = cause; - return throwables; - } - - /** - * @see Nestable#indexOfThrowable(Class) - */ - public int indexOfThrowable(Class type) - { - if(Exception.class.isInstance(type)) - { - return 0; - } - return -1; - } - - /** - * @see Nestable#indexOfThrowable(Class,int) - */ - public int indexOfThrowable(Class type, int fromIndex) - { - return indexOfThrowable(type); - } - -} - -/** - * Nestable and Throwable class which takes in a 'cause' object. - * Returns a message wrapping the 'cause' message - * Prints a fixed stack trace and partial stack trace. - */ -class ThrowableNestedNestable extends Throwable implements Nestable -{ - private Throwable cause = null; - - public ThrowableNestedNestable(Throwable cause) - { - this.cause = cause; - } - - /** - * @see Nestable#getThrowableCount() - * Returns 1 for this test. - */ - public int getThrowableCount() - { - return 1; - } - - /** - * @see Nestable#getMessage() - * For this test, returns "ThrowableNestable exception (" appended to the - * message of the cause specified in the constructor. - */ - @Override - public String getMessage() - { - return "ThrowableNestedNestable exception (" + cause.getMessage() + ")"; - } - - /** - * @see Nestable#getMessage(int) - * For this test, returns "ThrowableNestable exception (" appended to the - * message of the cause specified in the constructor. - */ - public String getMessage(int index) - { - return "ThrowableNestedNestable exception (" + cause.getMessage() + ")"; - } - - /** - * @see Nestable#getMessages() - * For this test, returns a single-element string array containing - * "ThrowableNestable exception (" appended to the - * message of the cause specified in the constructor. - */ - public String[] getMessages() - { - String[] msgs = new String[1]; - msgs[0] = "ThrowableNestedNestable exception (" + cause.getMessage() + ")"; - return msgs; - } - - /** - * @see Nestable#getCause() - */ - @Override - public Throwable getCause() - { - return cause; - } - - /** - * @see Nestable#printStackTrace(PrintWriter) - * For this test, writes the string - * "ThrowableNestedNestable stack trace place-holder" to the print writer. - */ - @Override - public void printStackTrace(PrintWriter out) - { - out.println("ThrowableNestedNestable stack trace place-holder"); - } - - /** - * @see Nestable#printPartialStackTrace(PrintWriter) - * For this test, writes the string - * "ThrowableNestedNestable partial stack trace place-holder" to the print - * writer. - */ - public void printPartialStackTrace(PrintWriter out) - { - out.println("ThrowableNestedNestable partial stack trace place-holder"); - } - - /** - * @see Nestable#getThrowable(int) - */ - public Throwable getThrowable(int index) - { - return cause; - } - - /** - * @see Nestable#getThrowables() - */ - public Throwable[] getThrowables() - { - Throwable throwables[] = new Throwable[1]; - throwables[0] = cause; - return throwables; - } - - /** - * @see Nestable#indexOfThrowable(Class) - */ - public int indexOfThrowable(Class type) - { - if(Exception.class.isInstance(type)) - { - return 0; - } - return -1; - } - - /** - * @see Nestable#indexOfThrowable(Class, int) - */ - public int indexOfThrowable(Class type, int fromIndex) - { - return indexOfThrowable(type); - } - -} - -/** - * Used to test that the constructor fails when passed a non-throwable cause - */ -class NonThrowableNestable implements Nestable -{ - /** - * @see Nestable#getThrowableCount() - * Returns 1 for this test. - */ - public int getThrowableCount() - { - return 1; - } - - /** - * @see Nestable#getMessage() - * Returns the string "non-throwable" for this test. - */ - public String getMessage() - { - return "non-throwable"; - } - - /** - * @see Nestable#getMessage(int) - * Returns the string "non-throwable" for this test. - */ - public String getMessage(int index) - { - return "non-throwable"; - } - - /** - * @see Nestable#getMessage() - * Returns a single-element array containing the string "non-throwable" for - * this test. - */ - public String[] getMessages() - { - String[] msgs = new String[1]; - msgs[0] = "non-throwable"; - return msgs; - } - - /** - * @see Nestable#getCause() - * Returns null for this test. - */ - public Throwable getCause() - { - return null; - } - - /** - * @see Nestable#printStackTrace(PrintWriter) - * Empty method to satisfy the implemented interface. Does nothing - * in this test. - */ - public void printStackTrace(PrintWriter out) - { - } - - /** - * @see Nestable#printStackTrace(PrintStream) - * Empty method to satisfy the implemented interface. Does nothing - * in this test. - */ - public void printStackTrace(PrintStream out) - { - } - - /** - * @see Nestable#printPartialStackTrace(PrintWriter) - * Empty method to satisfy the implemented interface. Does nothing - * in this test. - */ - public void printPartialStackTrace(PrintWriter out) - { - } - - - /** - * @see Nestable#getThrowable(int) - * Returns null for this test. - */ - public Throwable getThrowable(int index) - { - return null; - } - - /** - * @see Nestable#getThrowables() - * Returns zero-length Throwable array. - */ - public Throwable[] getThrowables() - { - return new Throwable[0]; - } - - /** - * @see Nestable#indexOfThrowable(Class) - * Returns -1 for this test. - */ - public int indexOfThrowable(Class type) - { - return -1; - } - - /** - * @see Nestable#indexOfThrowable(Class, int) - * Returns -1 for this test. - */ - public int indexOfThrowable(Class type, int fromIndex) - { - return -1; - } - -} diff --git a/src/test/org/apache/commons/lang/exception/NestableErrorTest.java b/src/test/org/apache/commons/lang/exception/NestableErrorTest.java deleted file mode 100644 index c85d3c1d2..000000000 --- a/src/test/org/apache/commons/lang/exception/NestableErrorTest.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.EOFException; - -import junit.framework.Test; -import junit.framework.TestSuite; -import junit.textui.TestRunner; - -/** - * Tests the org.apache.commons.lang.exception.NestableError class. - * - * @author Steven Caswell - * @version $Id$ - */ -public class NestableErrorTest extends AbstractNestableTest { - - /** - * Construct a new instance of - * NestableErrorTest. - * - * @param name test case name - */ - public NestableErrorTest(String name) - { - super(name); - } - - /** - * Sets up instance variables required by this test case. - */ - @Override - public void setUp() - { - } - - /** - * Returns the test suite - * - * @return the test suite - */ - public static Test suite() - { - return new TestSuite(NestableErrorTest.class); - } - - /** - * Tears down instance variables required by this test case. - */ - @Override - public void tearDown() - { - } - - /** - * Command line entry point for running the test suite. - * - * @param args array of command line arguments - */ - public static void main(String args[]) - { - TestRunner.run(suite()); - } - - /** - * @see AbstractNestableTest#getNestable() - */ - @Override - public Nestable getNestable() - { - return new NestableError(); - } - - /** - * @see AbstractNestableTest#getNestable(Nestable) - */ - @Override - public Nestable getNestable(Nestable n) - { - return new NestableError((Throwable) n); - } - - /** - * @see AbstractNestableTest#getNestable(String) - */ - @Override - public Nestable getNestable(String msg) - { - return new NestableError(msg); - } - - /** - * @see AbstractNestableTest#getNestable(Throwable) - */ - @Override - public Nestable getNestable(Throwable t) - { - return new NestableError(t); - } - - /** - * @see AbstractNestableTest#getNestable(String, Throwable) - */ - @Override - public Nestable getNestable(String msg, Throwable t) - { - return new NestableError(msg, t); - } - - /** - * @see AbstractNestableTest#getNestable(String, Nestable) - */ - @Override - public Nestable getNestable(String msg, Nestable n) - { - return new NestableError(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1(Throwable) - */ - @Override - public Nestable getTester1(Throwable t) - { - return new NestableErrorTester1(t); - } - - /** - * @see AbstractNestableTest#getTester1(Nestable) - */ - @Override - public Nestable getTester1(Nestable n) - { - return new NestableErrorTester1((Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1(String, Throwable) - */ - @Override - public Nestable getTester1(String msg, Throwable t) - { - return new NestableErrorTester1(msg, t); - } - - /** - * @see AbstractNestableTest#getTester1(String, Nestable) - */ - @Override - public Nestable getTester1(String msg, Nestable n) - { - return new NestableErrorTester1(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1Class() - */ - @Override - public Class getTester1Class() - { - return NestableErrorTester1.class; - } - - /** - * @see AbstractNestableTest#getTester2(String, Throwable) - */ - @Override - public Nestable getTester2(String msg, Throwable t) - { - return new NestableErrorTester2(msg, t); - } - - /** - * @see AbstractNestableTest#getTester2(String, Nestable) - */ - @Override - public Nestable getTester2(String msg, Nestable n) - { - return new NestableErrorTester2(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester2Class() - */ - @Override - public Class getTester2Class() - { - return NestableErrorTester2.class; - } - - /** - * @see AbstractNestableTest#getThrowable(String) - */ - @Override - public Throwable getThrowable(String msg) - { - return new EOFException(msg); - } - - /** - * @see AbstractNestableTest#getThrowableClass() - */ - @Override - public Class getThrowableClass() - { - return EOFException.class; - } - - /** - * @see AbstractNestableTest#getBaseThrowableClass() - */ - @Override - public Class getBaseThrowableClass() - { - return Error.class; - } - -} - -/** - * First nestable tester implementation for use in test cases. - */ -class NestableErrorTester1 extends NestableError -{ - public NestableErrorTester1() - { - super(); - } - - public NestableErrorTester1(String reason, Throwable cause) - { - super(reason, cause); - } - - public NestableErrorTester1(String reason) - { - super(reason); - } - - public NestableErrorTester1(Throwable cause) - { - super(cause); - } - -} - -/** - * Second nestable tester implementation for use in test cases. - */ -class NestableErrorTester2 extends NestableError -{ - public NestableErrorTester2() - { - super(); - } - - public NestableErrorTester2(String reason, Throwable cause) - { - super(reason, cause); - } - - public NestableErrorTester2(String reason) - { - super(reason); - } - - public NestableErrorTester2(Throwable cause) - { - super(cause); - } - -} diff --git a/src/test/org/apache/commons/lang/exception/NestableExceptionTest.java b/src/test/org/apache/commons/lang/exception/NestableExceptionTest.java deleted file mode 100644 index 8b966d9a5..000000000 --- a/src/test/org/apache/commons/lang/exception/NestableExceptionTest.java +++ /dev/null @@ -1,386 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.EOFException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.PrintStream; - -import junit.framework.Test; -import junit.framework.TestSuite; -import junit.textui.TestRunner; - -/** - * Tests the org.apache.commons.lang.exception.NestableException class. - * - * @author Steven Caswell - * @version $Id$ - */ -public class NestableExceptionTest extends AbstractNestableTest { - - /** - * Construct a new instance of - * NestableExceptionTest. - * - * @param name test case name - */ - public NestableExceptionTest(String name) - { - super(name); - } - - /** - * Sets up instance variables required by this test case. - */ - @Override - public void setUp() - { - } - - /** - * Returns the test suite - * - * @return the test suite - */ - public static Test suite() - { - return new TestSuite(NestableExceptionTest.class); - } - - /** - * Tears down instance variables required by this test case. - */ - @Override - public void tearDown() - { - } - - /** - * Command line entry point for running the test suite. - * - * @param args array of command line arguments - */ - public static void main(String args[]) - { - TestRunner.run(suite()); - } - - /** - * @see AbstractNestableTest#getNestable() - */ - @Override - public Nestable getNestable() - { - return new NestableException(); - } - - /** - * @see AbstractNestableTest#getNestable(Nestable) - */ - @Override - public Nestable getNestable(Nestable n) - { - return new NestableException((Throwable) n); - } - - /** - * @see AbstractNestableTest#getNestable(String) - */ - @Override - public Nestable getNestable(String msg) - { - return new NestableException(msg); - } - - /** - * @see AbstractNestableTest#getNestable(Throwable) - */ - @Override - public Nestable getNestable(Throwable t) - { - return new NestableException(t); - } - - /** - * @see AbstractNestableTest#getNestable(String, Throwable) - */ - @Override - public Nestable getNestable(String msg, Throwable t) - { - return new NestableException(msg, t); - } - - /** - * @see AbstractNestableTest#getNestable(String, Nestable) - */ - @Override - public Nestable getNestable(String msg, Nestable n) - { - return new NestableException(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1(Throwable) - */ - @Override - public Nestable getTester1(Throwable t) - { - return new NestableExceptionTester1(t); - } - - /** - * @see AbstractNestableTest#getTester1(Nestable) - */ - @Override - public Nestable getTester1(Nestable n) - { - return new NestableExceptionTester1((Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1(String, Throwable) - */ - @Override - public Nestable getTester1(String msg, Throwable t) - { - return new NestableExceptionTester1(msg, t); - } - - /** - * @see AbstractNestableTest#getTester1(String, Nestable) - */ - @Override - public Nestable getTester1(String msg, Nestable n) - { - return new NestableExceptionTester1(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1Class() - */ - @Override - public Class getTester1Class() - { - return NestableExceptionTester1.class; - } - - /** - * @see AbstractNestableTest#getTester2(String, Throwable) - */ - @Override - public Nestable getTester2(String msg, Throwable t) - { - return new NestableExceptionTester2(msg, t); - } - - /** - * @see AbstractNestableTest#getTester2(String, Nestable) - */ - @Override - public Nestable getTester2(String msg, Nestable n) - { - return new NestableExceptionTester2(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester2Class() - */ - @Override - public Class getTester2Class() - { - return NestableExceptionTester2.class; - } - - /** - * @see AbstractNestableTest#getThrowable(String) - */ - @Override - public Throwable getThrowable(String msg) - { - return new EOFException(msg); - } - - /** - * @see AbstractNestableTest#getThrowableClass() - */ - @Override - public Class getThrowableClass() - { - return EOFException.class; - } - - /** - * @see AbstractNestableTest#getBaseThrowableClass() - */ - @Override - public Class getBaseThrowableClass() - { - return Exception.class; - } - - public void testSpecificPrintStackTrace() - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(baos); - NestableException ne = new NestableException("outer", new NestableException("inner", new Exception("another exception"))); - for(int i = 0; i < 2; i++) - { - if(i == 0) - { - // Test printStackTrac() - // Replace System.err with our own PrintStream so that we can - // obtain and check the printStrackTrace output - PrintStream err = System.err; - System.setErr(ps); - ne.printStackTrace(); - // Restore the System.err - System.setErr(err); - } - else - { - // Test printStackTrace(PrintStream) - ne.printStackTrace(ps); - } - } - String msg = baos.toString(); - assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableException: outer")); - assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") >= 0); - assertTrue( "printStackTrace() contains 2nd nested message", msg.indexOf("Caused by: java.lang.Exception: another exception") >= 0); - assertTrue( "printStackTrace() inner message after outer message", - msg.indexOf("org.apache.commons.lang.exception.NestableException: outer") < - msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner")); - assertTrue( "printStackTrace() cause message after inner message", - msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") < - msg.indexOf("Caused by: java.lang.Exception: another exception")); - } - - public void testSerialization() - throws java.io.IOException, ClassNotFoundException - { - RuntimeException nestedEx = new RuntimeException("nested exception message"); - NestableExceptionTester1 ex = new NestableExceptionTester1("serialization test", nestedEx); - - assertTrue( "implements java.io.Serializable", nestedEx instanceof java.io.Serializable); - - assertTrue( "implements java.io.Serializable", ex instanceof java.io.Serializable); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ByteArrayInputStream bais = null; - ObjectOutputStream oos = null; - ObjectInputStream ois = null; - - try - { - oos = new ObjectOutputStream(baos); - oos.writeObject(ex); - oos.flush(); - bais = new ByteArrayInputStream(baos.toByteArray()); - ois = new ObjectInputStream(bais); - NestableExceptionTester1 deserializedEx = (NestableExceptionTester1) ois.readObject(); - assertEquals( - "getThrowableCount() return value", - ex.getThrowableCount(), - deserializedEx.getThrowableCount()); - - for (int i = 0; i < ex.getThrowableCount(); i++) - { - Throwable t = ex.getThrowable(i); - Throwable deserializedThrowable = deserializedEx.getThrowable(i); - - assertEquals( t.getClass(), - deserializedThrowable.getClass()); - - assertEquals( - t.getMessage(), - deserializedThrowable.getMessage()); - } - } - finally - { - if (null != oos) - { - try - { - oos.close(); - } - catch (Exception ignored) - { - // intentionally empty - } - } - } - - } -} - -/** - * First nestable tester implementation for use in test cases. - */ -class NestableExceptionTester1 extends NestableException -{ - public NestableExceptionTester1() - { - super(); - } - - public NestableExceptionTester1(String reason, Throwable cause) - { - super(reason, cause); - } - - public NestableExceptionTester1(String reason) - { - super(reason); - } - - public NestableExceptionTester1(Throwable cause) - { - super(cause); - } - -} - -/** - * Second nestable tester implementation for use in test cases. - */ -class NestableExceptionTester2 extends NestableException -{ - public NestableExceptionTester2() - { - super(); - } - - public NestableExceptionTester2(String reason, Throwable cause) - { - super(reason, cause); - } - - public NestableExceptionTester2(String reason) - { - super(reason); - } - - public NestableExceptionTester2(Throwable cause) - { - super(cause); - } - -} - diff --git a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTest.java b/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTest.java deleted file mode 100644 index bb5aa961f..000000000 --- a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTest.java +++ /dev/null @@ -1,329 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang.exception; - -import java.io.ByteArrayOutputStream; -import java.io.EOFException; -import java.io.PrintStream; - -import junit.framework.Test; -import junit.framework.TestSuite; -import junit.textui.TestRunner; - -/** - * Tests the org.apache.commons.lang.exception.NestableRuntimeException class. - * - * @author Steven Caswell - * @version $Id$ - */ -public class NestableRuntimeExceptionTest extends AbstractNestableTest { - - /** - * Construct a new instance of - * NestableRuntimeExceptionTest. - * - * @param name test case name - */ - public NestableRuntimeExceptionTest(String name) - { - super(name); - } - - /** - * Sets up instance variables required by this test case. - */ - @Override - public void setUp() - { - } - - /** - * Returns the test suite - * - * @return the test suite - */ - public static Test suite() - { - return new TestSuite(NestableRuntimeExceptionTest.class); - } - - /** - * Tears down instance variables required by this test case. - */ - @Override - public void tearDown() - { - } - - /** - * Command line entry point for running the test suite. - * - * @param args array of command line arguments - */ - public static void main(String args[]) - { - TestRunner.run(suite()); - } - - /** - * @see AbstractNestableTest#getNestable() - */ - @Override - public Nestable getNestable() - { - return new NestableRuntimeException(); - } - - /** - * @see AbstractNestableTest#getNestable(Nestable) - */ - @Override - public Nestable getNestable(Nestable n) - { - return new NestableRuntimeException((Throwable) n); - } - - /** - * @see AbstractNestableTest#getNestable(String) - */ - @Override - public Nestable getNestable(String msg) - { - return new NestableRuntimeException(msg); - } - - /** - * @see AbstractNestableTest#getNestable(Throwable) - */ - @Override - public Nestable getNestable(Throwable t) - { - return new NestableRuntimeException(t); - } - - /** - * @see AbstractNestableTest#getNestable(String, Throwable) - */ - @Override - public Nestable getNestable(String msg, Throwable t) - { - return new NestableRuntimeException(msg, t); - } - - /** - * @see AbstractNestableTest#getNestable(String, Nestable) - */ - @Override - public Nestable getNestable(String msg, Nestable n) - { - return new NestableRuntimeException(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1(Throwable) - */ - @Override - public Nestable getTester1(Throwable t) - { - return new NestableRuntimeExceptionTester1(t); - } - - /** - * @see AbstractNestableTest#getTester1(Nestable) - */ - @Override - public Nestable getTester1(Nestable n) - { - return new NestableRuntimeExceptionTester1((Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1(String, Throwable) - */ - @Override - public Nestable getTester1(String msg, Throwable t) - { - return new NestableRuntimeExceptionTester1(msg, t); - } - - /** - * @see AbstractNestableTest#getTester1(String, Nestable) - */ - @Override - public Nestable getTester1(String msg, Nestable n) - { - return new NestableRuntimeExceptionTester1(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester1Class() - */ - @Override - public Class getTester1Class() - { - return NestableRuntimeExceptionTester1.class; - } - - /** - * @see AbstractNestableTest#getTester2(String, Throwable) - */ - @Override - public Nestable getTester2(String msg, Throwable t) - { - return new NestableRuntimeExceptionTester2(msg, t); - } - - /** - * @see AbstractNestableTest#getTester2(String, Nestable) - */ - @Override - public Nestable getTester2(String msg, Nestable n) - { - return new NestableRuntimeExceptionTester2(msg, (Throwable) n); - } - - /** - * @see AbstractNestableTest#getTester2Class() - */ - @Override - public Class getTester2Class() - { - return NestableRuntimeExceptionTester2.class; - } - - /** - * @see AbstractNestableTest#getThrowable(String) - */ - @Override - public Throwable getThrowable(String msg) - { - return new EOFException(msg); - } - - /** - * @see AbstractNestableTest#getThrowableClass() - */ - @Override - public Class getThrowableClass() - { - return EOFException.class; - } - - /** - * @see AbstractNestableTest#getBaseThrowableClass() - */ - @Override - public Class getBaseThrowableClass() - { - return RuntimeException.class; - } - - public void testSpecificPrintStackTrace() - { - // Test printStackTrac() - // Replace System.err with our own PrintStream so that we can obtain - // and check the printStrackTrace output - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(baos); - NestableRuntimeException ne = new NestableRuntimeException("outer", new NestableRuntimeException("inner", new Exception("another exception"))); - for(int i = 0; i < 2; i++) - { - if(i == 0) - { - // Test printStackTrac() - // Replace System.err with our own PrintStream so that we can - // obtain and check the printStrackTrace output - PrintStream err = System.err; - System.setErr(ps); - ne.printStackTrace(); - // Restore the System.err - System.setErr(err); - } - else - { - // Test printStackTrace(PrintStream) - ne.printStackTrace(ps); - } - } - String msg = baos.toString(); - assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableRuntimeException: outer")); - assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner") >= 0); - assertTrue( "printStackTrace() contains 2nd nested message", msg.indexOf("Caused by: java.lang.Exception: another exception") >= 0); - assertTrue( "printStackTrace() inner message after outer message", - msg.indexOf("org.apache.commons.lang.exception.NestableRuntimeException: outer") < - msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner")); - assertTrue( "printStackTrace() cause message after inner message", - msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner") < - msg.indexOf("Caused by: java.lang.Exception: another exception")); - } - -} - -/** - * First nestable tester implementation for use in test cases. - */ -class NestableRuntimeExceptionTester1 extends NestableRuntimeException -{ - public NestableRuntimeExceptionTester1() - { - super(); - } - - public NestableRuntimeExceptionTester1(String reason, Throwable cause) - { - super(reason, cause); - } - - public NestableRuntimeExceptionTester1(String reason) - { - super(reason); - } - - public NestableRuntimeExceptionTester1(Throwable cause) - { - super(cause); - } - -} - -/** - * Second nestable tester implementation. - */ -class NestableRuntimeExceptionTester2 extends NestableRuntimeException -{ - public NestableRuntimeExceptionTester2() - { - super(); - } - - public NestableRuntimeExceptionTester2(String reason, Throwable cause) - { - super(reason, cause); - } - - public NestableRuntimeExceptionTester2(String reason) - { - super(reason); - } - - public NestableRuntimeExceptionTester2(Throwable cause) - { - super(cause); - } - -} -