11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2010 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * diOBJECTibuted under the License is diOBJECTibuted on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.base; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtIncompatible; 217dd252788645e940eada959bdde927426e2531c9Paul Duffinimport com.google.common.base.Equivalence.Wrapper; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.ImmutableList; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.testing.EqualsTester; 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.testing.EquivalenceTester; 257dd252788645e940eada959bdde927426e2531c9Paul Duffinimport com.google.common.testing.NullPointerTester; 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.testing.SerializableTester; 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport junit.framework.TestCase; 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unit test for {@link Equivalence}. 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Jige Yu 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true) 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class EquivalenceTest extends TestCase { 370888a09821a98ac0680fad765217302858e70fa4Paul Duffin @SuppressWarnings("unchecked") // varargs 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPairwiseEquivalent() { 397dd252788645e940eada959bdde927426e2531c9Paul Duffin EquivalenceTester.of(Equivalence.equals().<String>pairwise()) 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEquivalenceGroup(ImmutableList.<String>of()) 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEquivalenceGroup(ImmutableList.of("a")) 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEquivalenceGroup(ImmutableList.of("b")) 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEquivalenceGroup(ImmutableList.of("a", "b"), ImmutableList.of("a", "b")) 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .test(); 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPairwiseEquivalent_equals() { 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert new EqualsTester() 497dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.equals().pairwise(), Equivalence.equals().pairwise()) 507dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.identity().pairwise()) 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .testEquals(); 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private enum LengthFunction implements Function<String, Integer> { 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert INSTANCE; 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public Integer apply(String input) { 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return input.length(); 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 627dd252788645e940eada959bdde927426e2531c9Paul Duffin private static final Equivalence<String> LENGTH_EQUIVALENCE = Equivalence.equals() 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .onResultOf(LengthFunction.INSTANCE); 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testWrap() { 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert new EqualsTester() 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEqualityGroup( 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert LENGTH_EQUIVALENCE.wrap("hello"), 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert LENGTH_EQUIVALENCE.wrap("hello"), 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert LENGTH_EQUIVALENCE.wrap("world")) 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEqualityGroup( 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert LENGTH_EQUIVALENCE.wrap("hi"), 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert LENGTH_EQUIVALENCE.wrap("yo")) 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEqualityGroup( 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert LENGTH_EQUIVALENCE.wrap(null), 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert LENGTH_EQUIVALENCE.wrap(null)) 777dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.equals().wrap("hello")) 787dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.equals().wrap(null)) 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .testEquals(); 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 827dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testWrap_get() { 837dd252788645e940eada959bdde927426e2531c9Paul Duffin String test = "test"; 847dd252788645e940eada959bdde927426e2531c9Paul Duffin Wrapper<String> wrapper = LENGTH_EQUIVALENCE.wrap(test); 857dd252788645e940eada959bdde927426e2531c9Paul Duffin assertSame(test, wrapper.get()); 867dd252788645e940eada959bdde927426e2531c9Paul Duffin } 877dd252788645e940eada959bdde927426e2531c9Paul Duffin 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("SerializableTester") 897dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testSerialization() { 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SerializableTester.reserializeAndAssert(LENGTH_EQUIVALENCE.wrap("hello")); 917dd252788645e940eada959bdde927426e2531c9Paul Duffin SerializableTester.reserializeAndAssert(Equivalence.equals()); 927dd252788645e940eada959bdde927426e2531c9Paul Duffin SerializableTester.reserializeAndAssert(Equivalence.identity()); 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class IntValue { 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final int value; 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert IntValue(int value) { 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.value = value; 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public String toString() { 1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return "value = " + value; 1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testOnResultOf() { 1087dd252788645e940eada959bdde927426e2531c9Paul Duffin EquivalenceTester.of(Equivalence.equals().onResultOf(Functions.toStringFunction())) 1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEquivalenceGroup(new IntValue(1), new IntValue(1)) 1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEquivalenceGroup(new IntValue(2)) 1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .test(); 1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testOnResultOf_equals() { 1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert new EqualsTester() 1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEqualityGroup( 1177dd252788645e940eada959bdde927426e2531c9Paul Duffin Equivalence.identity().onResultOf(Functions.toStringFunction()), 1187dd252788645e940eada959bdde927426e2531c9Paul Duffin Equivalence.identity().onResultOf(Functions.toStringFunction())) 1197dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.equals().onResultOf(Functions.toStringFunction())) 1207dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.identity().onResultOf(Functions.identity())) 1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .testEquals(); 1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testEquivalentTo() { 1257dd252788645e940eada959bdde927426e2531c9Paul Duffin Predicate<Object> equalTo1 = Equivalence.equals().equivalentTo("1"); 1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(equalTo1.apply("1")); 1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertFalse(equalTo1.apply("2")); 1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertFalse(equalTo1.apply(null)); 1297dd252788645e940eada959bdde927426e2531c9Paul Duffin Predicate<Object> isNull = Equivalence.equals().equivalentTo(null); 1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertFalse(isNull.apply("1")); 1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertFalse(isNull.apply("2")); 1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(isNull.apply(null)); 1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert new EqualsTester() 1357dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(equalTo1, Equivalence.equals().equivalentTo("1")) 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addEqualityGroup(isNull) 1377dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.identity().equivalentTo("1")) 1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .testEquals(); 1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1407dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testEqualsEquivalent() { 1417dd252788645e940eada959bdde927426e2531c9Paul Duffin EquivalenceTester.of(Equivalence.equals()) 1427dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEquivalenceGroup(new Integer(42), 42) 1437dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEquivalenceGroup("a") 1447dd252788645e940eada959bdde927426e2531c9Paul Duffin .test(); 1457dd252788645e940eada959bdde927426e2531c9Paul Duffin } 1467dd252788645e940eada959bdde927426e2531c9Paul Duffin 1477dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testIdentityEquivalent() { 1487dd252788645e940eada959bdde927426e2531c9Paul Duffin EquivalenceTester.of(Equivalence.identity()) 1497dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEquivalenceGroup(new Integer(42)) 1507dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEquivalenceGroup(new Integer(42)) 1517dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEquivalenceGroup("a") 1527dd252788645e940eada959bdde927426e2531c9Paul Duffin .test(); 1537dd252788645e940eada959bdde927426e2531c9Paul Duffin } 1547dd252788645e940eada959bdde927426e2531c9Paul Duffin 1557dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testEquals() { 1567dd252788645e940eada959bdde927426e2531c9Paul Duffin new EqualsTester() 1577dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.equals(), Equivalence.equals()) 1587dd252788645e940eada959bdde927426e2531c9Paul Duffin .addEqualityGroup(Equivalence.identity(), Equivalence.identity()) 1597dd252788645e940eada959bdde927426e2531c9Paul Duffin .testEquals(); 1607dd252788645e940eada959bdde927426e2531c9Paul Duffin } 1617dd252788645e940eada959bdde927426e2531c9Paul Duffin 1627dd252788645e940eada959bdde927426e2531c9Paul Duffin @GwtIncompatible("NullPointerTester") 1637dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testNulls() { 1647dd252788645e940eada959bdde927426e2531c9Paul Duffin new NullPointerTester().testAllPublicStaticMethods(Equivalence.class); 1657dd252788645e940eada959bdde927426e2531c9Paul Duffin new NullPointerTester().testAllPublicInstanceMethods(Equivalence.equals()); 1667dd252788645e940eada959bdde927426e2531c9Paul Duffin new NullPointerTester().testAllPublicInstanceMethods(Equivalence.identity()); 1677dd252788645e940eada959bdde927426e2531c9Paul Duffin } 1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 169