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