You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by st...@apache.org on 2005/08/22 02:54:30 UTC

svn commit: r234406 - /jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/SerializationUtilsTest.java

Author: stevencaswell
Date: Sun Aug 21 17:54:27 2005
New Revision: 234406

URL: http://svn.apache.org/viewcvs?rev=234406&view=rev
Log:
increase SerializationUtils test coverage as reported by clover

Modified:
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/SerializationUtilsTest.java

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/SerializationUtilsTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/SerializationUtilsTest.java?rev=234406&r1=234405&r2=234406&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/SerializationUtilsTest.java (original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/SerializationUtilsTest.java Sun Aug 21 17:54:27 2005
@@ -17,8 +17,12 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
 import java.util.HashMap;
@@ -36,6 +40,10 @@
  * @version $Id$
  */
 public class SerializationUtilsTest extends TestCase {
+
+  static final String CLASS_NOT_FOUND_MESSAGE = "ClassNotFoundSerializationTest.readObject fake exception";
+    protected static final String SERIALIZE_IO_EXCEPTION_MESSAGE = "Anonymous OutputStream I/O exception";
+  
     private String iString;
     private Integer iInteger;
     private HashMap iMap;
@@ -166,6 +174,22 @@
         }
         fail();
     }
+    
+    public void testSerializeIOException() throws Exception {
+        // forces an IOException when the ObjectOutputStream is created, to test not closing the stream
+        // in the finally block
+        OutputStream streamTest = new OutputStream() {
+            public void write(int arg0) throws IOException {
+                throw new IOException(SERIALIZE_IO_EXCEPTION_MESSAGE);
+            }
+        };
+        try {
+            SerializationUtils.serialize(iMap, streamTest);
+        }
+        catch(SerializationException e) {
+            assertEquals("java.io.IOException: " + SERIALIZE_IO_EXCEPTION_MESSAGE, e.getMessage());
+        }
+    }
 
     //-----------------------------------------------------------------------
 
@@ -219,6 +243,21 @@
         fail();
     }
 
+    public void testDeserializeStreamClassNotFound() throws Exception {
+        ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(streamReal);
+        oos.writeObject(new ClassNotFoundSerializationTest());
+        oos.flush();
+        oos.close();
+
+        ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray());
+        try {
+            Object test = SerializationUtils.deserialize(inTest);
+        } catch(SerializationException se) {
+            assertEquals("java.lang.ClassNotFoundException: " + CLASS_NOT_FOUND_MESSAGE, se.getMessage());
+        }
+    }
+    
     //-----------------------------------------------------------------------
 
     public void testSerializeBytes() throws Exception {
@@ -344,3 +383,11 @@
     }
 
 }
+
+class ClassNotFoundSerializationTest implements Serializable
+{
+
+    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException    {
+        throw new ClassNotFoundException(SerializationUtilsTest.CLASS_NOT_FOUND_MESSAGE);
+    }
+}
\ No newline at end of file



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