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 30 /** 31 * Unit test for {@link Equivalence}. 32 * 33 * @author Jige Yu 34 */ 35 @ElementTypesAreNonnullByDefault 36 @GwtCompatible(emulated = true) 37 public class EquivalenceTest extends TestCase { 38 @SuppressWarnings("unchecked") // varargs 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(LENGTH_EQUIVALENCE.wrap(null), LENGTH_EQUIVALENCE.wrap(null)) 75 .addEqualityGroup(Equivalence.equals().wrap("hello")) 76 .addEqualityGroup(Equivalence.equals().wrap(null)) 77 .testEquals(); 78 } 79 testWrap_get()80 public void testWrap_get() { 81 String test = "test"; 82 Wrapper<String> wrapper = LENGTH_EQUIVALENCE.wrap(test); 83 assertSame(test, wrapper.get()); 84 } 85 86 @J2ktIncompatible 87 @GwtIncompatible // SerializableTester testSerialization()88 public void testSerialization() { 89 SerializableTester.reserializeAndAssert(LENGTH_EQUIVALENCE.wrap("hello")); 90 SerializableTester.reserializeAndAssert(Equivalence.equals()); 91 SerializableTester.reserializeAndAssert(Equivalence.identity()); 92 } 93 94 private static class IntValue { 95 private final int value; 96 IntValue(int value)97 IntValue(int value) { 98 this.value = value; 99 } 100 101 @Override toString()102 public String toString() { 103 return "value = " + value; 104 } 105 } 106 testOnResultOf()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 testOnResultOf_equals()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 testEquivalentTo()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 testEqualsEquivalent()141 public void testEqualsEquivalent() { 142 EquivalenceTester.of(Equivalence.equals()) 143 .addEquivalenceGroup(new Integer(42), 42) 144 .addEquivalenceGroup("a") 145 .test(); 146 } 147 testIdentityEquivalent()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 testEquals()156 public void testEquals() { 157 new EqualsTester() 158 .addEqualityGroup(Equivalence.equals(), Equivalence.equals()) 159 .addEqualityGroup(Equivalence.identity(), Equivalence.identity()) 160 .testEquals(); 161 } 162 163 @J2ktIncompatible 164 @GwtIncompatible // NullPointerTester testNulls()165 public void testNulls() throws NoSuchMethodException { 166 NullPointerTester tester = new NullPointerTester(); 167 // Necessary until JDK15: 168 // https://bugs.openjdk.org/browse/JDK-8202469 169 tester.ignore(Equivalence.class.getMethod("wrap", Object.class)); 170 171 tester.testAllPublicStaticMethods(Equivalence.class); 172 tester.testAllPublicInstanceMethods(Equivalence.equals()); 173 tester.testAllPublicInstanceMethods(Equivalence.identity()); 174 } 175 } 176