001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import com.google.common.annotations.GwtCompatible;
020import junit.framework.Assert;
021import junit.framework.AssertionFailedError;
022
023/**
024 * Tests serialization and deserialization of an object, optionally asserting that the resulting
025 * object is equal to the original.
026 *
027 * <p><b>GWT warning:</b> Under GWT, both methods simply returns their input, as proper GWT
028 * serialization tests require more setup. This no-op behavior allows test authors to intersperse
029 * {@code SerializableTester} calls with other, GWT-compatible tests.
030 *
031 * @author Mike Bostock
032 * @since 10.0
033 */
034@GwtCompatible // but no-op!
035public final class SerializableTester {
036  private SerializableTester() {}
037
038  /**
039   * Serializes and deserializes the specified object.
040   *
041   * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as proper GWT
042   * serialization tests require more setup. This no-op behavior allows test authors to intersperse
043   * {@code SerializableTester} calls with other, GWT-compatible tests.
044   *
045   * <p>Note that the specified object may not be known by the compiler to be a {@link
046   * java.io.Serializable} instance, and is thus declared an {@code Object}. For example, it might
047   * be declared as a {@code List}.
048   *
049   * @return the re-serialized object
050   * @throws RuntimeException if the specified object was not successfully serialized or
051   *     deserialized
052   */
053  public static <T> T reserialize(T object) {
054    return Platform.reserialize(object);
055  }
056
057  /**
058   * Serializes and deserializes the specified object and verifies that the re-serialized object is
059   * equal to the provided object, that the hashcodes are identical, and that the class of the
060   * re-serialized object is identical to that of the original.
061   *
062   * <p><b>GWT warning:</b> Under GWT, this method simply returns its input, as proper GWT
063   * serialization tests require more setup. This no-op behavior allows test authors to intersperse
064   * {@code SerializableTester} calls with other, GWT-compatible tests.
065   *
066   * <p>Note that the specified object may not be known by the compiler to be a {@link
067   * java.io.Serializable} instance, and is thus declared an {@code Object}. For example, it might
068   * be declared as a {@code List}.
069   *
070   * <p>Note also that serialization is not in general required to return an object that is
071   * {@linkplain Object#equals equal} to the original, nor is it required to return even an object
072   * of the same class. For example, if sublists of {@code MyList} instances were serializable,
073   * those sublists might implement a private {@code MySubList} type but serialize as a plain {@code
074   * MyList} to save space. So long as {@code MyList} has all the public supertypes of {@code
075   * MySubList}, this is safe. For these cases, for which {@code reserializeAndAssert} is too
076   * strict, use {@link #reserialize}.
077   *
078   * @return the re-serialized object
079   * @throws RuntimeException if the specified object was not successfully serialized or
080   *     deserialized
081   * @throws AssertionFailedError if the re-serialized object is not equal to the original object,
082   *     or if the hashcodes are different.
083   */
084  public static <T> T reserializeAndAssert(T object) {
085    T copy = reserialize(object);
086    new EqualsTester().addEqualityGroup(object, copy).testEquals();
087    Assert.assertEquals(object.getClass(), copy.getClass());
088    return copy;
089  }
090}