Make exception implement Nestable interface and use NestableDelegate

bug 26954 (alternate implementation)


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137821 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-03-04 00:13:38 +00:00
parent 9250f20144
commit 5e7cf0ed0e
2 changed files with 180 additions and 43 deletions

View File

@ -15,33 +15,156 @@
*/
package org.apache.commons.lang;
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 method has not been implemented.</p>
* <p>Thrown to indicate that a block of code has not been implemented.</p>
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
* @since 2.0
* @version $Id: NotImplementedException.java,v 1.6 2004/02/18 22:59:49 ggregory Exp $
* @version $Id: NotImplementedException.java,v 1.7 2004/03/04 00:13:38 scolebourne Exp $
*/
public class NotImplementedException extends UnsupportedOperationException {
public class NotImplementedException
extends UnsupportedOperationException implements Nestable {
/**
* <p>Constructs the exception with the specified class.</p>
* The exception helper to delegate nested exception handling to.
*/
protected 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
* detail message.
*/
public NotImplementedException() {
super("Code is not implemented");
}
/**
* Constructs a new <code>NotImplementedException</code> with specified
* detail message.
*
* @param msg The error message.
*/
public NotImplementedException(String msg) {
super(msg == null ? "Code is not implemented" : msg);
}
/**
* Constructs a new <code>NotImplementedException</code> with specified
* nested <code>Throwable</code> and default message.
*
* @param cause the exception or error that caused this exception to be
* thrown
*/
public NotImplementedException(Throwable cause) {
super("Code is not implemented");
this.cause = cause;
}
/**
* Constructs a new <code>NotImplementedException</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 NotImplementedException(String msg, Throwable cause) {
super(msg == null ? "Code is not implemented" : msg);
this.cause = cause;
}
/**
* Constructs a new <code>NotImplementedException</code> referencing
* the specified class.
*
* @param clazz the <code>Class</code> that has not implemented the method
*/
public NotImplementedException(Class clazz) {
super(
"Method is not implemented in class "
+ ((clazz == null) ? null : clazz.getName()));
super((clazz == null ?
"Code is not implemented" :
"Code is not implemented in " + clazz));
}
//-----------------------------------------------------------------------
public Throwable getCause() {
return cause;
}
/**
* <p>Constructs the exception with the specified message.</p>
*
* @param msg the exception message.
* Returns the detail message string of this throwable. If it was
* created with a null message, returns the following:
* (cause==null ? null : cause.toString()).
*/
public NotImplementedException(String msg) {
super(msg);
public String getMessage() {
if (super.getMessage() != null) {
return super.getMessage();
} else if (cause != null) {
return cause.toString();
} else {
return null;
}
}
public String getMessage(int index) {
if (index == 0) {
return super.getMessage();
} else {
return delegate.getMessage(index);
}
}
public String[] getMessages() {
return delegate.getMessages();
}
public Throwable getThrowable(int index) {
return delegate.getThrowable(index);
}
public int getThrowableCount() {
return delegate.getThrowableCount();
}
public Throwable[] getThrowables() {
return delegate.getThrowables();
}
public int indexOfThrowable(Class type) {
return delegate.indexOfThrowable(type, 0);
}
public int indexOfThrowable(Class type, int fromIndex) {
return delegate.indexOfThrowable(type, fromIndex);
}
public void printStackTrace() {
delegate.printStackTrace();
}
public void printStackTrace(PrintStream out) {
delegate.printStackTrace(out);
}
public void printStackTrace(PrintWriter out) {
delegate.printStackTrace(out);
}
public final void printPartialStackTrace(PrintWriter out) {
super.printStackTrace(out);
}
}

View File

@ -24,7 +24,7 @@
* JUnit tests.
*
* @author Matthew Hawthorne
* @version $Id: NotImplementedExceptionTest.java,v 1.3 2004/02/18 23:06:19 ggregory Exp $
* @version $Id: NotImplementedExceptionTest.java,v 1.4 2004/03/04 00:13:38 scolebourne Exp $
* @see NotImplementedException
*/
public class NotImplementedExceptionTest extends TestCase {
@ -41,43 +41,57 @@ public NotImplementedExceptionTest(String testName) {
super(testName);
}
// testConstructor
public void testConstructor_classArg_nullInput() {
final Class c = null;
new NotImplementedException(c);
//-----------------------------------------------------------------------
public void testConstructor_() {
NotImplementedException ex = new NotImplementedException();
assertEquals("Code is not implemented", ex.getMessage());
assertEquals(null, ex.getCause());
}
public void testConstructor_stringArg_nullInput() {
final String s = null;
new NotImplementedException(s);
public void testConstructor_String1() {
NotImplementedException ex = new NotImplementedException((String) null);
assertEquals("Code is not implemented", ex.getMessage());
assertEquals(null, ex.getCause());
}
public void testConstructor_String2() {
NotImplementedException ex = new NotImplementedException("msg");
assertEquals("msg", ex.getMessage());
assertEquals(null, ex.getCause());
}
// testGetMessage
public void testGetMessage_classArg_nullInput() {
final Class c = null;
final Throwable t = new NotImplementedException(c);
assertEquals("Method is not implemented in class null", t.getMessage());
public void testConstructor_Throwable1() {
NotImplementedException ex = new NotImplementedException((Throwable) null);
assertEquals("Code is not implemented", ex.getMessage());
assertEquals(null, ex.getCause());
}
public void testConstructor_Throwable2() {
Exception npe = new NullPointerException();
NotImplementedException ex = new NotImplementedException(npe);
assertEquals("Code is not implemented", ex.getMessage());
assertSame(npe, ex.getCause());
}
public void testGetMessage_classArg_validInput() {
final Throwable t = new NotImplementedException(String.class);
assertEquals(
"Method is not implemented in class java.lang.String",
t.getMessage());
public void testConstructor_StringThrowable1() {
NotImplementedException ex = new NotImplementedException((String) null, (Throwable) null);
assertEquals("Code is not implemented", ex.getMessage());
assertEquals(null, ex.getCause());
}
public void testConstructor_StringThrowable2() {
Exception npe = new NullPointerException();
NotImplementedException ex = new NotImplementedException("msg", npe);
assertEquals("msg", ex.getMessage());
assertSame(npe, ex.getCause());
}
public void testGetMessage_stringArg_nullInput() {
final String s = null;
final Throwable t = new NotImplementedException(s);
assertEquals(null, t.getMessage());
public void testConstructor_Class1() {
NotImplementedException ex = new NotImplementedException((Class) null);
assertEquals("Code is not implemented", ex.getMessage());
assertEquals(null, ex.getCause());
}
public void testConstructor_Class2() {
NotImplementedException ex = new NotImplementedException(String.class);
assertEquals("Code is not implemented in class java.lang.String", ex.getMessage());
assertEquals(null, ex.getCause());
}
public void testGetMessage_stringArg_validInput() {
final String msg = "message";
final Throwable t = new NotImplementedException(msg);
assertEquals(msg, t.getMessage());
}
} // NotImplementedExceptionTest
}