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