Class SerializableTester


  • @Beta
    @GwtCompatible
    public final class SerializableTester
    extends Object
    Tests serialization and deserialization of an object, optionally asserting that the resulting object is equal to the original.

    GWT warning: Under GWT, both methods simply returns their input, as proper GWT serialization tests require more setup. This no-op behavior allows test authors to intersperse SerializableTester calls with other, GWT-compatible tests.

    Since:
    10.0
    Author:
    Mike Bostock
    • Method Detail

      • reserialize

        public static <T> T reserialize​(T object)
        Serializes and deserializes the specified object.

        GWT warning: Under GWT, this method simply returns its input, as proper GWT serialization tests require more setup. This no-op behavior allows test authors to intersperse SerializableTester calls with other, GWT-compatible tests.

        Note that the specified object may not be known by the compiler to be a Serializable instance, and is thus declared an Object. For example, it might be declared as a List.

        Returns:
        the re-serialized object
        Throws:
        RuntimeException - if the specified object was not successfully serialized or deserialized
      • reserializeAndAssert

        public static <T> T reserializeAndAssert​(T object)
        Serializes and deserializes the specified object and verifies that the re-serialized object is equal to the provided object, that the hashcodes are identical, and that the class of the re-serialized object is identical to that of the original.

        GWT warning: Under GWT, this method simply returns its input, as proper GWT serialization tests require more setup. This no-op behavior allows test authors to intersperse SerializableTester calls with other, GWT-compatible tests.

        Note that the specified object may not be known by the compiler to be a Serializable instance, and is thus declared an Object. For example, it might be declared as a List.

        Note also that serialization is not in general required to return an object that is equal to the original, nor is it required to return even an object of the same class. For example, if sublists of MyList instances were serializable, those sublists might implement a private MySubList type but serialize as a plain MyList to save space. So long as MyList has all the public supertypes of MySubList, this is safe. For these cases, for which reserializeAndAssert is too strict, use reserialize(T).

        Returns:
        the re-serialized object
        Throws:
        RuntimeException - if the specified object was not successfully serialized or deserialized
        junit.framework.AssertionFailedError - if the re-serialized object is not equal to the original object, or if the hashcodes are different.