Removing the Lang NestableException concept and instead relying on the JDK support. [LANG-492]. [LANG-491] notes the need to rethink ExceptionUtils and its test.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@754589 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-03-15 02:10:44 +00:00
parent 13bd2d388e
commit 791e7f3830
19 changed files with 26 additions and 4436 deletions

View File

@ -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;
/**
* <p>Thrown to indicate that a block of code has not been implemented.
* This exception supplements <code>UnsupportedOperationException</code>
@ -29,9 +26,7 @@
*
* <p><code>NotImplementedException</code> 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.</p>
* This can act as an exception based TODO tag. </p>
*
* <pre>
* 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 <code>NotImplementedException</code> 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 <code>Throwable</code> in the chain
* of <code>Throwable</code>s at the specified index, numbered from 0.
*
* @param index the index of the <code>Throwable</code> in the chain
* @return the error message, or null if the <code>Throwable</code> at the
* specified index in the chain does not contain a message
* @throws IndexOutOfBoundsException if the <code>index</code> argument is
* negative or not less than the count of <code>Throwable</code>s 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 <code>Throwable</code> objects.
* Each throwable returns a message, a null string is included in the array if
* there is no message for a particular <code>Throwable</code>.
*
* @return the error messages
* @since 2.1
*/
public String[] getMessages() {
return delegate.getMessages();
}
/**
* Returns the <code>Throwable</code> in the chain by index.
*
* @param index the index to retrieve
* @return the <code>Throwable</code>
* @throws IndexOutOfBoundsException if the <code>index</code> argument is
* negative or not less than the count of <code>Throwable</code>s in the chain
* @since 2.1
*/
public Throwable getThrowable(int index) {
return delegate.getThrowable(index);
}
/**
* Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
*
* @return the throwable count
* @since 2.1
*/
public int getThrowableCount() {
return delegate.getThrowableCount();
}
/**
* Returns this <code>Nestable</code> and any nested <code>Throwable</code>s
* in an array of <code>Throwable</code>s, one element for each
* <code>Throwable</code>.
*
* @return the <code>Throwable</code>s
* @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, <code>-1</code> 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, <code>-1</code> 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 <code>fromIndex</code> argument
* is negative or not less than the count of <code>Throwable</code>s 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);
}
}

View File

@ -16,8 +16,6 @@
*/
package org.apache.commons.lang;
import org.apache.commons.lang.exception.NestableRuntimeException;
/**
* <p>Exception thrown when the Serialization process fails.</p>
*
@ -27,7 +25,7 @@
* @since 1.0
* @version $Id$
*/
public class SerializationException extends NestableRuntimeException {
public class SerializationException extends RuntimeException {
/**
* Required for serialization support.

View File

@ -21,8 +21,6 @@
import java.io.Writer;
import java.util.Locale;
import org.apache.commons.lang.exception.NestableRuntimeException;
/**
* <p>Escapes and unescapes <code>String</code>s for
* Java, Java Script, HTML, XML, and SQL.</p>
@ -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;

View File

@ -16,8 +16,6 @@
*/
package org.apache.commons.lang;
import org.apache.commons.lang.exception.NestableRuntimeException;
/**
* <p>Thrown when it is impossible or undesirable to consume or throw a checked exception.</p>
* 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.

View File

@ -248,8 +248,7 @@ public static boolean isCauseMethodName(String methodName) {
*
* <p>The method searches for methods with specific names that return a
* <code>Throwable</code> 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)}.</p>
*
* <p>The default list searched for are:</p>
@ -359,9 +358,7 @@ public static Throwable getRootCause(Throwable throwable) {
* @return the wrapped exception, or <code>null</code> 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) {
*
* <p>The end of line is determined by the value of {@link SystemUtils#LINE_SEPARATOR}.</p>
*
* <p>Functionality shared between the
* <code>getStackFrames(Throwable)</code> methods of this and the
* {@link org.apache.commons.lang.exception.NestableDelegate} classes.</p>
*
* @param stackTrace a stack trace String
* @return an array where each element is a line from the argument
*/

View File

@ -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 <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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 <code>Nestable</code> to be thrown.
*
* @return throwable that caused the original exception
*/
public Throwable getCause();
/**
* Returns the error message of this and any nested
* <code>Throwable</code>.
*
* @return the error message
*/
public String getMessage();
/**
* Returns the error message of the <code>Throwable</code> in the chain
* of <code>Throwable</code>s at the specified index, numbered from 0.
*
* @param index the index of the <code>Throwable</code> in the chain of
* <code>Throwable</code>s
* @return the error message, or null if the <code>Throwable</code> at the
* specified index in the chain does not contain a message
* @throws IndexOutOfBoundsException if the <code>index</code> argument is
* negative or not less than the count of <code>Throwable</code>s in the
* chain
*/
public String getMessage(int index);
/**
* Returns the error message of this and any nested <code>Throwable</code>s
* in an array of Strings, one element for each message. Any
* <code>Throwable</code> 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 <code>Throwable</code> in the chain of
* <code>Throwable</code>s at the specified index, numbered from 0.
*
* @param index the index, numbered from 0, of the <code>Throwable</code> in
* the chain of <code>Throwable</code>s
* @return the <code>Throwable</code>
* @throws IndexOutOfBoundsException if the <code>index</code> argument is
* negative or not less than the count of <code>Throwable</code>s in the
* chain
*/
public Throwable getThrowable(int index);
/**
* Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
*
* @return the throwable count
*/
public int getThrowableCount();
/**
* Returns this <code>Nestable</code> and any nested <code>Throwable</code>s
* in an array of <code>Throwable</code>s, one element for each
* <code>Throwable</code>.
*
* @return the <code>Throwable</code>s
*/
public Throwable[] getThrowables();
/**
* Returns the index, numbered from 0, of the first occurrence of the
* specified type, or a subclass, in the chain of <code>Throwable</code>s.
* The method returns -1 if the specified type is not found in the chain.
* <p>
* NOTE: From v2.1, we have clarified the <code>Nestable</code> 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 <code>Throwable</code>
* that matches the specified type, or a subclass, in the chain of <code>Throwable</code>s
* 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.
* <p>
* NOTE: From v2.1, we have clarified the <code>Nestable</code> 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 <code>fromIndex</code> argument
* is negative or not less than the count of <code>Throwable</code>s 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 <code>PrintWriter</code> 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 <code>PrintStream</code> 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
* <code>super.printStackTrace(out);</code> in most cases.
*
* @param out The writer to use.
*/
public void printPartialStackTrace(PrintWriter out);
}

View File

@ -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;
/**
* <p>A shared implementation of the nestable exception functionality.</p>
* <p>
* 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}.
* </p>
*
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
* @author Daniel L. Rall
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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 <code>NestableDelegate</code> instance to manage the
* specified <code>Nestable</code>.
*
* @param nestable the Nestable implementation (<i>must</i> 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 <code>Throwable</code> in the chain of <code>Throwable</code>s at the
* specified index, numbered from 0.
*
* @param index
* the index of the <code>Throwable</code> in the chain of <code>Throwable</code>s
* @return the error message, or null if the <code>Throwable</code> at the specified index in the chain does not
* contain a message
* @throws IndexOutOfBoundsException
* if the <code>index</code> argument is negative or not less than the count of <code>Throwable</code>s
* 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 <code>Nestable</code> and any nested <code>Throwable</code>s.
*
* @param baseMsg
* the base message to use when creating the full message. Should be generally be called via
* <code>nestableHelper.getMessage(super.getMessage())</code>, where <code>super</code> is an
* instance of {@link java.lang.Throwable}.
* @return The concatenated message for this and all nested <code>Throwable</code>s
* @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 <code>Throwable</code>s in an array of Strings, one element
* for each message. Any <code>Throwable</code> 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 <code>Throwable</code> in the chain of
* <code>Throwable</code>s at the specified index, numbered from 0.
*
* @param index the index, numbered from 0, of the <code>Throwable</code> in
* the chain of <code>Throwable</code>s
* @return the <code>Throwable</code>
* @throws IndexOutOfBoundsException if the <code>index</code> argument is
* negative or not less than the count of <code>Throwable</code>s 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 <code>Throwable</code>s contained in the
* <code>Nestable</code> contained by this delegate.
*
* @return the throwable count
* @since 2.0
*/
public int getThrowableCount() {
return ExceptionUtils.getThrowableCount(this.nestable);
}
/**
* Returns this delegate's <code>Nestable</code> and any nested
* <code>Throwable</code>s in an array of <code>Throwable</code>s, one
* element for each <code>Throwable</code>.
*
* @return the <code>Throwable</code>s
* @since 2.0
*/
public Throwable[] getThrowables() {
return ExceptionUtils.getThrowables(this.nestable);
}
/**
* Returns the index, numbered from 0, of the first <code>Throwable</code>
* that matches the specified type, or a subclass, in the chain of <code>Throwable</code>s
* 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.
* <p>
* NOTE: From v2.1, we have clarified the <code>Nestable</code> 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 <code>NestableDelegate</code>, 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 <code>fromIndex</code> argument
* is negative or not less than the count of <code>Throwable</code>s 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 <code>PrintStream</code> 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 <code>getCause</code>
* 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 <code>PrintWriter</code> 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
* <code>Throwable</code> object, decomposing it into a list of
* stack frames.
*
* @param t The <code>Throwable</code>.
* @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()])
);
}
}
}
}

View File

@ -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 <code>NestableError</code> without specified
* detail message.
*/
public NestableError() {
super();
this.cause = null;
}
/**
* Constructs a new <code>NestableError</code> with specified
* detail message.
*
* @param msg The error message.
*/
public NestableError(String msg) {
super(msg);
this.cause = null;
}
/**
* Constructs a new <code>NestableError</code> with specified
* nested <code>Throwable</code>.
*
* @param cause the exception or error that caused this exception to be
* thrown
*/
public NestableError(Throwable cause) {
super();
this.cause = cause;
}
/**
* Constructs a new <code>NestableError</code> with specified
* detail message and nested <code>Throwable</code>.
*
* @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);
}
}

View File

@ -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 <code>NestedException</code>, when the
* exception is finally printed out using any of the <code>
* printStackTrace()</code> methods, the stack trace will contain
* the information about all exceptions thrown and caught on
* the way.
* <p> Running the following program
* <p><blockquote><pre>
* 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 }
* </pre></blockquote>
* <p>Yields the following stack trace:
* <p><blockquote><pre>
* 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
* </pre></blockquote><br>
*
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
* @author Daniel L. Rall
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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 <code>NestableException</code> without specified
* detail message.
*/
public NestableException() {
super();
this.cause = null;
}
/**
* Constructs a new <code>NestableException</code> with specified
* detail message.
*
* @param msg The error message.
*/
public NestableException(String msg) {
super(msg);
this.cause = null;
}
/**
* Constructs a new <code>NestableException</code> with specified
* nested <code>Throwable</code>.
*
* @param cause the exception or error that caused this exception to be
* thrown
*/
public NestableException(Throwable cause) {
super();
this.cause = cause;
}
/**
* Constructs a new <code>NestableException</code> with specified
* detail message and nested <code>Throwable</code>.
*
* @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);
}
}

View File

@ -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 <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
* @author Daniel L. Rall
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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 <code>NestableRuntimeException</code> without specified
* detail message.
*/
public NestableRuntimeException() {
super();
this.cause = null;
}
/**
* Constructs a new <code>NestableRuntimeException</code> with specified
* detail message.
*
* @param msg the error message
*/
public NestableRuntimeException(String msg) {
super(msg);
this.cause = null;
}
/**
* Constructs a new <code>NestableRuntimeException</code> with specified
* nested <code>Throwable</code>.
*
* @param cause the exception or error that caused this exception to be
* thrown
*/
public NestableRuntimeException(Throwable cause) {
super();
this.cause = cause;
}
/**
* Constructs a new <code>NestableRuntimeException</code> with specified
* detail message and nested <code>Throwable</code>.
*
* @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);
}
}

View File

@ -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);
}
}

View File

@ -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());
}

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @version $Id$
*/
public abstract class AbstractNestableTest extends TestCase
{
/**
* Constructs an instance of
* <code>AbstractNestableTest</code>.
*
* @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 <code>Nestable</code> implementation being
* tested.
*
* @return the instance
*/
public abstract Nestable getNestable();
/**
* Returns an instance of the <code>Nestable</code> implementation being
* tested.
*
* @param n <code>Nestable</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Nestable getNestable(Nestable n);
/**
* Returns an instance of the <code>Nestable</code> implementation being
* tested.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Nestable getNestable(String msg);
/**
* Returns an instance of the <code>Nestable</code> implementation being
* tested.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @param n <code>Nestable</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Nestable getNestable(String msg, Nestable n);
/**
* Returns an instance of the <code>Nestable</code> implementation being
* tested.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @param t <code>Throwable</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Nestable getNestable(String msg, Throwable t);
/**
* Returns an instance of the <code>Nestable</code> implementation being
* tested.
*
* @param t <code>Throwable</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Nestable getNestable(Throwable t);
/**
* Returns an instance of a <code>Throwable</code> to be used in
* constructing instances of the <code>Nestable</code> implementation being
* tested.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Throwable getThrowable(String msg);
/**
* Returns an instance of one tester <code>Nestable</code> implementation.
*
* @param n <code>Nestable</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Nestable getTester1(Nestable n);
/**
* Returns an instance of one tester <code>Nestable</code> implementation.
*
* @param t <code>Throwable</code> argument to be provided to the instance
* constructor
* @return the instance
*/
public abstract Nestable getTester1(Throwable t);
/**
* Returns an instance of one tester <code>Nestable</code> implementation.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @param n <code>Nestable</code> 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 <code>Nestable</code> implementation.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @param t <code>Throwable</code> 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 <code>Nestable</code>
* implementation.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @param n <code>Nestable</code> 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 <code>Nestable</code>
* implementation.
*
* @param msg <code>String</code> argument to be provided to the instance
* constructor
* @param t <code>Throwable</code> 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 <code>Nestable</code>
* implementation.
*
* @return the class
*/
public abstract Class getTester1Class();
/**
* Returns the class of the second tester <code>Nestable</code>
* implementation.
*
* @return the class
*/
public abstract Class getTester2Class();
/**
* Returns the class of the <code>Throwable</code> used in constructing
* instances of the <code>Nestable</code> 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();
}

View File

@ -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;
}

View File

@ -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); }
}
}

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @version $Id$
*/
public class NestableErrorTest extends AbstractNestableTest {
/**
* Construct a new instance of
* <code>NestableErrorTest</code>.
*
* @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);
}
}

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @version $Id$
*/
public class NestableExceptionTest extends AbstractNestableTest {
/**
* Construct a new instance of
* <code>NestableExceptionTest</code>.
*
* @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);
}
}

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @version $Id$
*/
public class NestableRuntimeExceptionTest extends AbstractNestableTest {
/**
* Construct a new instance of
* <code>NestableRuntimeExceptionTest</code>.
*
* @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);
}
}