commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/exception Nestable.java NestableDelegate.java
Date Sat, 09 Oct 2004 10:45:24 GMT
scolebourne    2004/10/09 03:45:24

  Modified:    lang/src/test/org/apache/commons/lang/exception
                        NestableDelegateTestCase.java
                        AbstractNestableTestCase.java
                        NestableErrorTestCase.java
                        NestableRuntimeExceptionTestCase.java
                        NestableExceptionTestCase.java
               lang/src/java/org/apache/commons/lang/exception
                        Nestable.java NestableDelegate.java
  Log:
  Change behaviour of indexOf to match subclasses, with appropriate comments
  bug 30929
  
  Revision  Changes    Path
  1.9       +11 -5     jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java
  
  Index: NestableDelegateTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- NestableDelegateTestCase.java	18 Feb 2004 23:02:15 -0000	1.8
  +++ NestableDelegateTestCase.java	9 Oct 2004 10:45:24 -0000	1.9
  @@ -16,6 +16,8 @@
   package org.apache.commons.lang.exception;
   
   import java.io.ByteArrayOutputStream;
  +import java.io.EOFException;
  +import java.io.IOException;
   import java.io.PrintStream;
   import java.io.PrintWriter;
   
  @@ -422,13 +424,13 @@
           throwables[1] = NestableDelegateTester2.class;
           throwables[2] = NestableDelegateTester1.class;
           throwables[3] = NestableDelegateTester2.class;
  -        throwables[4] = Exception.class;
  +        throwables[4] = EOFException.class;
           int[] indexes = {0, 1, 0, 1, 4};
           n = new NestableDelegateTester1(msgs[0], 
                   new NestableDelegateTester2(msgs[1], 
                       new NestableDelegateTester1(
                           new NestableDelegateTester2(msgs[3], 
  -                            new Exception(msgs[4])
  +                            new EOFException(msgs[4])
                           )
                       )
                   )
  @@ -442,8 +444,12 @@
           doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 1, 2, msgs[2]);
           doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 3, -1, null);
           doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 4, -1, null);
  -        doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 4, msgs[4]);
  +        doNestableDelegateIndexOfThrowable(d, EOFException.class, 2, 4, msgs[4]);
  +        doNestableDelegateIndexOfThrowable(d, IOException.class, 2, 4, msgs[4]);
  +        doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 2, msgs[2]);
  +        doNestableDelegateIndexOfThrowable(d, Exception.class, 0, 0, msgs[0]);
           doNestableDelegateIndexOfThrowable(d, java.util.Date.class, 0, -1, null);
  +        doNestableDelegateIndexOfThrowable(d, null, 0, -1, null);
           
           // Test for index out of bounds
           try
  @@ -469,7 +475,7 @@
           Throwable t = null;
           
           int index = d.indexOfThrowable(type, fromIndex);
  -        assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  +        assertEquals("index of throwable " + (type == null ? "null" : type.getName()),
expectedIndex, index);
           if(expectedIndex > -1)
           {
               t = d.getThrowable(index);
  
  
  
  1.7       +14 -3     jakarta-commons/lang/src/test/org/apache/commons/lang/exception/AbstractNestableTestCase.java
  
  Index: AbstractNestableTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/AbstractNestableTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- AbstractNestableTestCase.java	18 Feb 2004 23:02:15 -0000	1.6
  +++ AbstractNestableTestCase.java	9 Oct 2004 10:45:24 -0000	1.7
  @@ -360,7 +360,9 @@
           {
               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)
  @@ -368,7 +370,7 @@
           Throwable t = null;
           
           int index = n.indexOfThrowable(type);
  -        assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  +        assertEquals("index of throwable " + (type == null ? "null" : type.getName()),
expectedIndex, index);
           if(expectedIndex > -1)
           {
               t = n.getThrowable(index);
  @@ -421,6 +423,7 @@
           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
  @@ -447,7 +450,7 @@
           Throwable t = null;
           
           int index = n.indexOfThrowable(type, fromIndex);
  -        assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  +        assertEquals("index of throwable " + (type == null ? "null" : type.getName()),
expectedIndex, index);
           if(expectedIndex > -1)
           {
               t = n.getThrowable(index);
  @@ -665,5 +668,13 @@
        * @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();
  +
   }
   
  
  
  
  1.7       +12 -2     jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableErrorTestCase.java
  
  Index: NestableErrorTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableErrorTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- NestableErrorTestCase.java	18 Feb 2004 23:02:15 -0000	1.6
  +++ NestableErrorTestCase.java	9 Oct 2004 10:45:24 -0000	1.7
  @@ -15,6 +15,8 @@
    */
   package org.apache.commons.lang.exception;
   
  +import java.io.EOFException;
  +
   import junit.framework.Test;
   import junit.framework.TestSuite;
   import junit.textui.TestRunner;
  @@ -189,13 +191,21 @@
        */
       public Throwable getThrowable(String msg)
       {
  -        return new Error(msg);
  +        return new EOFException(msg);
       }
       
       /**
        * @see AbstractNestableTestCase#getThrowableClass()
        */
       public Class getThrowableClass()
  +    {
  +        return EOFException.class;
  +    }
  +    
  +    /**
  +     * @see AbstractNestableTestCase#getBaseThrowableClass()
  +     */
  +    public Class getBaseThrowableClass()
       {
           return Error.class;
       }
  
  
  
  1.12      +11 -2     jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java
  
  Index: NestableRuntimeExceptionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- NestableRuntimeExceptionTestCase.java	18 Feb 2004 23:22:29 -0000	1.11
  +++ NestableRuntimeExceptionTestCase.java	9 Oct 2004 10:45:24 -0000	1.12
  @@ -16,6 +16,7 @@
   package org.apache.commons.lang.exception;
   
   import java.io.ByteArrayOutputStream;
  +import java.io.EOFException;
   import java.io.PrintStream;
   
   import junit.framework.Test;
  @@ -192,13 +193,21 @@
        */
       public Throwable getThrowable(String msg)
       {
  -        return new RuntimeException(msg);
  +        return new EOFException(msg);
       }
       
       /**
        * @see AbstractNestableTestCase#getThrowableClass()
        */
       public Class getThrowableClass()
  +    {
  +        return EOFException.class;
  +    }
  +    
  +    /**
  +     * @see AbstractNestableTestCase#getBaseThrowableClass()
  +     */
  +    public Class getBaseThrowableClass()
       {
           return RuntimeException.class;
       }
  
  
  
  1.12      +12 -3     jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java
  
  Index: NestableExceptionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- NestableExceptionTestCase.java	18 Feb 2004 23:02:15 -0000	1.11
  +++ NestableExceptionTestCase.java	9 Oct 2004 10:45:24 -0000	1.12
  @@ -17,6 +17,7 @@
   
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
  +import java.io.EOFException;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
   import java.io.PrintStream;
  @@ -195,7 +196,7 @@
        */
       public Throwable getThrowable(String msg)
       {
  -        return new Exception(msg);
  +        return new EOFException(msg);
       }
       
       /**
  @@ -203,9 +204,17 @@
        */
       public Class getThrowableClass()
       {
  -        return Exception.class;
  +        return EOFException.class;
       }
   
  +    /**
  +     * @see AbstractNestableTestCase#getBaseThrowableClass()
  +     */
  +    public Class getBaseThrowableClass()
  +    {
  +        return Exception.class;
  +    }
  +    
       public void testSpecificPrintStackTrace()
       {
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
  
  
  1.12      +22 -10    jakarta-commons/lang/src/java/org/apache/commons/lang/exception/Nestable.java
  
  Index: Nestable.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/Nestable.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Nestable.java	18 Feb 2004 22:54:04 -0000	1.11
  +++ Nestable.java	9 Oct 2004 10:45:24 -0000	1.12
  @@ -106,10 +106,16 @@
   
       /**
        * Returns the index, numbered from 0, of the first occurrence of the
  -     * specified type in the chain of <code>Throwable</code>s, or -1 if the
  -     * specified type is not found in the chain.
  +     * 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 <code>Class</code> to be found
  +     * @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
        */
  @@ -117,11 +123,17 @@
   
       /**
        * Returns the index, numbered from 0, of the first <code>Throwable</code>
  -     * that matches the specified type in the chain of <code>Throwable</code>s
  -     * with an index greater than or equal to the specified index, or -1 if
  -     * the type is not found.
  +     * 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 <code>Class</code> to be found
  +     * @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
  @@ -152,8 +164,8 @@
   
       /**
        * 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
  +     * 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.
  
  
  
  1.26      +43 -10    jakarta-commons/lang/src/java/org/apache/commons/lang/exception/NestableDelegate.java
  
  Index: NestableDelegate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/NestableDelegate.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- NestableDelegate.java	30 Sep 2004 07:03:25 -0000	1.25
  +++ NestableDelegate.java	9 Oct 2004 10:45:24 -0000	1.26
  @@ -62,6 +62,9 @@
       /**
        * 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;
  @@ -69,9 +72,22 @@
       /**
        * 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
  @@ -212,11 +228,19 @@
   
       /**
        * Returns the index, numbered from 0, of the first <code>Throwable</code>
  -     * that matches the specified type in the chain of <code>Throwable</code>s
  -     * held in this delegate's <code>Nestable</code> with an index greater
than
  -     * or equal to the specified index, or -1 if the type is not found.
  +     * 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 <code>Class</code> to be found
  +     * @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
  @@ -227,6 +251,9 @@
        * @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);
           }
  @@ -235,11 +262,17 @@
               throw new IndexOutOfBoundsException("The start index was out of bounds: "
                   + fromIndex + " >= " + throwables.length);
           }
  -        for (int i = fromIndex; i < throwables.length; i++) {
  -// TODO: decide on whether to include this
  -//            if (type.isAssignableFrom(throwables[i].getClass())) {
  -            if (throwables[i].getClass().equals(type)) {
  -                return i;
  +        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;
  
  
  

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


Mime
View raw message