FileDocCategorySizeDatePackage
SerializationTestHelper.javaAPI DocApache log4j 1.2.154912Sat Aug 25 00:09:34 BST 2007org.apache.log4j.util

SerializationTestHelper

public class SerializationTestHelper extends Object
Utiities for serialization tests.
author
Curt Arnold

Fields Summary
Constructors Summary
private SerializationTestHelper()
Private constructor.

  
Methods Summary
public static voidassertSerializationEquals(java.lang.String witness, java.lang.Object obj, int[] skip, int endCompare)
Checks the serialization of an object against an file containing the expected serialization.

param
witness name of file containing expected serialization.
param
obj object to be serialized.
param
skip positions in serialized stream that should not be compared.
param
endCompare position to stop comparison.
throws
Exception thrown on IO or serialization exception.

    ByteArrayOutputStream memOut = new ByteArrayOutputStream();
    ObjectOutputStream objOut = new ObjectOutputStream(memOut);
    objOut.writeObject(obj);
    objOut.close();

    assertStreamEquals(witness, memOut.toByteArray(), skip, endCompare);
  
public static voidassertStreamEquals(java.lang.String witness, byte[] actual, int[] skip, int endCompare)
Asserts the serialized form of an object.

param
witness file name of expected serialization.
param
actual byte array of actual serialization.
param
skip positions to skip comparison.
param
endCompare position to stop comparison.
throws
IOException thrown on IO or serialization exception.

    File witnessFile = new File(witness);

    if (witnessFile.exists()) {
      int skipIndex = 0;
      byte[] expected = new byte[actual.length];
      FileInputStream is = new FileInputStream(witnessFile);
      int bytesRead = is.read(expected);
      is.close();

      if(bytesRead < endCompare) {
          TestCase.assertEquals(bytesRead, actual.length);
      }

      int endScan = actual.length;

      if (endScan > endCompare) {
        endScan = endCompare;
      }

      for (int i = 0; i < endScan; i++) {
        if ((skipIndex < skip.length) && (skip[skipIndex] == i)) {
          skipIndex++;
        } else {
          if (expected[i] != actual[i]) {
            TestCase.assertEquals(
              "Difference at offset " + i, expected[i], actual[i]);
          }
        }
      }
    } else {
      //
      //  if the file doesn't exist then
      //      assume that we are setting up and need to write it
      FileOutputStream os = new FileOutputStream(witnessFile);
      os.write(actual);
      os.close();
      TestCase.fail("Writing witness file " + witness);
    }
  
public static java.lang.ObjectdeserializeStream(java.lang.String witness)
Deserializes a specified file.

param
witness serialization file, may not be null.
return
deserialized object.
throws
Exception thrown on IO or deserialization exception.

    FileInputStream fileIs = new FileInputStream(witness);
    ObjectInputStream objIs = new ObjectInputStream(fileIs);

    return objIs.readObject();
  
public static java.lang.ObjectserializeClone(java.lang.Object obj)
Creates a clone by serializing object and deserializing byte stream.

param
obj object to serialize and deserialize.
return
clone
throws
IOException on IO error.
throws
ClassNotFoundException if class not found.

    ByteArrayOutputStream memOut = new ByteArrayOutputStream();
    ObjectOutputStream objOut = new ObjectOutputStream(memOut);
    objOut.writeObject(obj);
    objOut.close();

    ByteArrayInputStream src = new ByteArrayInputStream(memOut.toByteArray());
    ObjectInputStream objIs = new ObjectInputStream(src);

    return objIs.readObject();