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
This commit is contained in:
Steven Caswell 2005-08-22 00:54:27 +00:00
parent 40e8865efe
commit 8ed194eb19
1 changed files with 47 additions and 0 deletions

View File

@ -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;
@ -167,6 +175,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());
}
}
//-----------------------------------------------------------------------
public void testDeserializeStream() throws Exception {
@ -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);
}
}