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