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  @SuppressWarnings("unchecked") // varargs
38  public void testPairwiseEquivalent() {
39    EquivalenceTester.of(Equivalence.equals().<String>pairwise())
40        .addEquivalenceGroup(ImmutableList.<String>of())
41        .addEquivalenceGroup(ImmutableList.of("a"))
42        .addEquivalenceGroup(ImmutableList.of("b"))
43        .addEquivalenceGroup(ImmutableList.of("a", "b"), ImmutableList.of("a", "b"))
44        .test();
45  }
46
47  public void testPairwiseEquivalent_equals() {
48    new EqualsTester()
49        .addEqualityGroup(Equivalence.equals().pairwise(), Equivalence.equals().pairwise())
50        .addEqualityGroup(Equivalence.identity().pairwise())
51        .testEquals();
52  }
53
54  private enum LengthFunction implements Function<String, Integer> {
55    INSTANCE;
56
57    @Override public Integer apply(String input) {
58      return input.length();
59    }
60  }
61
62  private static final Equivalence<String> LENGTH_EQUIVALENCE = Equivalence.equals()
63      .onResultOf(LengthFunction.INSTANCE);
64
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(
72            LENGTH_EQUIVALENCE.wrap("hi"),
73            LENGTH_EQUIVALENCE.wrap("yo"))
74        .addEqualityGroup(
75            LENGTH_EQUIVALENCE.wrap(null),
76            LENGTH_EQUIVALENCE.wrap(null))
77        .addEqualityGroup(Equivalence.equals().wrap("hello"))
78        .addEqualityGroup(Equivalence.equals().wrap(null))
79        .testEquals();
80  }
81
82  public void testWrap_get() {
83    String test = "test";
84    Wrapper<String> wrapper = LENGTH_EQUIVALENCE.wrap(test);
85    assertSame(test, wrapper.get());
86  }
87
88  @GwtIncompatible("SerializableTester")
89  public void testSerialization() {
90    SerializableTester.reserializeAndAssert(LENGTH_EQUIVALENCE.wrap("hello"));
91    SerializableTester.reserializeAndAssert(Equivalence.equals());
92    SerializableTester.reserializeAndAssert(Equivalence.identity());
93  }
94
95  private static class IntValue {
96    private final int value;
97
98    IntValue(int value) {
99      this.value = value;
100    }
101
102    @Override public String toString() {
103      return "value = " + value;
104    }
105  }
106
107  public void testOnResultOf() {
108    EquivalenceTester.of(Equivalence.equals().onResultOf(Functions.toStringFunction()))
109        .addEquivalenceGroup(new IntValue(1), new IntValue(1))
110        .addEquivalenceGroup(new IntValue(2))
111        .test();
112  }
113
114  public void testOnResultOf_equals() {
115    new EqualsTester()
116        .addEqualityGroup(
117            Equivalence.identity().onResultOf(Functions.toStringFunction()),
118            Equivalence.identity().onResultOf(Functions.toStringFunction()))
119        .addEqualityGroup(Equivalence.equals().onResultOf(Functions.toStringFunction()))
120        .addEqualityGroup(Equivalence.identity().onResultOf(Functions.identity()))
121        .testEquals();
122  }
123
124  public void testEquivalentTo() {
125    Predicate<Object> equalTo1 = Equivalence.equals().equivalentTo("1");
126    assertTrue(equalTo1.apply("1"));
127    assertFalse(equalTo1.apply("2"));
128    assertFalse(equalTo1.apply(null));
129    Predicate<Object> isNull = Equivalence.equals().equivalentTo(null);
130    assertFalse(isNull.apply("1"));
131    assertFalse(isNull.apply("2"));
132    assertTrue(isNull.apply(null));
133
134    new EqualsTester()
135        .addEqualityGroup(equalTo1, Equivalence.equals().equivalentTo("1"))
136        .addEqualityGroup(isNull)
137        .addEqualityGroup(Equivalence.identity().equivalentTo("1"))
138        .testEquals();
139  }
140  public void testEqualsEquivalent() {
141    EquivalenceTester.of(Equivalence.equals())
142        .addEquivalenceGroup(new Integer(42), 42)
143        .addEquivalenceGroup("a")
144        .test();
145  }
146
147  public void testIdentityEquivalent() {
148    EquivalenceTester.of(Equivalence.identity())
149        .addEquivalenceGroup(new Integer(42))
150        .addEquivalenceGroup(new Integer(42))
151        .addEquivalenceGroup("a")
152        .test();
153  }
154
155  public void testEquals() {
156    new EqualsTester()
157        .addEqualityGroup(Equivalence.equals(), Equivalence.equals())
158        .addEqualityGroup(Equivalence.identity(), Equivalence.identity())
159        .testEquals();
160  }
161
162  @GwtIncompatible("NullPointerTester")
163  public void testNulls() {
164    new NullPointerTester().testAllPublicStaticMethods(Equivalence.class);
165    new NullPointerTester().testAllPublicInstanceMethods(Equivalence.equals());
166    new NullPointerTester().testAllPublicInstanceMethods(Equivalence.identity());
167  }
168}
169