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