You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by su...@apache.org on 2002/10/09 07:28:53 UTC

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

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/NestableExceptionTestCase.java
  
  Index: NestableExceptionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java,v
  retrieving revision 1.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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


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

Posted by Steve Downey <st...@netfolio.com>.
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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>