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 2003/07/26 15:11:22 UTC

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

scolebourne    2003/07/26 06:11:22

  Modified:    lang/src/test/org/apache/commons/lang/exception
                        ExceptionUtilsTestCase.java
  Log:
  Update null handling in ExceptionUtils
  
  Revision  Changes    Path
  1.9       +237 -66   jakarta-commons/lang/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
  
  Index: ExceptionUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ExceptionUtilsTestCase.java	23 Mar 2003 17:36:16 -0000	1.8
  +++ ExceptionUtilsTestCase.java	26 Jul 2003 13:11:21 -0000	1.9
  @@ -53,8 +53,14 @@
    */
   package org.apache.commons.lang.exception;
   
  +import java.io.ByteArrayOutputStream;
  +import java.io.PrintStream;
   import java.io.PrintWriter;
   import java.io.StringWriter;
  +import java.lang.reflect.InvocationTargetException;
  +import java.sql.SQLException;
  +
  +import org.apache.commons.lang.SystemUtils;
   
   import junit.framework.Test;
   import junit.framework.TestSuite;
  @@ -63,121 +69,287 @@
    * Tests {@link org.apache.commons.lang.exception.ExceptionUtils}.
    *
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  - * @aithor <a href="mailto:steven@caswell.name">Steven Caswell</a>
  + * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
  + * @author Stephen Colebourne
    * @since 1.0
    */
  -public class ExceptionUtilsTestCase extends junit.framework.TestCase
  -{
  +public class ExceptionUtilsTestCase extends junit.framework.TestCase {
  +    
       private NestableException nested;
       private Throwable withCause;
       private Throwable withoutCause;
   
  -    public ExceptionUtilsTestCase(String name)
  -    {
  +    public ExceptionUtilsTestCase(String name) {
           super(name);
       }
  -    
  -    public static Test suite()
  -    {
  +
  +    public static Test suite() {
           return new TestSuite(ExceptionUtilsTestCase.class);
       }
   
  -    public void setUp()
  -    {
  +    public void setUp() {
           withoutCause = createExceptionWithoutCause();
           nested = new NestableException(withoutCause);
           withCause = new ExceptionWithCause(nested);
       }
  -    
  -    private Throwable createExceptionWithoutCause(){
  +
  +    //-----------------------------------------------------------------------
  +    private Throwable createExceptionWithoutCause() {
           try {
               throw new ExceptionWithoutCause();
  -        }
  -        catch (Throwable t){
  +        } catch (Throwable t) {
               return t;
           }
       }
   
  -    private Throwable createExceptionWithCause(){
  +    private Throwable createExceptionWithCause() {
           try {
               try {
                   throw new ExceptionWithCause(createExceptionWithoutCause());
  -            }
  -            catch (Throwable t){
  +            } catch (Throwable t) {
                   throw new ExceptionWithCause(t);
               }
  -        }
  -        catch (Throwable t){
  +        } catch (Throwable t) {
               return t;
           }
  -    } 
  -    
  -    public void testGetCause()
  -    {
  -        assertNull(ExceptionUtils.getCause(withoutCause));
  -        assertTrue(ExceptionUtils.getCause(nested) == withoutCause);
  -        assertTrue(ExceptionUtils.getCause(withCause) == nested);
       }
   
  -    public void testGetRootCause()
  -    {
  -        assertNull(ExceptionUtils.getRootCause(withoutCause));
  -        assertTrue(ExceptionUtils.getRootCause(withCause) == withoutCause);
  -        assertTrue(ExceptionUtils.getRootCause(withCause) == withoutCause);
  +    //-----------------------------------------------------------------------
  +    public void testGetCause_Throwable() {
  +        assertSame(null, ExceptionUtils.getCause(null));
  +        assertSame(null, ExceptionUtils.getCause(withoutCause));
  +        assertSame(withoutCause, ExceptionUtils.getCause(nested));
  +        assertSame(nested, ExceptionUtils.getCause(withCause));
  +    }
  +
  +    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]));
  +        assertSame(null, ExceptionUtils.getCause(withCause, new String[] {null}));
  +        assertSame(nested, ExceptionUtils.getCause(withCause, new String[] {"getCause"}));
  +        
  +        // not known type, so match on supplied method names
  +        assertSame(null, ExceptionUtils.getCause(withoutCause, null));
  +        assertSame(null, ExceptionUtils.getCause(withoutCause, new String[0]));
  +        assertSame(null, ExceptionUtils.getCause(withoutCause, new String[] {null}));
  +        assertSame(null, ExceptionUtils.getCause(withoutCause, new String[] {"getCause"}));
  +        assertSame(null, ExceptionUtils.getCause(withoutCause, new String[] {"getTargetException"}));
  +    }
  +
  +    public void testGetRootCause_Throwable() {
  +        assertSame(null, ExceptionUtils.getRootCause(null));
  +        assertSame(null, ExceptionUtils.getRootCause(withoutCause));
  +        assertSame(withoutCause, ExceptionUtils.getRootCause(nested));
  +        assertSame(withoutCause, ExceptionUtils.getRootCause(withCause));
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    public void testIsThrowableNested() {
  +        if (SystemUtils.isJavaVersionAtLeast(140)) {
  +            assertEquals(true, ExceptionUtils.isThrowableNested());
  +        } else {
  +            assertEquals(false, ExceptionUtils.isThrowableNested());
  +        }
  +    }
  +    
  +    public void testIsNestedThrowable_Throwable() {
  +        assertEquals(true, ExceptionUtils.isNestedThrowable(new SQLException()));
  +        assertEquals(true, ExceptionUtils.isNestedThrowable(new InvocationTargetException(new Exception())));
  +        assertEquals(true, ExceptionUtils.isNestedThrowable(new NestableRuntimeException()));
  +        assertEquals(true, ExceptionUtils.isNestedThrowable(withCause));
  +        assertEquals(true, ExceptionUtils.isNestedThrowable(nested));
  +        if (SystemUtils.isJavaVersionAtLeast(140)) {
  +            assertEquals(true, ExceptionUtils.isNestedThrowable(withoutCause));
  +            assertEquals(true, ExceptionUtils.isNestedThrowable(new Throwable()));
  +        } else {
  +            assertEquals(false, ExceptionUtils.isNestedThrowable(withoutCause));
  +            assertEquals(false, ExceptionUtils.isNestedThrowable(new Throwable()));
  +        }
       }
   
  -    public void testGetThrowableCount()
  -    {
  -        assertEquals(ExceptionUtils.getThrowableCount(null), 0);
  +    //-----------------------------------------------------------------------
  +    public void testGetThrowableCount_Throwable() {
  +        assertEquals(0, ExceptionUtils.getThrowableCount(null));
  +        assertEquals(1, ExceptionUtils.getThrowableCount(withoutCause));
  +        assertEquals(2, ExceptionUtils.getThrowableCount(nested));
  +        assertEquals(3, ExceptionUtils.getThrowableCount(withCause));
  +    }
  +
  +    public void testGetThrowables_Throwable() {
  +        assertEquals(0, ExceptionUtils.getThrowables(null).length);
  +        assertEquals(1, ExceptionUtils.getThrowables(withoutCause).length);
  +        assertSame(withoutCause, ExceptionUtils.getThrowables(withoutCause)[0]);
  +        
  +        assertEquals(2, ExceptionUtils.getThrowables(nested).length);
  +        assertSame(nested, ExceptionUtils.getThrowables(nested)[0]);
  +        assertSame(withoutCause, ExceptionUtils.getThrowables(nested)[1]);
  +        
  +        assertEquals(3, ExceptionUtils.getThrowables(withCause).length);
  +        assertSame(withCause, ExceptionUtils.getThrowables(withCause)[0]);
  +        assertSame(nested, ExceptionUtils.getThrowables(withCause)[1]);
  +        assertSame(withoutCause, ExceptionUtils.getThrowables(withCause)[2]);
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    public void testIndexOf_ThrowableClass() {
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(null, null));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(null, NestableException.class));
  +        
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, null));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithCause.class));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, NestableException.class));
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithoutCause.class));
  +        
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, null));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithCause.class));
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(nested, NestableException.class));
  +        assertEquals(1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithoutCause.class));
  +        
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, null));
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class));
  +        assertEquals(1, ExceptionUtils.indexOfThrowable(withCause, NestableException.class));
  +        assertEquals(2, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithoutCause.class));
  +    }
  +
  +    public void testIndexOf_ThrowableClassInt() {
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(null, null, 0));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(null, NestableException.class, 0));
  +        
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, null));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithCause.class, 0));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, NestableException.class, 0));
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithoutCause.class, 0));
  +        
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, null, 0));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithCause.class, 0));
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(nested, NestableException.class, 0));
  +        assertEquals(1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithoutCause.class, 0));
  +        
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, null));
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 0));
  +        assertEquals(1, ExceptionUtils.indexOfThrowable(withCause, NestableException.class, 0));
  +        assertEquals(2, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithoutCause.class, 0));
  +
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, -1));
  +        assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 0));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 1));
  +        assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 9));
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    public void testPrintRootCauseStackTrace_Throwable() throws Exception {
  +        ExceptionUtils.printRootCauseStackTrace(null);
  +        // could pipe system.err to a known stream, but not much point as
  +        // internally this method calls stram method anyway
  +    }
  +    
  +    public void testPrintRootCauseStackTrace_ThrowableStream() throws Exception {
  +        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
  +        ExceptionUtils.printRootCauseStackTrace(null, (PrintStream) null);
  +        ExceptionUtils.printRootCauseStackTrace(null, new PrintStream(out));
  +        assertEquals(0, out.toString().length());
  +        
  +        out = new ByteArrayOutputStream(1024);
  +        try {
  +            ExceptionUtils.printRootCauseStackTrace(withCause, (PrintStream) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {
  +        }
  +        
  +        out = new ByteArrayOutputStream(1024);
  +        Throwable withCause = createExceptionWithCause();
  +        ExceptionUtils.printRootCauseStackTrace(withCause, new PrintStream(out));
  +        String stackTrace = out.toString();
  +        assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) != -1);
  +        
  +        out = new ByteArrayOutputStream(1024);
  +        ExceptionUtils.printRootCauseStackTrace(withoutCause, new PrintStream(out));
  +        stackTrace = out.toString();
  +        assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) == -1);
       }
   
  -    public void testPrintThrowables()
  -        throws Exception
  -    {
  +    public void testPrintRootCauseStackTrace_ThrowableWriter() throws Exception {
           StringWriter writer = new StringWriter(1024);
  +        ExceptionUtils.printRootCauseStackTrace(null, (PrintWriter) null);
  +        ExceptionUtils.printRootCauseStackTrace(null, new PrintWriter(writer));
  +        assertEquals(0, writer.getBuffer().length());
  +        
  +        writer = new StringWriter(1024);
  +        try {
  +            ExceptionUtils.printRootCauseStackTrace(withCause, (PrintWriter) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {
  +        }
  +        
  +        writer = new StringWriter(1024);
           Throwable withCause = createExceptionWithCause();
  -        ExceptionUtils.printRootCauseStackTrace(withCause, 
  -            new PrintWriter(writer));
  +        ExceptionUtils.printRootCauseStackTrace(withCause, new PrintWriter(writer));
           String stackTrace = writer.toString();
  -        assertTrue("printRootCauseStackTrace(Throwable, PrintWriter) failed",
  -                   stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) != -1);
  +        assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) != -1);
  +        
           writer = new StringWriter(1024);
  -        ExceptionUtils.printRootCauseStackTrace(withoutCause, 
  -            new PrintWriter(writer));
  +        ExceptionUtils.printRootCauseStackTrace(withoutCause, new PrintWriter(writer));
           stackTrace = writer.toString();
  -        assertTrue("printRootCauseStackTrace(Throwable, PrintWriter) failed",
  -                   stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) == -1);
  +        assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) == -1);
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    public void testGetRootCauseStackTrace_Throwable() throws Exception {
  +        assertEquals(0, ExceptionUtils.getRootCauseStackTrace(null).length);
  +        
  +        Throwable withCause = createExceptionWithCause();
  +        String[] stackTrace = ExceptionUtils.getRootCauseStackTrace(withCause);
  +        boolean match = false;
  +        for (int i = 0; i < stackTrace.length; i++) {
  +            if (stackTrace[i].startsWith(ExceptionUtils.WRAPPED_MARKER)) {
  +                match = true;
  +                break;
  +            }
  +        }
  +        assertEquals(true, match);
  +        
  +        stackTrace = ExceptionUtils.getRootCauseStackTrace(withoutCause);
  +        match = false;
  +        for (int i = 0; i < stackTrace.length; i++) {
  +            if (stackTrace[i].startsWith(ExceptionUtils.WRAPPED_MARKER)) {
  +                match = true;
  +                break;
  +            }
  +        }
  +        assertEquals(false, match);
       }
   
  -    public void testIsNestedThrowable() {
  -        assertTrue("SQLException not nested", 
  -                   ExceptionUtils.isNestedThrowable(new java.sql.SQLException() ) );
  -        assertTrue("InvocationTargetException not nested", 
  -                   ExceptionUtils.isNestedThrowable(new java.lang.reflect.InvocationTargetException( new Exception() ) ) );
  -        assertTrue("NestableRuntimeException not nested", 
  -                   ExceptionUtils.isNestedThrowable(new NestableRuntimeException() ) );
  -                   
  -        // TODO: Come up with a way to test if java.lang.Throwable is nested.
  -        // bearing in mind that in JDK 1.4 it is, and in 1.3 and previous 
  -        // it isn't.
  +    public void testRemoveCommonFrames_ListList() throws Exception {
  +        try {
  +            ExceptionUtils.removeCommonFrames(null, null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {
  +        }
       }
       
  +    //-----------------------------------------------------------------------
       /**
        * Provides a method with a well known chained/nested exception
        * name which matches the full signature (e.g. has a return value
        * of <code>Throwable</code>.
        */
  -    private static class ExceptionWithCause extends Exception
  -    {
  +    private static class ExceptionWithCause extends Exception {
           private Throwable cause;
   
  -        public ExceptionWithCause(Throwable cause)
  -        {
  +        public ExceptionWithCause(Throwable cause) {
               this.cause = cause;
           }
   
  -        public Throwable getCause()
  -        {
  +        public Throwable getCause() {
               return cause;
           }
       }
  @@ -187,10 +359,9 @@
        * name which does not match the full signature (e.g. lacks a
        * return value of <code>Throwable</code>.
        */
  -    private static class ExceptionWithoutCause extends Exception
  -    {
  -        public void getTargetException()
  -        {
  +    private static class ExceptionWithoutCause extends Exception {
  +        public void getTargetException() {
           }
       }
  +    
   }
  
  
  

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