commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Downey <steve.dow...@netfolio.com>
Subject Re: cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/exception NestableExceptionTestCase.java
Date Wed, 09 Oct 2002 11:38:29 GMT
For tests that fail on exception, the preferred method of handling them is to 
let the exception propagate outward. 
ie:
public void testSerialization() throws IOException {
}

Only handle the exception if the test fails if an exception is NOT thrown.

The stack trace from the exception allows much faster location of the cause of 
failure than fail(), and a number of test frameworks take advantage of it.


On Wednesday 09 October 2002 01:28 am, sullis@apache.org wrote:
> sullis      2002/10/08 22:28:53
>
>   Modified:    lang/src/test/org/apache/commons/lang/exception
>                         NestableExceptionTestCase.java
>   Log:
>   added test code for serialization / deserialization
>
>   Revision  Changes    Path
>   1.6       +66 -1    
> jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableExc
>eptionTestCase.java
>
>   Index: NestableExceptionTestCase.java
>   ===================================================================
>   RCS file:
> /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/N
>estableExceptionTestCase.java,v retrieving revision 1.5
>   retrieving revision 1.6
>   diff -u -r1.5 -r1.6
>   --- NestableExceptionTestCase.java	11 Sep 2002 19:00:52 -0000	1.5
>   +++ NestableExceptionTestCase.java	9 Oct 2002 05:28:53 -0000	1.6
>   @@ -55,8 +55,12 @@
>     */
>
>    import java.io.ByteArrayOutputStream;
>   +import java.io.ByteArrayInputStream;
>    import java.io.PrintStream;
>    import java.io.PrintWriter;
>   +import java.io.ObjectOutputStream;
>   +import java.io.ObjectInputStream;
>   +import java.io.Serializable;
>
>    import junit.framework.*;
>    import junit.textui.TestRunner;
>   @@ -242,6 +246,67 @@
>            return Exception.class;
>        }
>
>   +    public void testSerialization()
>   +    {
>   +    	RuntimeException nestedEx = new RuntimeException("nested exception
> message"); +    	NestableExceptionTester1 ex = new
> NestableExceptionTester1("serialization test", nestedEx); +
>   +		assertTrue( "implements java.io.Serializable", nestedEx instanceof
> java.io.Serializable); +
>   +		assertTrue( "implements java.io.Serializable", ex instanceof
> java.io.Serializable); +
>   +		ByteArrayOutputStream baos = new ByteArrayOutputStream();
>   +		ByteArrayInputStream bais = null;
>   +		ObjectOutputStream oos = null;
>   +		ObjectInputStream ois = null;
>   +
>   +		try
>   +		{
>   +			oos = new ObjectOutputStream(baos);
>   +			oos.writeObject(ex);
>   +			oos.flush();
>   +			bais = new ByteArrayInputStream(baos.toByteArray());
>   +			ois = new ObjectInputStream(bais);
>   +			NestableExceptionTester1 deserializedEx = (NestableExceptionTester1)
> ois.readObject(); +			assertEquals(
>   +					"getThrowableCount() return value",
>   +						ex.getThrowableCount(),
>   +						deserializedEx.getThrowableCount());
>   +
>   +			for (int i = 0; i < ex.getThrowableCount(); i++)
>   +			{
>   +				Throwable t = ex.getThrowable(i);
>   +				Throwable deserializedThrowable = deserializedEx.getThrowable(i);
>   +
>   +				assertEquals( t.getClass(),
>   +						deserializedThrowable.getClass());
>   +
>   +				assertEquals(
>   +					t.getMessage(),
>   +					deserializedThrowable.getMessage());
>   +			}
>   +		}
>   +		catch (Exception caughtEx)
>   +		{
>   +			fail("an unexpected exception occurred: "
>   +					+ caughtEx.toString());
>   +		}
>   +		finally
>   +		{
>   +			if (null != oos)
>   +			{
>   +				try
>   +				{
>   +					oos.close();
>   +				}
>   +				catch (Exception ignored)
>   +				{
>   +					// intentionally empty
>   +				}
>   +			}
>   +		}
>   +
>   +    }
>    }
>
>    /**


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


Mime
View raw message