• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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  * diOBJECTibuted under the License is diOBJECTibuted 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.base;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.base.Equivalence.Wrapper;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.testing.EqualsTester;
24 import com.google.common.testing.EquivalenceTester;
25 import com.google.common.testing.NullPointerTester;
26 import com.google.common.testing.SerializableTester;
27 import junit.framework.TestCase;
28 
29 /**
30  * Unit test for {@link Equivalence}.
31  *
32  * @author Jige Yu
33  */
34 @GwtCompatible(emulated = true)
35 public class EquivalenceTest extends TestCase {
36   @SuppressWarnings("unchecked") // varargs
testPairwiseEquivalent()37   public void testPairwiseEquivalent() {
38     EquivalenceTester.of(Equivalence.equals().<String>pairwise())
39         .addEquivalenceGroup(ImmutableList.<String>of())
40         .addEquivalenceGroup(ImmutableList.of("a"))
41         .addEquivalenceGroup(ImmutableList.of("b"))
42         .addEquivalenceGroup(ImmutableList.of("a", "b"), ImmutableList.of("a", "b"))
43         .test();
44   }
45 
testPairwiseEquivalent_equals()46   public void testPairwiseEquivalent_equals() {
47     new EqualsTester()
48         .addEqualityGroup(Equivalence.equals().pairwise(), Equivalence.equals().pairwise())
49         .addEqualityGroup(Equivalence.identity().pairwise())
50         .testEquals();
51   }
52 
53   private enum LengthFunction implements Function<String, Integer> {
54     INSTANCE;
55 
56     @Override
apply(String input)57     public Integer apply(String input) {
58       return input.length();
59     }
60   }
61 
62   private static final Equivalence<String> LENGTH_EQUIVALENCE =
63       Equivalence.equals().onResultOf(LengthFunction.INSTANCE);
64 
testWrap()65   public void testWrap() {
66     new EqualsTester()
67         .addEqualityGroup(
68             LENGTH_EQUIVALENCE.wrap("hello"),
69             LENGTH_EQUIVALENCE.wrap("hello"),
70             LENGTH_EQUIVALENCE.wrap("world"))
71         .addEqualityGroup(LENGTH_EQUIVALENCE.wrap("hi"), LENGTH_EQUIVALENCE.wrap("yo"))
72         .addEqualityGroup(LENGTH_EQUIVALENCE.wrap(null), LENGTH_EQUIVALENCE.wrap(null))
73         .addEqualityGroup(Equivalence.equals().wrap("hello"))
74         .addEqualityGroup(Equivalence.equals().wrap(null))
75         .testEquals();
76   }
77 
testWrap_get()78   public void testWrap_get() {
79     String test = "test";
80     Wrapper<String> wrapper = LENGTH_EQUIVALENCE.wrap(test);
81     assertSame(test, wrapper.get());
82   }
83 
84   @GwtIncompatible // SerializableTester
testSerialization()85   public void testSerialization() {
86     SerializableTester.reserializeAndAssert(LENGTH_EQUIVALENCE.wrap("hello"));
87     SerializableTester.reserializeAndAssert(Equivalence.equals());
88     SerializableTester.reserializeAndAssert(Equivalence.identity());
89   }
90 
91   private static class IntValue {
92     private final int value;
93 
IntValue(int value)94     IntValue(int value) {
95       this.value = value;
96     }
97 
98     @Override
toString()99     public String toString() {
100       return "value = " + value;
101     }
102   }
103 
testOnResultOf()104   public void testOnResultOf() {
105     EquivalenceTester.of(Equivalence.equals().onResultOf(Functions.toStringFunction()))
106         .addEquivalenceGroup(new IntValue(1), new IntValue(1))
107         .addEquivalenceGroup(new IntValue(2))
108         .test();
109   }
110 
testOnResultOf_equals()111   public void testOnResultOf_equals() {
112     new EqualsTester()
113         .addEqualityGroup(
114             Equivalence.identity().onResultOf(Functions.toStringFunction()),
115             Equivalence.identity().onResultOf(Functions.toStringFunction()))
116         .addEqualityGroup(Equivalence.equals().onResultOf(Functions.toStringFunction()))
117         .addEqualityGroup(Equivalence.identity().onResultOf(Functions.identity()))
118         .testEquals();
119   }
120 
testEquivalentTo()121   public void testEquivalentTo() {
122     Predicate<Object> equalTo1 = Equivalence.equals().equivalentTo("1");
123     assertTrue(equalTo1.apply("1"));
124     assertFalse(equalTo1.apply("2"));
125     assertFalse(equalTo1.apply(null));
126     Predicate<Object> isNull = Equivalence.equals().equivalentTo(null);
127     assertFalse(isNull.apply("1"));
128     assertFalse(isNull.apply("2"));
129     assertTrue(isNull.apply(null));
130 
131     new EqualsTester()
132         .addEqualityGroup(equalTo1, Equivalence.equals().equivalentTo("1"))
133         .addEqualityGroup(isNull)
134         .addEqualityGroup(Equivalence.identity().equivalentTo("1"))
135         .testEquals();
136   }
137 
testEqualsEquivalent()138   public void testEqualsEquivalent() {
139     EquivalenceTester.of(Equivalence.equals())
140         .addEquivalenceGroup(new Integer(42), 42)
141         .addEquivalenceGroup("a")
142         .test();
143   }
144 
testIdentityEquivalent()145   public void testIdentityEquivalent() {
146     EquivalenceTester.of(Equivalence.identity())
147         .addEquivalenceGroup(new Integer(42))
148         .addEquivalenceGroup(new Integer(42))
149         .addEquivalenceGroup("a")
150         .test();
151   }
152 
testEquals()153   public void testEquals() {
154     new EqualsTester()
155         .addEqualityGroup(Equivalence.equals(), Equivalence.equals())
156         .addEqualityGroup(Equivalence.identity(), Equivalence.identity())
157         .testEquals();
158   }
159 
160   @GwtIncompatible // NullPointerTester
testNulls()161   public void testNulls() {
162     new NullPointerTester().testAllPublicStaticMethods(Equivalence.class);
163     new NullPointerTester().testAllPublicInstanceMethods(Equivalence.equals());
164     new NullPointerTester().testAllPublicInstanceMethods(Equivalence.identity());
165   }
166 }
167