de.smartics.util.test.io
Class SerialTestUtils

Package class diagram package SerialTestUtils
java.lang.Object
  extended by de.smartics.util.test.io.SerialTestUtils

public final class SerialTestUtils
     
     
extends Object

Provides utility functions to help on tests dealing with serialization.

The example shows the typical usage of runSerialization(Object):

Example Usage Error

final MyClass uut = new MyClass(); ...
SerialTestUtils.runSerialization(uut);
assertTrue("Serialization successful.", true);

If the serialization is not successful, an exception is thrown. If you do not want an error to be reported by JUnit you have catch the exception and fail.

Example Usage Failure

final MyClass uut = new MyClass(); ...
SerialTestUtils.runSerializationQuietly(uut);
assertTrue("Serialization successful.", true);

Author:
Robert Reiner

Method Summary
static
<T> T
readObject(byte[] data)
          Reads the object from the given byte array.
static
<T> T
runSerialization(T uut)
          Test the serialization and deserialization of the unit under test.
static
<T> T
runSerializationQuietly(T uut)
          Runs the serialization test and signals a fail to the JUnit runtime.
static
<T> byte[]
writeObject(T uut)
          Writes the given object to an byte array.
static
<T> byte[]
writeObject(T uut, int bufferSize)
          Writes the given object to an byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

runSerializationQuietly

public static <T> T runSerializationQuietly(T uut)
Runs the serialization test and signals a fail to the JUnit runtime.

Type Parameters:
T - the type of the instance to read and write.
Parameters:
uut - the unit under test to serialize and deserialized.
Returns:
the serialized and deserialized object. Allows the client to check the internals if required.

runSerialization

public static <T> T runSerialization(T uut)
                          throws IOException,
                                 ClassNotFoundException
Test the serialization and deserialization of the unit under test.

Type Parameters:
T - the type of the instance to read and write.
Parameters:
uut - the unit under test to serialize and deserialized.
Returns:
the serialized and deserialized object. Allows the client to check the internals if required.
Throws:
IOException - if the object cannot be serialized or deserialized.
ClassNotFoundException - if the class for the deserialized object cannot be found.

readObject

public static <T> T readObject(byte[] data)
                    throws IOException,
                           ClassNotFoundException
Reads the object from the given byte array.

Type Parameters:
T - the type of the instance to read.
Parameters:
data - the serialized object to deserialize.
Returns:
the deserialized object.
Throws:
IOException - if the object cannot be deserialized.
ClassNotFoundException - if the class for the deserialized object cannot be found.

writeObject

public static <T> byte[] writeObject(T uut,
                                     int bufferSize)
                          throws IOException
Writes the given object to an byte array.

Type Parameters:
T - the type of the instance to write.
Parameters:
uut - the unit under test to serialize.
bufferSize - the size of the in-memory buffer to write to.
Returns:
the serialized object in a byte array.
Throws:
IOException - if the object cannot be serialized.

writeObject

public static <T> byte[] writeObject(T uut)
                          throws IOException
Writes the given object to an byte array. The size of the in-memory buffer is set to 1024 bytes.

Type Parameters:
T - the type of the instance to write.
Parameters:
uut - the unit under test to serialize.
Returns:
the serialized object in a byte array.
Throws:
IOException - if the object cannot be serialized.


Copyright © 2008-2012 Kronseder & Reiner GmbH - smartics. All Rights Reserved.