You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Stephen Connolly <st...@gmail.com> on 2011/08/03 21:22:25 UTC

Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

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, <st...@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:struberg@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;
> }
> }
>
>

Re: RE: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

Posted by Stephen Connolly <st...@gmail.com>.
theorys or parameters tests. both would require a separate test class.

- 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 20:55, "Mark Struberg" <st...@yahoo.de> wrote:
> I'm learning something new every day ;)
>
> What I need to do:
>
> Assume you have a methodX with 2 params
>
> So I need to test
>
> 1.) methodX(a, null);
> 2.) methodX(null, b);
> 3.) methodX(null, null);
>
> And I don't like to write 3 extra methods for it.
> Is this possible without catching them separately?
>
> txs and LieGrue,
> strub
>
>
>
> --- On Wed, 8/3/11, Thiessen, Todd (Todd) <tt...@avaya.com> wrote:
>
>> From: Thiessen, Todd (Todd) <tt...@avaya.com>
>> Subject: RE: svn commit: r1153562 - in
/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util:
ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java
>> To: "Maven Developers List" <de...@maven.apache.org>
>> Cc: "commits@maven.apache.org" <co...@maven.apache.org>
>> Date: Wednesday, August 3, 2011, 7:37 PM
>> Better still would be to use the
>> @Rule annotation.  Ie:
>>
>> @Rule
>> public ExpectedException exepctedException =
>> ExpectedException.none();
>>
>> @Test
>> public void doSomethingThatBlowsup throws Exception
>> {
>>   expectedException.expect(SomeException.class);
>>   expectedException.expectMessage("Ensure message
>> contains this string");
>>   doSomething();
>> }
>>
>> The annotation is nice since you can check the string as
>> well. And if you really need, you can create a matcher to
>> check things like that cause of the exception etc...
>>
>> But this may need java 1.6.
>>
>> > -----Original Message-----
>> > From: Stephen Connolly [mailto:stephen.alan.connolly@gmail.com]
>> > Sent: Wednesday, August 03, 2011 3:22 PM
>> > To: dev@maven.apache.org
>> > Cc: commits@maven.apache.org
>> > Subject: Re: svn commit: r1153562 - in
>> /maven/sandbox/trunk/plexus-
>> > utils-commons-bridge/plexus-utils-
>> > tck/src/test/java/org/codehaus/plexus/util:
>> ExceptionUtilsTest.java
>> > exceptionutils/TestExceptionWithDetail.java
>> >
>> > 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, <st...@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/TestException
>> > WithDetail.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/TestException
>> > WithDetail.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/TestException
>> >
>> WithDetail.java?p2=maven/sandbox/trunk/plexus-utils-commons-
>> > bridge/plexus-utils-
>> >
>> tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException
>> >
>> WithDetail.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/TestException
>> > WithDetail.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:struberg@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;
>> > > }
>> > > }
>> > >
>> > >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

Posted by Kristian Rosenvold <kr...@gmail.com>.
I may have observed (in the corner of my eye) that surefire does not 
support @Rules too well, at least if you try
to change the result from success to failure or do other funky stuff.

According the the late Douglas Adams, this makes it a SEP, so I have not 
filed an issue ;)

Kristian


Den 03.08.2011 21:37, skrev Thiessen, Todd (Todd):
> Better still would be to use the @Rule annotation.  Ie:
>
> @Rule
> public ExpectedException exepctedException = ExpectedException.none();
>
> @Test
> public void doSomethingThatBlowsup throws Exception
> {
>    expectedException.expect(SomeException.class);
>    expectedException.expectMessage("Ensure message contains this string");
>    doSomething();
> }
>
> The annotation is nice since you can check the string as well. And if you really need, you can create a matcher to check things like that cause of the exception etc...
>
> But this may need java 1.6.
>
>> -----Original Message-----
>> From: Stephen Connolly [mailto:stephen.alan.connolly@gmail.com]
>> Sent: Wednesday, August 03, 2011 3:22 PM
>> To: dev@maven.apache.org
>> Cc: commits@maven.apache.org
>> Subject: Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-
>> utils-commons-bridge/plexus-utils-
>> tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java
>> exceptionutils/TestExceptionWithDetail.java
>>
>> 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,<st...@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/TestException
>> WithDetail.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/TestException
>> WithDetail.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/TestException
>> WithDetail.java?p2=maven/sandbox/trunk/plexus-utils-commons-
>> bridge/plexus-utils-
>> tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException
>> WithDetail.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/TestException
>> WithDetail.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:struberg@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;
>>> }
>>> }
>>>
>>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>


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


RE: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

Posted by Mark Struberg <st...@yahoo.de>.
I'm learning something new every day ;)

What I need to do:

Assume you have a methodX with 2 params

So I need to test

1.) methodX(a, null);
2.) methodX(null, b);
3.) methodX(null, null);

And I don't like to write 3 extra methods for it. 
Is this possible without catching them separately?

txs and LieGrue,
strub



--- On Wed, 8/3/11, Thiessen, Todd (Todd) <tt...@avaya.com> wrote:

> From: Thiessen, Todd (Todd) <tt...@avaya.com>
> Subject: RE: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java
> To: "Maven Developers List" <de...@maven.apache.org>
> Cc: "commits@maven.apache.org" <co...@maven.apache.org>
> Date: Wednesday, August 3, 2011, 7:37 PM
> Better still would be to use the
> @Rule annotation.  Ie:
> 
> @Rule
> public ExpectedException exepctedException =
> ExpectedException.none();
> 
> @Test
> public void doSomethingThatBlowsup throws Exception
> {
>   expectedException.expect(SomeException.class);
>   expectedException.expectMessage("Ensure message
> contains this string");
>   doSomething();
> }
> 
> The annotation is nice since you can check the string as
> well. And if you really need, you can create a matcher to
> check things like that cause of the exception etc...
> 
> But this may need java 1.6.
> 
> > -----Original Message-----
> > From: Stephen Connolly [mailto:stephen.alan.connolly@gmail.com]
> > Sent: Wednesday, August 03, 2011 3:22 PM
> > To: dev@maven.apache.org
> > Cc: commits@maven.apache.org
> > Subject: Re: svn commit: r1153562 - in
> /maven/sandbox/trunk/plexus-
> > utils-commons-bridge/plexus-utils-
> > tck/src/test/java/org/codehaus/plexus/util:
> ExceptionUtilsTest.java
> > exceptionutils/TestExceptionWithDetail.java
> > 
> > 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, <st...@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/TestException
> > WithDetail.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/TestException
> > WithDetail.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/TestException
> >
> WithDetail.java?p2=maven/sandbox/trunk/plexus-utils-commons-
> > bridge/plexus-utils-
> >
> tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException
> >
> WithDetail.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/TestException
> > WithDetail.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:struberg@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;
> > > }
> > > }
> > >
> > >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
> 
> 

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


RE: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

Posted by "Thiessen, Todd (Todd)" <tt...@avaya.com>.
Better still would be to use the @Rule annotation.  Ie:

@Rule
public ExpectedException exepctedException = ExpectedException.none();

@Test
public void doSomethingThatBlowsup throws Exception
{
  expectedException.expect(SomeException.class);
  expectedException.expectMessage("Ensure message contains this string");
  doSomething();
}

The annotation is nice since you can check the string as well. And if you really need, you can create a matcher to check things like that cause of the exception etc...

But this may need java 1.6.

> -----Original Message-----
> From: Stephen Connolly [mailto:stephen.alan.connolly@gmail.com]
> Sent: Wednesday, August 03, 2011 3:22 PM
> To: dev@maven.apache.org
> Cc: commits@maven.apache.org
> Subject: Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-
> utils-commons-bridge/plexus-utils-
> tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java
> exceptionutils/TestExceptionWithDetail.java
> 
> 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, <st...@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/TestException
> WithDetail.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/TestException
> WithDetail.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/TestException
> WithDetail.java?p2=maven/sandbox/trunk/plexus-utils-commons-
> bridge/plexus-utils-
> tck/src/test/java/org/codehaus/plexus/util/exceptionutils/TestException
> WithDetail.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/TestException
> WithDetail.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:struberg@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;
> > }
> > }
> >
> >

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


Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

Posted by Stephen Connolly <st...@gmail.com>.
we can switch to the full set of matchers by adding the dependencies

- 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 20:40, "Mark Struberg" <st...@yahoo.de> wrote:
> Txs Stephen, I just did it that way to not have 10s of different methods.
>
> Anything else I should do different in those tests?
>
> I had troubles to find enough hamcrest machters. E.g. a startsWith exists
in the hamcrest libs, but I could not find them in junit-4.8.2.
> Any tips?
>
> txs and LieGrue,
> strub
>
>
> --- On Wed, 8/3/11, Stephen Connolly <st...@gmail.com>
wrote:
>
>> From: Stephen Connolly <st...@gmail.com>
>> Subject: Re: svn commit: r1153562 - in
/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util:
ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java
>> To: dev@maven.apache.org
>> Cc: commits@maven.apache.org
>> Date: Wednesday, August 3, 2011, 7:22 PM
>> 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, <st...@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:struberg@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;
>> > }
>> > }
>> >
>> >
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

Posted by Baptiste MATHUS <ml...@batmat.net>.
Le 3 août 2011 21:40, "Mark Struberg" <st...@yahoo.de> a écrit :
>
> Txs Stephen, I just did it that way to not have 10s of different methods.
>
> Anything else I should do different in those tests?
>
> I had troubles to find enough hamcrest machters. E.g. a startsWith exists
in the hamcrest libs, but I could not find them in junit-4.8.2.

What about using fest-assert? I don't know hamcrest very well but I heard
many people saying fest-assert was even more.fluent. I feel the construct is
very natural. It really gives a fluent and simple-to-use api.
Just do a static import of fest-assert assertThat and you're done.
Example from the site:

List<Employee> newEmployees = employees.hired(TODAY);
assertThat(newEmployees).hasSize(6)
                        .contains(frodo, sam);

assertThat(yoda).isInstanceOf(Jedi.class)
         .isEqualTo(foundJedi)
         .isNotEqualTo(foundSith);

Cheers

-- Baptiste
Sent from my phone.

Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java

Posted by Mark Struberg <st...@yahoo.de>.
Txs Stephen, I just did it that way to not have 10s of different methods.

Anything else I should do different in those tests?

I had troubles to find enough hamcrest machters. E.g. a startsWith exists in the hamcrest libs, but I could not find them in junit-4.8.2.
Any tips?

txs and LieGrue,
strub


--- On Wed, 8/3/11, Stephen Connolly <st...@gmail.com> wrote:

> From: Stephen Connolly <st...@gmail.com>
> Subject: Re: svn commit: r1153562 - in /maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util: ExceptionUtilsTest.java exceptionutils/TestExceptionWithDetail.java
> To: dev@maven.apache.org
> Cc: commits@maven.apache.org
> Date: Wednesday, August 3, 2011, 7:22 PM
> 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, <st...@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:struberg@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;
> > }
> > }
> >
> >
> 

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