public abstract class SerializationTest
extends TestCase
Modifier and Type | Class and Description |
---|---|
static interface |
SerializationTest.SerializableAssert
Interface to compare (de)serialized objects
Should be implemented if a class under test does not provide specific
equals() method and it's instances should to be compared manually.
|
Modifier and Type | Field and Description |
---|---|
static String |
GOLDEN_PATH
Key to a system property defining root location of golden files.
|
static SerializationTest.SerializableAssert |
PERMISSION_COLLECTION_COMPARATOR
Comparator for PermissionCollection objects
|
static SerializationTest.SerializableAssert |
SAME_COMPARATOR
Comparator for verifying that deserialized object is the same as initial.
|
static SerializationTest.SerializableAssert |
THROWABLE_COMPARATOR
Comparator for Throwable objects
|
Constructor and Description |
---|
SerializationTest() |
Modifier and Type | Method and Description |
---|---|
static Serializable |
copySerializable(Serializable initial)
Copies an object by serializing/deserializing it.
|
static void |
createGoldenFile(String root,
TestCase test,
Object object)
Creates golden file.
|
static SerializationTest.SerializableAssert |
defineComparator(TestCase test,
Object object)
Returns
comparator for provided serializable
object . |
protected abstract Object[] |
getData()
This is the main working method of this framework.
|
protected File |
getDataFile(int index)
Returns golden file for an object being tested.
|
static Serializable |
getObjectFromStream(InputStream is)
Deserializes single object from an input stream.
|
protected void |
produceGoldenFiles()
Working method for files generation mode.
|
static void |
putObjectToStream(Object obj,
OutputStream os)
Serializes specified object to an output stream.
|
void |
testGolden()
Tests that data objects can be deserialized from golden files, to verify
compatibility with Reference Implementation.
|
void |
testSelf()
Tests that data objects can be serialized and deserialized without
exceptions, and that deserialization really produces deeply cloned
objects.
|
static void |
verifyGolden(TestCase test,
Object object)
Verifies that object deserialized from golden file correctly.
|
static void |
verifyGolden(TestCase test,
Object[] objects)
Verifies that objects from array deserialized from golden files
correctly.
|
static void |
verifyGolden(TestCase test,
Object[] objects,
SerializationTest.SerializableAssert comparator)
Verifies that objects from array deserialized from golden files
correctly.
|
static void |
verifyGolden(TestCase test,
Object object,
SerializationTest.SerializableAssert comparator)
Verifies that object deserialized from golden file correctly.
|
static void |
verifySelf(Object object)
Verifies that object can be smoothly serialized/deserialized.
|
static void |
verifySelf(Object[] objects)
Verifies that that objects from array can be smoothly
serialized/deserialized.
|
static void |
verifySelf(Object[] objects,
SerializationTest.SerializableAssert comparator)
Verifies that that objects from array can be smoothly
serialized/deserialized.
|
static void |
verifySelf(Object object,
SerializationTest.SerializableAssert comparator)
Verifies that object can be smoothly serialized/deserialized.
|
public static final String GOLDEN_PATH
public static final SerializationTest.SerializableAssert SAME_COMPARATOR
public static final SerializationTest.SerializableAssert THROWABLE_COMPARATOR
public static final SerializationTest.SerializableAssert PERMISSION_COLLECTION_COMPARATOR
protected abstract Object[] getData()
public void testSelf() throws Throwable
Throwable
public void testGolden() throws Throwable
Throwable
protected File getDataFile(int index)
index
- array index of tested data (as returned by
getData()
)protected void produceGoldenFiles() throws IOException
getData()
to golden files, each object to
a separate file.IOException
public static void putObjectToStream(Object obj, OutputStream os) throws IOException
IOException
public static Serializable getObjectFromStream(InputStream is) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
public static SerializationTest.SerializableAssert defineComparator(TestCase test, Object object) throws Exception
comparator
for provided serializable
object
.
The comparator
is searched in the following order: test
implements SerializableAssert interface then it is
selected as comparator.object
has class in its classes hierarchy that overrides equals(Object)
method then DEFAULT_COMPARATOR
is selected.object's
class,for example, if passed object
is instance of
Throwable then THROWABLE_COMPARATOR
is used.test
- - test caseobject
- - object to be comparedException
public static void verifyGolden(TestCase test, Object object) throws Exception
test
- - test caseobject
- - to be comparedException
public static void verifyGolden(TestCase test, Object object, SerializationTest.SerializableAssert comparator) throws Exception
testName
.golden.ser" resource file
from "testPackage
"
folder, reads an object from the loaded file and compares it with
object
using specified comparator
.test
- - test caseobject
- - to be comparedcomparator
- - for comparing (de)serialized objectsException
public static void verifyGolden(TestCase test, Object[] objects) throws Exception
test
- - test caseobjects
- - array of objects to be comparedException
public static void verifyGolden(TestCase test, Object[] objects, SerializationTest.SerializableAssert comparator) throws Exception
testName
.golden.N
.ser"
resource files from "testPackage
"
folder, from each loaded file it reads an object from and compares it
with corresponding object in provided array (i.e. objects[N]
)
using specified comparator
. (N
is index
in object's array.)test
- - test caseobjects
- - array of objects to be comparedcomparator
- - for comparing (de)serialized objectsException
public static void verifySelf(Object object) throws Exception
object
- - to be serialized/deserializedException
public static void verifySelf(Object object, SerializationTest.SerializableAssert comparator) throws Exception
object
and compare it
with initial object
.object
- - object to be serialized/deserializedcomparator
- - for comparing serialized/deserialized object with initial objectException
public static void verifySelf(Object[] objects) throws Exception
objects
- - array of objects to be serialized/deserializedException
public static void verifySelf(Object[] objects, SerializationTest.SerializableAssert comparator) throws Exception
objects
array and compare it with initial object.objects
- - array of objects to be serialized/deserializedcomparator
- - for comparing serialized/deserialized object with initial objectException
public static void createGoldenFile(String root, TestCase test, Object object) throws IOException
root + test's package name
.
The file name is: test's name + "golden.ser"
root
- - root directory for serialization resource filestest
- - test caseobject
- - to be serializedIOException
- - if I/O errorpublic static Serializable copySerializable(Serializable initial) throws IOException, ClassNotFoundException
initial
- - an object to be copiedIOException
ClassNotFoundException