Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.testing;
 
 
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.

Author(s):
Mike Bostock
Since:
10.0
 
 @GwtCompatible // but no-op!
 public final class SerializableTester {
   private SerializableTester() {}

  
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 java.io.Serializable instance, and is thus declared an Object. For example, it might be declared as a List.

Returns:
the re-serialized object
Throws:
java.lang.RuntimeException if the specified object was not successfully serialized or deserialized
 
   @SuppressWarnings("unchecked")
   public static <T> T reserialize(T object) {
     return Platform.reserialize(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 java.io.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(java.lang.Object).

Returns:
the re-serialized object
Throws:
java.lang.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.
 
   public static <T> T reserializeAndAssert(T object) {
     T copy = reserialize(object);
     new EqualsTester()
         .addEqualityGroup(objectcopy)
         .testEquals();
     Assert.assertEquals(object.getClass(), copy.getClass());
    return copy;
  }
New to GrepCode? Check out our FAQ X