17dd252788645e940eada959bdde927426e2531c9Paul Duffin/* 27dd252788645e940eada959bdde927426e2531c9Paul Duffin * Copyright (C) 2011 The Guava Authors 37dd252788645e940eada959bdde927426e2531c9Paul Duffin * 47dd252788645e940eada959bdde927426e2531c9Paul Duffin * Licensed under the Apache License, Version 2.0 (the "License"); 57dd252788645e940eada959bdde927426e2531c9Paul Duffin * you may not use this file except in compliance with the License. 67dd252788645e940eada959bdde927426e2531c9Paul Duffin * You may obtain a copy of the License at 77dd252788645e940eada959bdde927426e2531c9Paul Duffin * 87dd252788645e940eada959bdde927426e2531c9Paul Duffin * http://www.apache.org/licenses/LICENSE-2.0 97dd252788645e940eada959bdde927426e2531c9Paul Duffin * 107dd252788645e940eada959bdde927426e2531c9Paul Duffin * Unless required by applicable law or agreed to in writing, software 117dd252788645e940eada959bdde927426e2531c9Paul Duffin * distributed under the License is distributed on an "AS IS" BASIS, 127dd252788645e940eada959bdde927426e2531c9Paul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137dd252788645e940eada959bdde927426e2531c9Paul Duffin * See the License for the specific language governing permissions and 147dd252788645e940eada959bdde927426e2531c9Paul Duffin * limitations under the License. 157dd252788645e940eada959bdde927426e2531c9Paul Duffin */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.hash; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 190888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static org.mockito.Mockito.inOrder; 207dd252788645e940eada959bdde927426e2531c9Paul Duffinimport static org.mockito.Mockito.mock; 217dd252788645e940eada959bdde927426e2531c9Paul Duffinimport static org.mockito.Mockito.verify; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 230888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.base.Charsets; 247dd252788645e940eada959bdde927426e2531c9Paul Duffinimport com.google.common.hash.AbstractStreamingHashFunction.AbstractStreamingHasher; 250888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.testing.EqualsTester; 260888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.testing.SerializableTester; 270888a09821a98ac0680fad765217302858e70fa4Paul Duffin 280888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport junit.framework.TestCase; 290888a09821a98ac0680fad765217302858e70fa4Paul Duffin 300888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport org.mockito.InOrder; 31dbd967a6e5c96cc1a97c5521f88dc1564ba2f81bPaul Duffin 327dd252788645e940eada959bdde927426e2531c9Paul Duffinimport java.io.OutputStream; 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.nio.ByteBuffer; 340888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.nio.charset.Charset; 350888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Arrays; 367dd252788645e940eada959bdde927426e2531c9Paul Duffin 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Tests for HashExtractors. 397dd252788645e940eada959bdde927426e2531c9Paul Duffin * 407dd252788645e940eada959bdde927426e2531c9Paul Duffin * @author Dimitris Andreou 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class FunnelsTest extends TestCase { 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testForBytes() { 440888a09821a98ac0680fad765217302858e70fa4Paul Duffin PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 450888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.byteArrayFunnel().funnel(new byte[] { 4, 3, 2, 1 }, primitiveSink); 460888a09821a98ac0680fad765217302858e70fa4Paul Duffin verify(primitiveSink).putBytes(new byte[] { 4, 3, 2, 1 }); 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testForBytes_null() { 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertNullsThrowException(Funnels.byteArrayFunnel()); 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testForStrings() { 540888a09821a98ac0680fad765217302858e70fa4Paul Duffin PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 550888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.unencodedCharsFunnel().funnel("test", primitiveSink); 560888a09821a98ac0680fad765217302858e70fa4Paul Duffin verify(primitiveSink).putUnencodedChars("test"); 57dbd967a6e5c96cc1a97c5521f88dc1564ba2f81bPaul Duffin } 587dd252788645e940eada959bdde927426e2531c9Paul Duffin 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testForStrings_null() { 600888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertNullsThrowException(Funnels.unencodedCharsFunnel()); 610888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 620888a09821a98ac0680fad765217302858e70fa4Paul Duffin 630888a09821a98ac0680fad765217302858e70fa4Paul Duffin public void testForStringsCharset() { 640888a09821a98ac0680fad765217302858e70fa4Paul Duffin for (Charset charset : Charset.availableCharsets().values()) { 650888a09821a98ac0680fad765217302858e70fa4Paul Duffin PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 660888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.stringFunnel(charset).funnel("test", primitiveSink); 670888a09821a98ac0680fad765217302858e70fa4Paul Duffin verify(primitiveSink).putString("test", charset); 680888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 690888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 700888a09821a98ac0680fad765217302858e70fa4Paul Duffin 710888a09821a98ac0680fad765217302858e70fa4Paul Duffin public void testForStringsCharset_null() { 720888a09821a98ac0680fad765217302858e70fa4Paul Duffin for (Charset charset : Charset.availableCharsets().values()) { 730888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertNullsThrowException(Funnels.stringFunnel(charset)); 740888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 767dd252788645e940eada959bdde927426e2531c9Paul Duffin 777dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testForInts() { 787dd252788645e940eada959bdde927426e2531c9Paul Duffin Integer value = 1234; 790888a09821a98ac0680fad765217302858e70fa4Paul Duffin PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 800888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.integerFunnel().funnel(value, primitiveSink); 810888a09821a98ac0680fad765217302858e70fa4Paul Duffin verify(primitiveSink).putInt(1234); 827dd252788645e940eada959bdde927426e2531c9Paul Duffin } 837dd252788645e940eada959bdde927426e2531c9Paul Duffin 847dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testForInts_null() { 857dd252788645e940eada959bdde927426e2531c9Paul Duffin assertNullsThrowException(Funnels.integerFunnel()); 867dd252788645e940eada959bdde927426e2531c9Paul Duffin } 877dd252788645e940eada959bdde927426e2531c9Paul Duffin 887dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testForLongs() { 897dd252788645e940eada959bdde927426e2531c9Paul Duffin Long value = 1234L; 900888a09821a98ac0680fad765217302858e70fa4Paul Duffin PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 910888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.longFunnel().funnel(value, primitiveSink); 920888a09821a98ac0680fad765217302858e70fa4Paul Duffin verify(primitiveSink).putLong(1234); 937dd252788645e940eada959bdde927426e2531c9Paul Duffin } 947dd252788645e940eada959bdde927426e2531c9Paul Duffin 957dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testForLongs_null() { 967dd252788645e940eada959bdde927426e2531c9Paul Duffin assertNullsThrowException(Funnels.longFunnel()); 977dd252788645e940eada959bdde927426e2531c9Paul Duffin } 987dd252788645e940eada959bdde927426e2531c9Paul Duffin 990888a09821a98ac0680fad765217302858e70fa4Paul Duffin public void testSequential() { 1000888a09821a98ac0680fad765217302858e70fa4Paul Duffin @SuppressWarnings("unchecked") 1010888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnel<Object> elementFunnel = mock(Funnel.class); 1020888a09821a98ac0680fad765217302858e70fa4Paul Duffin PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 1030888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnel<Iterable<? extends Object>> sequential = Funnels.sequentialFunnel(elementFunnel); 1040888a09821a98ac0680fad765217302858e70fa4Paul Duffin sequential.funnel(Arrays.asList("foo", "bar", "baz", "quux"), primitiveSink); 1050888a09821a98ac0680fad765217302858e70fa4Paul Duffin InOrder inOrder = inOrder(elementFunnel); 1060888a09821a98ac0680fad765217302858e70fa4Paul Duffin inOrder.verify(elementFunnel).funnel("foo", primitiveSink); 1070888a09821a98ac0680fad765217302858e70fa4Paul Duffin inOrder.verify(elementFunnel).funnel("bar", primitiveSink); 1080888a09821a98ac0680fad765217302858e70fa4Paul Duffin inOrder.verify(elementFunnel).funnel("baz", primitiveSink); 1090888a09821a98ac0680fad765217302858e70fa4Paul Duffin inOrder.verify(elementFunnel).funnel("quux", primitiveSink); 1100888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 1110888a09821a98ac0680fad765217302858e70fa4Paul Duffin 1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static void assertNullsThrowException(Funnel<?> funnel) { 1130888a09821a98ac0680fad765217302858e70fa4Paul Duffin PrimitiveSink primitiveSink = new AbstractStreamingHasher(4, 4) { 1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override HashCode makeHash() { throw new UnsupportedOperationException(); } 1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override protected void process(ByteBuffer bb) { 1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert while (bb.hasRemaining()) { 1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert bb.get(); 1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 1230888a09821a98ac0680fad765217302858e70fa4Paul Duffin funnel.funnel(null, primitiveSink); 1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (NullPointerException ok) {} 1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1270888a09821a98ac0680fad765217302858e70fa4Paul Duffin 1287dd252788645e940eada959bdde927426e2531c9Paul Duffin public void testAsOutputStream() throws Exception { 1297dd252788645e940eada959bdde927426e2531c9Paul Duffin PrimitiveSink sink = mock(PrimitiveSink.class); 1307dd252788645e940eada959bdde927426e2531c9Paul Duffin OutputStream out = Funnels.asOutputStream(sink); 1317dd252788645e940eada959bdde927426e2531c9Paul Duffin byte[] bytes = { 1, 2, 3, 4 }; 1327dd252788645e940eada959bdde927426e2531c9Paul Duffin out.write(255); 1337dd252788645e940eada959bdde927426e2531c9Paul Duffin out.write(bytes); 1347dd252788645e940eada959bdde927426e2531c9Paul Duffin out.write(bytes, 1, 2); 1357dd252788645e940eada959bdde927426e2531c9Paul Duffin verify(sink).putByte((byte) 255); 1367dd252788645e940eada959bdde927426e2531c9Paul Duffin verify(sink).putBytes(bytes); 1377dd252788645e940eada959bdde927426e2531c9Paul Duffin verify(sink).putBytes(bytes, 1, 2); 1387dd252788645e940eada959bdde927426e2531c9Paul Duffin } 1390888a09821a98ac0680fad765217302858e70fa4Paul Duffin 1400888a09821a98ac0680fad765217302858e70fa4Paul Duffin public void testSerialization() { 1410888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertSame( 1420888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.byteArrayFunnel(), 1430888a09821a98ac0680fad765217302858e70fa4Paul Duffin SerializableTester.reserialize(Funnels.byteArrayFunnel())); 1440888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertSame( 1450888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.integerFunnel(), 1460888a09821a98ac0680fad765217302858e70fa4Paul Duffin SerializableTester.reserialize(Funnels.integerFunnel())); 1470888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertSame( 1480888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.longFunnel(), 1490888a09821a98ac0680fad765217302858e70fa4Paul Duffin SerializableTester.reserialize(Funnels.longFunnel())); 1500888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertSame( 1510888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.unencodedCharsFunnel(), 1520888a09821a98ac0680fad765217302858e70fa4Paul Duffin SerializableTester.reserialize(Funnels.unencodedCharsFunnel())); 1530888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertEquals( 1540888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.sequentialFunnel(Funnels.integerFunnel()), 1550888a09821a98ac0680fad765217302858e70fa4Paul Duffin SerializableTester.reserialize(Funnels.sequentialFunnel(Funnels.integerFunnel()))); 1560888a09821a98ac0680fad765217302858e70fa4Paul Duffin assertEquals( 1570888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.stringFunnel(Charsets.US_ASCII), 1580888a09821a98ac0680fad765217302858e70fa4Paul Duffin SerializableTester.reserialize(Funnels.stringFunnel(Charsets.US_ASCII))); 1590888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 1600888a09821a98ac0680fad765217302858e70fa4Paul Duffin 1610888a09821a98ac0680fad765217302858e70fa4Paul Duffin public void testEquals() { 1620888a09821a98ac0680fad765217302858e70fa4Paul Duffin new EqualsTester() 1630888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.byteArrayFunnel()) 1640888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.integerFunnel()) 1650888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.longFunnel()) 1660888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.unencodedCharsFunnel()) 1670888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.stringFunnel(Charsets.UTF_8)) 1680888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.stringFunnel(Charsets.US_ASCII)) 1690888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.sequentialFunnel(Funnels.integerFunnel()), 1700888a09821a98ac0680fad765217302858e70fa4Paul Duffin SerializableTester.reserialize(Funnels.sequentialFunnel( 1710888a09821a98ac0680fad765217302858e70fa4Paul Duffin Funnels.integerFunnel()))) 1720888a09821a98ac0680fad765217302858e70fa4Paul Duffin .addEqualityGroup(Funnels.sequentialFunnel(Funnels.longFunnel())) 1730888a09821a98ac0680fad765217302858e70fa4Paul Duffin .testEquals(); 1740888a09821a98ac0680fad765217302858e70fa4Paul Duffin } 1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 176