2
* Copyright (C) 2007 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.testing;
19
import com.google.common.annotations.GwtCompatible;
20
import com.google.errorprone.annotations.CanIgnoreReturnValue;
21
import junit.framework.Assert;
22
import junit.framework.AssertionFailedError;
25
* Tests serialization and deserialization of an object, optionally asserting that the resulting
26
* object is equal to the original.
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.
32
* @author Mike Bostock
35
@GwtCompatible // but no-op!
36
@ElementTypesAreNonnullByDefault
37
public final class SerializableTester {
38
private SerializableTester() {}
41
* Serializes and deserializes the specified object.
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.
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}.
51
* @return the re-serialized object
52
* @throws RuntimeException if the specified object was not successfully serialized or
56
public static <T> T reserialize(T object) {
57
return Platform.reserialize(object);
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.
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.
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}.
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}.
81
* @return the re-serialized object
82
* @throws RuntimeException if the specified object was not successfully serialized or
84
* @throws AssertionFailedError if the re-serialized object is not equal to the original object,
85
* or if the hashcodes are different.
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());