You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/03/04 01:13:39 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang NotImplementedExceptionTest.java

scolebourne    2004/03/03 16:13:39

  Modified:    lang/src/java/org/apache/commons/lang
                        NotImplementedException.java
               lang/src/test/org/apache/commons/lang
                        NotImplementedExceptionTest.java
  Log:
  Make exception implement Nestable interface and use NestableDelegate
  bug 26954 (alternate implementation)
  
  Revision  Changes    Path
  1.7       +135 -12   jakarta-commons/lang/src/java/org/apache/commons/lang/NotImplementedException.java
  
  Index: NotImplementedException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/NotImplementedException.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- NotImplementedException.java	18 Feb 2004 22:59:49 -0000	1.6
  +++ NotImplementedException.java	4 Mar 2004 00:13:38 -0000	1.7
  @@ -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$
    */
  -public class NotImplementedException extends UnsupportedOperationException {
  +public class NotImplementedException
  +        extends UnsupportedOperationException implements Nestable {
  +
  +    /**
  +     * 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);
  +    }
   
       /**
  -     * <p>Constructs the exception with the specified class.</p>
  +     * 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);
       }
   
   }
  
  
  
  1.4       +53 -39    jakarta-commons/lang/src/test/org/apache/commons/lang/NotImplementedExceptionTest.java
  
  Index: NotImplementedExceptionTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/NotImplementedExceptionTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- NotImplementedExceptionTest.java	18 Feb 2004 23:06:19 -0000	1.3
  +++ NotImplementedExceptionTest.java	4 Mar 2004 00:13:38 -0000	1.4
  @@ -41,43 +41,57 @@
           super(testName);
       }
   
  -    // testConstructor
  -
  -    public void testConstructor_classArg_nullInput() {
  -        final Class c = null;
  -        new NotImplementedException(c);
  -    }
  -
  -    public void testConstructor_stringArg_nullInput() {
  -        final String s = null;
  -        new NotImplementedException(s);
  -    }
  -
  -    // 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 testGetMessage_classArg_validInput() {
  -        final Throwable t = new NotImplementedException(String.class);
  -        assertEquals(
  -            "Method is not implemented in class java.lang.String",
  -            t.getMessage());
  -    }
  -
  -    public void testGetMessage_stringArg_nullInput() {
  -        final String s = null;
  -        final Throwable t = new NotImplementedException(s);
  -        assertEquals(null, t.getMessage());
  -    }
  -
  -    public void testGetMessage_stringArg_validInput() {
  -        final String msg = "message";
  -        final Throwable t = new NotImplementedException(msg);
  -        assertEquals(msg, t.getMessage());
  +    //-----------------------------------------------------------------------
  +    public void testConstructor_() {
  +        NotImplementedException ex = new NotImplementedException();
  +        assertEquals("Code is not implemented", ex.getMessage());
  +        assertEquals(null, ex.getCause());
  +    }
  +
  +    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());
  +    }
  +
  +    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 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 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());
       }
   
  -} // NotImplementedExceptionTest
  +}
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org