FileDocCategorySizeDatePackage
SerializationTester.javaAPI DocAndroid 1.5 API7684Wed May 06 22:41:06 BST 2009tests.util

SerializationTester

public class SerializationTester extends Object
This class simplifies the serialization test.

Fields Summary
private static Object
lastOutput
Constructors Summary
private SerializationTester()


    /*
     * -------------------------------------------------------------------
     * Constructors
     * -------------------------------------------------------------------
     */

      

    
Methods Summary
public static booleanassertCompabilityEquals(java.lang.Object obj, java.lang.String fileName)
Tests the serialization compatibility with reference for instance objects.

param
obj the object to be checked
param
fileName the serialization output file generated by reference
return
true if compatible, otherwise false
throws
Exception If any occurs.

        return obj.equals(readObject(obj, fileName));
    
public static booleanassertCompabilitySame(java.lang.Object obj, java.lang.String fileName)
Tests the serialization compatibility with reference const objects.

param
obj the object to be checked
param
fileName the serialization output file generated by reference
return
true if compatible, otherwise false
throws
Exception If any occurs.

        return obj == readObject(obj, fileName);
    
public static booleanassertEquals(java.lang.Object inputObject)
Tests the serialization and deserialization of instance objects.

param
inputObject An object
return
true if the deserialized object is equal to the input object, otherwise false
throws
Exception If any occurs.

        return inputObject.equals(getDeserilizedObject(inputObject));
    
public static booleanassertSame(java.lang.Object inputObject)
Tests the serialization and deserialization of const objects.

param
inputObject A const object
return
true if the deserialized object is the same as the input object, otherwise false
throws
Exception If any occurs.

        return inputObject == getDeserilizedObject(inputObject);
    
public static java.lang.ObjectgetDeserilizedObject(java.lang.Object inputObject)
Serialize an object and then deserialize it.

param
inputObject the input object
return
the deserialized object

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(inputObject);
        oos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Object outputObject = ois.readObject();
        lastOutput = outputObject;
        ois.close();
        return outputObject;
    
public static java.lang.ObjectgetLastOutput()
Gets the last deserialized object.

return
the last deserialized object

        return lastOutput;
    
public static voidmain(java.lang.String[] args)

    
public static java.lang.ObjectreadObject(java.lang.Object obj, java.lang.String fileName)
Deserialize an object from a file.

param
obj the object to be serialized if no serialization file is found
param
fileName the serialization file
return
the deserialized object
throws
Exception If any occurs.

        InputStream input = null;
        ObjectInputStream oinput = null;
        URL url = SerializationTester.class.getResource(
                fileName);
        if (null == url) {
            // serialization file does not exist, create one in the current dir
            writeObject(obj, new File(fileName).getName());
            throw new Error(
                    "Serialization file does not exist, created in the current dir.");
        }
        input = url.openStream();
        try {
            oinput = new ObjectInputStream(input);
            Object newObj = oinput.readObject();
            return newObj;
        } finally {
            try {
                if (null != oinput) {
                    oinput.close();
                }
            } catch (Exception e) {
                // ignore
            }
            try {
                if (null != input) {
                    input.close();
                }
            } catch (Exception e) {
                // ignore
            }
        }
    
public static voidwriteObject(java.lang.Object obj, java.lang.String fileName)

        // String path = SerializationTester.class.getResource(".").getPath();
        // if (path.endsWith(".")) {
        // path = path.substring(0, path.length() - 1);
        // }
        // if (!path.endsWith("/")) {
        // path += "/";
        // }
        // path += fileName;
        // System.out.println(path);
        OutputStream output = null;
        ObjectOutputStream ooutput = null;
        try {
            output = new FileOutputStream(fileName);
            ooutput = new ObjectOutputStream(output);
            ooutput.writeObject(obj);
        } finally {
            try {
                if (null != ooutput) {
                    ooutput.close();
                }
            } catch (Exception e) {
                // ignore
            }
            try {
                if (null != output) {
                    output.close();
                }
            } catch (Exception e) {
                // ignore
            }
        }