From 8ed194eb1991834f7528c657e280e1e5666a2ba1 Mon Sep 17 00:00:00 2001 From: Steven Caswell Date: Mon, 22 Aug 2005 00:54:27 +0000 Subject: [PATCH] increase SerializationUtils test coverage as reported by clover git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@234406 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/lang/SerializationUtilsTest.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/test/org/apache/commons/lang/SerializationUtilsTest.java b/src/test/org/apache/commons/lang/SerializationUtilsTest.java index 1189799e1..2d9fdfb9a 100644 --- a/src/test/org/apache/commons/lang/SerializationUtilsTest.java +++ b/src/test/org/apache/commons/lang/SerializationUtilsTest.java @@ -17,8 +17,12 @@ package org.apache.commons.lang; 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 @@ import junit.textui.TestRunner; * @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 @@ public class SerializationUtilsTest extends TestCase { } 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 @@ public class SerializationUtilsTest extends TestCase { 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 @@ public class SerializationUtilsTest extends TestCase { } } + +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