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.annotations.J2ktIncompatible; 22 import com.google.common.base.Equivalence.Wrapper; 23 import com.google.common.collect.ImmutableList; 24 import com.google.common.testing.EqualsTester; 25 import com.google.common.testing.EquivalenceTester; 26 import com.google.common.testing.NullPointerTester; 27 import com.google.common.testing.SerializableTester; 28 import junit.framework.TestCase; 29 import org.checkerframework.checker.nullness.qual.Nullable; 30 31 /** 32 * Unit test for {@link Equivalence}. 33 * 34 * @author Jige Yu 35 */ 36 @ElementTypesAreNonnullByDefault 37 @GwtCompatible(emulated = true) 38 public class EquivalenceTest extends TestCase { testPairwiseEquivalent()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 testPairwiseEquivalent_equals()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 apply(String input)59 public Integer apply(String input) { 60 return input.length(); 61 } 62 } 63 64 private static final Equivalence<String> LENGTH_EQUIVALENCE = 65 Equivalence.equals().onResultOf(LengthFunction.INSTANCE); 66 testWrap()67 public void testWrap() { 68 new EqualsTester() 69 .addEqualityGroup( 70 LENGTH_EQUIVALENCE.wrap("hello"), 71 LENGTH_EQUIVALENCE.wrap("hello"), 72 LENGTH_EQUIVALENCE.wrap("world")) 73 .addEqualityGroup(LENGTH_EQUIVALENCE.wrap("hi"), LENGTH_EQUIVALENCE.wrap("yo")) 74 .addEqualityGroup( 75 LENGTH_EQUIVALENCE.<@Nullable String>wrap(null), 76 LENGTH_EQUIVALENCE.<@Nullable String>wrap(null)) 77 .addEqualityGroup(Equivalence.equals().wrap("hello")) 78 .addEqualityGroup(Equivalence.equals().<@Nullable Object>wrap(null)) 79 .testEquals(); 80 } 81 testWrap_get()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 @J2ktIncompatible 89 @GwtIncompatible // SerializableTester testSerialization()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 IntValue(int value)99 IntValue(int value) { 100 this.value = value; 101 } 102 103 @Override toString()104 public String toString() { 105 return "value = " + value; 106 } 107 } 108 testOnResultOf()109 public void testOnResultOf() { 110 EquivalenceTester.of(Equivalence.equals().onResultOf(Functions.toStringFunction())) 111 .addEquivalenceGroup(new IntValue(1), new IntValue(1)) 112 .addEquivalenceGroup(new IntValue(2)) 113 .test(); 114 } 115 testOnResultOf_equals()116 public void testOnResultOf_equals() { 117 new EqualsTester() 118 .addEqualityGroup( 119 Equivalence.identity().onResultOf(Functions.toStringFunction()), 120 Equivalence.identity().onResultOf(Functions.toStringFunction())) 121 .addEqualityGroup(Equivalence.equals().onResultOf(Functions.toStringFunction())) 122 .addEqualityGroup(Equivalence.identity().onResultOf(Functions.identity())) 123 .testEquals(); 124 } 125 testEquivalentTo()126 public void testEquivalentTo() { 127 Predicate<@Nullable Object> equalTo1 = Equivalence.equals().equivalentTo("1"); 128 assertTrue(equalTo1.apply("1")); 129 assertFalse(equalTo1.apply("2")); 130 assertFalse(equalTo1.apply(null)); 131 Predicate<@Nullable Object> isNull = Equivalence.equals().equivalentTo(null); 132 assertFalse(isNull.apply("1")); 133 assertFalse(isNull.apply("2")); 134 assertTrue(isNull.apply(null)); 135 136 new EqualsTester() 137 .addEqualityGroup(equalTo1, Equivalence.equals().equivalentTo("1")) 138 .addEqualityGroup(isNull) 139 .addEqualityGroup(Equivalence.identity().equivalentTo("1")) 140 .testEquals(); 141 } 142 143 /* 144 * We use large numbers to avoid the integer cache. Normally, we'd accomplish that merely by using 145 * `new Integer` (as we do) instead of `Integer.valueOf`. However, under J2KT, `new Integer` 146 * gets translated back to `Integer.valueOf` because that is the only thing J2KT can support. And 147 * anyway, it's nice to avoid `Integer.valueOf` because the Android toolchain optimizes multiple 148 * `Integer.valueOf` calls into one! So we stick with the deprecated `Integer` constructor. 149 */ 150 testEqualsEquivalent()151 public void testEqualsEquivalent() { 152 EquivalenceTester.of(Equivalence.equals()) 153 .addEquivalenceGroup(new Integer(42_000_000), 42_000_000) 154 .addEquivalenceGroup("a") 155 .test(); 156 } 157 testIdentityEquivalent()158 public void testIdentityEquivalent() { 159 EquivalenceTester.of(Equivalence.identity()) 160 .addEquivalenceGroup(new Integer(42_000_000)) 161 .addEquivalenceGroup(new Integer(42_000_000)) 162 .addEquivalenceGroup("a") 163 .test(); 164 } 165 testEquals()166 public void testEquals() { 167 new EqualsTester() 168 .addEqualityGroup(Equivalence.equals(), Equivalence.equals()) 169 .addEqualityGroup(Equivalence.identity(), Equivalence.identity()) 170 .testEquals(); 171 } 172 173 @J2ktIncompatible 174 @GwtIncompatible // NullPointerTester testNulls()175 public void testNulls() throws NoSuchMethodException { 176 NullPointerTester tester = new NullPointerTester(); 177 // Necessary until JDK15: 178 // https://bugs.openjdk.org/browse/JDK-8202469 179 tester.ignore(Equivalence.class.getMethod("wrap", Object.class)); 180 181 tester.testAllPublicStaticMethods(Equivalence.class); 182 tester.testAllPublicInstanceMethods(Equivalence.equals()); 183 tester.testAllPublicInstanceMethods(Equivalence.identity()); 184 } 185 } 186