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  *
33  * @author Mike Bostock
34  * @since 10.0
35  */
36 @Beta
37 @GwtCompatible // but no-op!
38 public final class SerializableTester {
SerializableTester()39   private SerializableTester() {}
40 
41   /**
42    * Serializes and deserializes the specified object.
43    *
44    * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as proper GWT
45    * serialization tests require more setup. This no-op behavior allows test authors to intersperse
46    * {@code SerializableTester} calls with other, GWT-compatible tests.
47    *
48    * <p>Note that the specified object may not be known by the compiler to be a {@link
49    * java.io.Serializable} instance, and is thus declared an {@code Object}. For example, it might
50    * be declared as a {@code List}.
51    *
52    * @return the re-serialized object
53    * @throws RuntimeException if the specified object was not successfully serialized or
54    *     deserialized
55    */
56   @SuppressWarnings("unchecked")
reserialize(T object)57   public static <T> T reserialize(T object) {
58     return Platform.reserialize(object);
59   }
60 
61   /**
62    * Serializes and deserializes the specified object and verifies that the re-serialized object is
63    * equal to the provided object, that the hashcodes are identical, and that the class of the
64    * re-serialized object is identical to that of the original.
65    *
66    * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as proper GWT
67    * serialization tests require more setup. This no-op behavior allows test authors to intersperse
68    * {@code SerializableTester} calls with other, GWT-compatible tests.
69    *
70    * <p>Note that the specified object may not be known by the compiler to be a {@link
71    * java.io.Serializable} instance, and is thus declared an {@code Object}. For example, it might
72    * be declared as a {@code List}.
73    *
74    * <p>Note also that serialization is not in general required to return an object that is
75    * {@linkplain Object#equals equal} to the original, nor is it required to return even an object
76    * of the same class. For example, if sublists of {@code MyList} instances were serializable,
77    * those sublists might implement a private {@code MySubList} type but serialize as a plain {@code
78    * MyList} to save space. So long as {@code MyList} has all the public supertypes of {@code
79    * MySubList}, this is safe. For these cases, for which {@code reserializeAndAssert} is too
80    * strict, use {@link #reserialize}.
81    *
82    * @return the re-serialized object
83    * @throws RuntimeException if the specified object was not successfully serialized or
84    *     deserialized
85    * @throws AssertionFailedError if the re-serialized object is not equal to the original object,
86    *     or if the hashcodes are different.
87    */
reserializeAndAssert(T object)88   public static <T> T reserializeAndAssert(T object) {
89     T copy = reserialize(object);
90     new EqualsTester().addEqualityGroup(object, copy).testEquals();
91     Assert.assertEquals(object.getClass(), copy.getClass());
92     return copy;
93   }
94 }
95