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:
parent
40e8865efe
commit
8ed194eb19
|
@ -17,8 +17,12 @@ package org.apache.commons.lang;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -36,6 +40,10 @@ import junit.textui.TestRunner;
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class SerializationUtilsTest extends TestCase {
|
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 String iString;
|
||||||
private Integer iInteger;
|
private Integer iInteger;
|
||||||
private HashMap iMap;
|
private HashMap iMap;
|
||||||
|
@ -167,6 +175,22 @@ public class SerializationUtilsTest extends TestCase {
|
||||||
fail();
|
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 {
|
public void testDeserializeStream() throws Exception {
|
||||||
|
@ -219,6 +243,21 @@ public class SerializationUtilsTest extends TestCase {
|
||||||
fail();
|
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 {
|
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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue