1/*
2 * Copyright (C) 2009 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
17package com.google.common.testing;
18
19import junit.framework.AssertionFailedError;
20import junit.framework.TestCase;
21
22import java.io.Serializable;
23
24/**
25 * Tests for {@link SerializableTester}.
26 *
27 * @author Nick Kralevich
28 */
29public class SerializableTesterTest extends TestCase {
30  public void testStringAssertions() {
31    String original = "hello world";
32    String copy = SerializableTester.reserializeAndAssert(original);
33    assertEquals(original, copy);
34    assertNotSame(original, copy);
35  }
36
37  public void testClassWhichDoesNotImplementEquals() {
38    ClassWhichDoesNotImplementEquals orig =
39        new ClassWhichDoesNotImplementEquals();
40    boolean errorNotThrown = false;
41    try {
42      SerializableTester.reserializeAndAssert(orig);
43      errorNotThrown = true;
44    } catch (AssertionFailedError error) {
45      // expected
46      assertContains("must be Object#equals to", error.getMessage());
47    }
48    assertFalse(errorNotThrown);
49  }
50
51  public void testClassWhichIsAlwaysEqualButHasDifferentHashcodes() {
52    ClassWhichIsAlwaysEqualButHasDifferentHashcodes orig =
53        new ClassWhichIsAlwaysEqualButHasDifferentHashcodes();
54    boolean errorNotThrown = false;
55    try {
56      SerializableTester.reserializeAndAssert(orig);
57      errorNotThrown = true;
58    } catch (AssertionFailedError error) {
59      // expected
60      assertContains("must be equal to the Object#hashCode", error.getMessage());
61    }
62    assertFalse(errorNotThrown);
63  }
64
65  public void testObjectWhichIsEqualButChangesClass() {
66    ObjectWhichIsEqualButChangesClass orig =
67        new ObjectWhichIsEqualButChangesClass();
68    boolean errorNotThrown = false;
69    try {
70      SerializableTester.reserializeAndAssert(orig);
71      errorNotThrown = true;
72    } catch (AssertionFailedError error) {
73      // expected
74      assertContains("expected:<class ", error.getMessage());
75    }
76    assertFalse(errorNotThrown);
77  }
78
79  private static class ClassWhichDoesNotImplementEquals
80      implements Serializable {
81    private static final long serialVersionUID = 1L;
82  }
83
84  private static class ClassWhichIsAlwaysEqualButHasDifferentHashcodes
85      implements Serializable {
86    private static final long serialVersionUID = 2L;
87
88    @Override
89    public boolean equals(Object other) {
90      return (other instanceof ClassWhichIsAlwaysEqualButHasDifferentHashcodes);
91    }
92  }
93
94  private static class ObjectWhichIsEqualButChangesClass
95      implements Serializable {
96    private static final long serialVersionUID = 1L;
97
98    @Override
99    public boolean equals(Object other) {
100      return (other instanceof ObjectWhichIsEqualButChangesClass
101          || other instanceof OtherForm);
102    }
103
104    @Override
105    public int hashCode() {
106      return 1;
107    }
108
109    private Object writeReplace() {
110      return new OtherForm();
111    }
112
113    private static class OtherForm implements Serializable {
114      @Override
115      public boolean equals(Object other) {
116        return (other instanceof ObjectWhichIsEqualButChangesClass
117            || other instanceof OtherForm);
118      }
119
120      @Override
121      public int hashCode() {
122        return 1;
123      }
124    }
125  }
126
127  private static void assertContains(String expectedSubstring, String actual) {
128    // TODO(kevinb): use a Truth assertion here
129    if (!actual.contains(expectedSubstring)) {
130      fail("expected <" + actual + "> to contain <" + expectedSubstring + ">");
131    }
132  }
133}
134