Coverage Summary for Class: SerializableTester (com.google.common.testing)

Class Class, % Method, % Line, %
SerializableTester 100% (1/1) 66.7% (2/3) 83.3% (5/6)


1 /* 2  * Copyright (C) 2007 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.testing; 18  19 import com.google.common.annotations.Beta; 20 import com.google.common.annotations.GwtCompatible; 21 import junit.framework.Assert; 22 import junit.framework.AssertionFailedError; 23  24 /** 25  * Tests serialization and deserialization of an object, optionally asserting that the resulting 26  * object is equal to the original. 27  * 28  * <p><b>GWT warning:</b> Under GWT, both methods simply returns their input, as proper GWT 29  * serialization tests require more setup. This no-op behavior allows test authors to intersperse 30  * {@code SerializableTester} calls with other, GWT-compatible tests. 31  * 32  * @author Mike Bostock 33  * @since 10.0 34  */ 35 @Beta 36 @GwtCompatible // but no-op! 37 public final class SerializableTester { 38  private SerializableTester() {} 39  40  /** 41  * Serializes and deserializes the specified object. 42  * 43  * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as proper GWT 44  * serialization tests require more setup. This no-op behavior allows test authors to intersperse 45  * {@code SerializableTester} calls with other, GWT-compatible tests. 46  * 47  * <p>Note that the specified object may not be known by the compiler to be a {@link 48  * java.io.Serializable} instance, and is thus declared an {@code Object}. For example, it might 49  * be declared as a {@code List}. 50  * 51  * @return the re-serialized object 52  * @throws RuntimeException if the specified object was not successfully serialized or 53  * deserialized 54  */ 55  @SuppressWarnings("unchecked") 56  public static <T> T reserialize(T object) { 57  return Platform.reserialize(object); 58  } 59  60  /** 61  * Serializes and deserializes the specified object and verifies that the re-serialized object is 62  * equal to the provided object, that the hashcodes are identical, and that the class of the 63  * re-serialized object is identical to that of the original. 64  * 65  * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as proper GWT 66  * serialization tests require more setup. This no-op behavior allows test authors to intersperse 67  * {@code SerializableTester} calls with other, GWT-compatible tests. 68  * 69  * <p>Note that the specified object may not be known by the compiler to be a {@link 70  * java.io.Serializable} instance, and is thus declared an {@code Object}. For example, it might 71  * be declared as a {@code List}. 72  * 73  * <p>Note also that serialization is not in general required to return an object that is 74  * {@linkplain Object#equals equal} to the original, nor is it required to return even an object 75  * of the same class. For example, if sublists of {@code MyList} instances were serializable, 76  * those sublists might implement a private {@code MySubList} type but serialize as a plain {@code 77  * MyList} to save space. So long as {@code MyList} has all the public supertypes of {@code 78  * MySubList}, this is safe. For these cases, for which {@code reserializeAndAssert} is too 79  * strict, use {@link #reserialize}. 80  * 81  * @return the re-serialized object 82  * @throws RuntimeException if the specified object was not successfully serialized or 83  * deserialized 84  * @throws AssertionFailedError if the re-serialized object is not equal to the original object, 85  * or if the hashcodes are different. 86  */ 87  public static <T> T reserializeAndAssert(T object) { 88  T copy = reserialize(object); 89  new EqualsTester().addEqualityGroup(object, copy).testEquals(); 90  Assert.assertEquals(object.getClass(), copy.getClass()); 91  return copy; 92  } 93 }