fyi, you may have a little inefficiency when you have an expected exception.


@Test(expected=...)
public void dosomethingblowsup() throws Exception {
  dosomething();
}

is better than

@Test
public void dosomethingblowsup() throws Exception {
try {
  dosomething();
  fail();
} catch (...) {
  // expected
}
}

unless you have further asserts to make on the expected exception.

- Stephen

---
Sent from my Android phone, so random spelling mistakes, random nonsense
words and other nonsense are a direct result of using swype to type on the
screen
On 3 Aug 2011 17:16, <strub...@apache.org> wrote:
> Author: struberg
> Date: Wed Aug 3 16:16:23 2011
> New Revision: 1153562
>
> URL: http://svn.apache.org/viewvc?rev=1153562&view=rev
> Log:
> refine ExceptionUtilsTest
>
> almost done now!
>
> Added:
>
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestExceptionWithDetail.java
> - copied, changed from r1153297,
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException.java
> Modified:
>
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java
>
> Modified:
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java
> URL:
http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java?rev=1153562&r1=1153561&r2=1153562&view=diff
>
==============================================================================
> ---
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java
(original)
> +++
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExceptionUtilsTest.java
Wed Aug 3 16:16:23 2011
> @@ -21,11 +21,14 @@ package org.codehaus.plexus.util;
>
> import org.apache.maven.tck.FixPlexusBugs;
> import org.codehaus.plexus.util.exceptionutils.TestException;
> +import org.codehaus.plexus.util.exceptionutils.TestExceptionWithDetail;
> import org.junit.Rule;
> import org.junit.Test;
> import org.junit.Assert;
> import org.junit.matchers.JUnitMatchers;
>
> +import java.io.ByteArrayOutputStream;
> +import java.io.PrintStream;
> import java.lang.reflect.InvocationTargetException;
> import java.sql.SQLException;
> import java.util.List;
> @@ -245,7 +248,7 @@ public class ExceptionUtilsTest extends
> // NPE safe test
> try
> {
> - ExceptionUtils.getStackTrace( (Throwable) null );
> + ExceptionUtils.getStackTrace((Throwable) null);
> fail( "getStackTrace(null) NPE expected" );
> }
> catch ( NullPointerException e )
> @@ -274,7 +277,7 @@ public class ExceptionUtilsTest extends
> // NPE safe test
> try
> {
> - ExceptionUtils.getStackFrames( (Throwable) null );
> + ExceptionUtils.getStackFrames((Throwable) null);
> fail( "getStackFrames(null) NPE expected" );
> }
> catch ( NullPointerException e )
> @@ -315,7 +318,7 @@ public class ExceptionUtilsTest extends
>
> String[] stackFrames = ExceptionUtils.getStackFrames( stackTrace );
> assertNotNull( stackFrames );
> - assertEquals( 23, stackFrames.length );
> + assertEquals(23, stackFrames.length);
>
> assertEquals( "java.lang.NullPointerException: mymessage", stackFrames[0]
);
> assertThat( "stackFrames", stackFrames[1]
> @@ -339,8 +342,26 @@ public class ExceptionUtilsTest extends
> @Test
> public void testGetThrowableCount()
> {
> - //X TODO refine test!
> - logger.warning("TODO implement!");
> + NullPointerException npe = new NullPointerException( "dooh just a
random, nullpointer" );
> + SQLException sqlException = new SQLException( npe );
> + TestException testException = new TestException();
> + testException.setSourceException( sqlException );
> +
> + assertThat( "getThrowableCount"
> + , ExceptionUtils.getThrowableCount( npe )
> + , is( 1 ));
> +
> + assertThat( "getThrowableCount"
> + , ExceptionUtils.getThrowableCount( sqlException )
> + , is( 2 ));
> +
> + assertThat( "getThrowableCount"
> + , ExceptionUtils.getThrowableCount( testException )
> + , is( 3 ));
> +
> + // NPE safe test
> + // this method should NOT throw a NPE on a null argument!
> + ExceptionUtils.getThrowableCount( null );
> }
>
> /**
> @@ -350,18 +371,109 @@ public class ExceptionUtilsTest extends
> @Test
> public void testIndexOfThrowable()
> {
> - //X TODO refine test!
> - logger.warning("TODO implement!");
> + NullPointerException npe = new NullPointerException( "dooh just a
random, nullpointer" );
> + SQLException sqlException = new SQLException( npe );
> + TestException testException = new TestException();
> + testException.setSourceException( sqlException );
> +
> + assertThat("indexOfThrowable"
> + , ExceptionUtils.indexOfThrowable(npe, NullPointerException.class)
> + , is(0));
> +
> + assertThat( "indexOfThrowable for non contained Exception type"
> + , ExceptionUtils.indexOfThrowable( npe, SQLException.class )
> + , is( -1 ));
> +
> +
> + assertThat( "indexOfThrowable"
> + , ExceptionUtils.indexOfThrowable( testException,
NullPointerException.class )
> + , is( 2 ));
> +
> + assertThat( "indexOfThrowable for non contained Exception type"
> + , ExceptionUtils.indexOfThrowable( testException, SQLException.class )
> + , is( 1 ));
> +
> + assertThat( "indexOfThrowable"
> + , ExceptionUtils.indexOfThrowable( testException, TestException.class )
> + , is( 0 ));
> +
> +
> + // tests for indexOfThrowable with start index param
> + assertThat( "indexOfThrowable"
> + , ExceptionUtils.indexOfThrowable( testException,
NullPointerException.class, 2 )
> + , is( 2 ));
> +
> + assertThat( "indexOfThrowable"
> + , ExceptionUtils.indexOfThrowable( testException, SQLException.class, 2
)
> + , is( -1 ));
> +
> + try
> + {
> + ExceptionUtils.indexOfThrowable( testException, TestException.class, 3
);
> + fail( "indexOfThrowable with too large inces" );
> + }
> + catch ( IndexOutOfBoundsException e )
> + {
> + //nothing to do, Exception was expected
> + }
> +
> + // NPE safe tests
> + try
> + {
> + ExceptionUtils.indexOfThrowable( null, TestException.class );
> + fail( "indexOfThrowable(null, Exception.class) NPE expected" );
> + }
> + catch ( IndexOutOfBoundsException e )
> + {
> + //nothing to do, Exception was expected
> + }
> + assertThat( "indexOfThrowable for null Exception type"
> + , ExceptionUtils.indexOfThrowable(npe, null)
> + , is(-1));
> }
>
> /**
> + * Most probably this only ever returns false on null in JDK > 1.4
> + * Because Throwable itself nowadays has a getCause() method which
> + * is in the method list...
> + *
> * @see ExceptionUtils#isNestedThrowable(Throwable)
> */
> @Test
> public void testIsNestedThrowable()
> {
> - //X TODO refine test!
> - logger.warning("TODO implement!");
> + NullPointerException npe = new NullPointerException( "dooh just a
random, nullpointer" );
> + SQLException sqlException = new SQLException( npe );
> + TestException testException = new TestException();
> + testException.setSourceException( sqlException );
> +
> + assertThat( "isNestedThrowable"
> + , ExceptionUtils.isNestedThrowable( null )
> + , is( false ) );
> +
> + assertThat("isNestedThrowable"
> + , ExceptionUtils.isNestedThrowable(npe)
> + , is(true));
> +
> + assertThat( "isNestedThrowable"
> + , ExceptionUtils.isNestedThrowable( sqlException )
> + , is( true ) );
> +
> + assertThat( "isNestedThrowable"
> + , ExceptionUtils.isNestedThrowable( new InvocationTargetException( npe )
)
> + , is( true ) );
> +
> + assertThat( "isNestedThrowable"
> + , ExceptionUtils.isNestedThrowable( new TestExceptionWithDetail() )
> + , is( true ) );
> +
> + assertThat( "isNestedThrowable"
> + , ExceptionUtils.isNestedThrowable( new Exception() )
> + , is( true ) );
> +
> + assertThat( "isNestedThrowable"
> + , ExceptionUtils.isNestedThrowable( new Throwable() )
> + , is( true ) );
> }
>
> /**
> @@ -372,8 +484,32 @@ public class ExceptionUtilsTest extends
> @Test
> public void testPrintRootCauseStackTrace()
> {
> - //X TODO refine test!
> - logger.warning("TODO implement!");
> + NullPointerException npe = new NullPointerException( "dooh just a
random, nullpointer" );
> + SQLException sqlException = new SQLException( npe );
> + TestException testException = new TestException();
> + testException.setSourceException( sqlException );
> +
> + ByteArrayOutputStream bao = new ByteArrayOutputStream();
> + PrintStream outStream = new PrintStream( bao );
> + PrintStream originalErr = System.err;
> +
> + try
> + {
> + System.setErr( outStream );
> + ExceptionUtils.printRootCauseStackTrace( npe );
> +
> + assertThat( "stackFrames"
> + , bao.toString()
> + , JUnitMatchers.containsString( "java.lang.NullPointerException: dooh
just a random, nullpointer"
> + + "\n\tat org.codehaus.plexus.util.ExceptionUtilsTest."
> + + "testPrintRootCauseStackTrace(ExceptionUtilsTest.java:" ) );
> + }
> + finally
> + {
> + System.setErr( originalErr );
> + }
> +
> + //X TODO A FEW THINGS STILL MISSING! will continue later today...
> }
>
> }
>
> Copied:
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestExceptionWithDetail.java
(from r1153297,
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException.java)
> URL:
http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestExceptionWithDetail.java?p2=maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestExceptionWithDetail.java&p1=maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException.java&r1=1153297&r2=1153562&rev=1153562&view=diff
>
==============================================================================
> ---
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException.java
(original)
> +++
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestExceptionWithDetail.java
Wed Aug 3 16:16:23 2011
> @@ -23,36 +23,26 @@ package org.codehaus.plexus.util.excepti
>
>
> /**
> + * This test exception has a 'detail' field.
> *
> * @author <a href="mailto:strub...@yahoo.de";>Mark Struberg</a>
> */
> -public class TestException extends Exception
> +public class TestExceptionWithDetail extends Exception
> {
> - private Throwable cause;
> - private Throwable specialCause;
> + private Throwable detail;
>
> - public TestException()
> + public TestExceptionWithDetail()
> {
> super();
> }
>
> - public void setSourceException( Throwable cause )
> + public Throwable getDetail()
> {
> - this.cause = cause;
> + return detail;
> }
>
> - public Throwable getSourceException()
> + public void setDetail( Throwable detail )
> {
> - return cause;
> - }
> -
> - public Throwable getSpecialCause()
> - {
> - return specialCause;
> - }
> -
> - public void setSpecialCause( Throwable specialCause )
> - {
> - this.specialCause = specialCause;
> + this.detail = detail;
> }
> }
>
>

Reply via email to