1 /* 2 * Copyright (C) 2011 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 * distributed under the License is distributed 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.hash; 18 19 import static org.mockito.Mockito.inOrder; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.verify; 22 23 import com.google.common.base.Charsets; 24 import com.google.common.testing.EqualsTester; 25 import com.google.common.testing.SerializableTester; 26 import java.io.OutputStream; 27 import java.nio.ByteBuffer; 28 import java.nio.charset.Charset; 29 import java.util.Arrays; 30 import junit.framework.TestCase; 31 import org.mockito.InOrder; 32 33 /** 34 * Tests for HashExtractors. 35 * 36 * @author Dimitris Andreou 37 */ 38 public class FunnelsTest extends TestCase { testForBytes()39 public void testForBytes() { 40 PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 41 Funnels.byteArrayFunnel().funnel(new byte[] {4, 3, 2, 1}, primitiveSink); 42 verify(primitiveSink).putBytes(new byte[] {4, 3, 2, 1}); 43 } 44 testForBytes_null()45 public void testForBytes_null() { 46 assertNullsThrowException(Funnels.byteArrayFunnel()); 47 } 48 testForStrings()49 public void testForStrings() { 50 PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 51 Funnels.unencodedCharsFunnel().funnel("test", primitiveSink); 52 verify(primitiveSink).putUnencodedChars("test"); 53 } 54 testForStrings_null()55 public void testForStrings_null() { 56 assertNullsThrowException(Funnels.unencodedCharsFunnel()); 57 } 58 testForStringsCharset()59 public void testForStringsCharset() { 60 for (Charset charset : Charset.availableCharsets().values()) { 61 PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 62 Funnels.stringFunnel(charset).funnel("test", primitiveSink); 63 verify(primitiveSink).putString("test", charset); 64 } 65 } 66 testForStringsCharset_null()67 public void testForStringsCharset_null() { 68 for (Charset charset : Charset.availableCharsets().values()) { 69 assertNullsThrowException(Funnels.stringFunnel(charset)); 70 } 71 } 72 testForInts()73 public void testForInts() { 74 Integer value = 1234; 75 PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 76 Funnels.integerFunnel().funnel(value, primitiveSink); 77 verify(primitiveSink).putInt(1234); 78 } 79 testForInts_null()80 public void testForInts_null() { 81 assertNullsThrowException(Funnels.integerFunnel()); 82 } 83 testForLongs()84 public void testForLongs() { 85 Long value = 1234L; 86 PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 87 Funnels.longFunnel().funnel(value, primitiveSink); 88 verify(primitiveSink).putLong(1234); 89 } 90 testForLongs_null()91 public void testForLongs_null() { 92 assertNullsThrowException(Funnels.longFunnel()); 93 } 94 testSequential()95 public void testSequential() { 96 @SuppressWarnings("unchecked") 97 Funnel<Object> elementFunnel = mock(Funnel.class); 98 PrimitiveSink primitiveSink = mock(PrimitiveSink.class); 99 Funnel<Iterable<?>> sequential = Funnels.sequentialFunnel(elementFunnel); 100 sequential.funnel(Arrays.asList("foo", "bar", "baz", "quux"), primitiveSink); 101 InOrder inOrder = inOrder(elementFunnel); 102 inOrder.verify(elementFunnel).funnel("foo", primitiveSink); 103 inOrder.verify(elementFunnel).funnel("bar", primitiveSink); 104 inOrder.verify(elementFunnel).funnel("baz", primitiveSink); 105 inOrder.verify(elementFunnel).funnel("quux", primitiveSink); 106 } 107 assertNullsThrowException(Funnel<?> funnel)108 private static void assertNullsThrowException(Funnel<?> funnel) { 109 PrimitiveSink primitiveSink = 110 new AbstractStreamingHasher(4, 4) { 111 @Override 112 protected HashCode makeHash() { 113 throw new UnsupportedOperationException(); 114 } 115 116 @Override 117 protected void process(ByteBuffer bb) { 118 while (bb.hasRemaining()) { 119 bb.get(); 120 } 121 } 122 }; 123 try { 124 funnel.funnel(null, primitiveSink); 125 fail(); 126 } catch (NullPointerException ok) { 127 } 128 } 129 testAsOutputStream()130 public void testAsOutputStream() throws Exception { 131 PrimitiveSink sink = mock(PrimitiveSink.class); 132 OutputStream out = Funnels.asOutputStream(sink); 133 byte[] bytes = {1, 2, 3, 4}; 134 out.write(255); 135 out.write(bytes); 136 out.write(bytes, 1, 2); 137 verify(sink).putByte((byte) 255); 138 verify(sink).putBytes(bytes); 139 verify(sink).putBytes(bytes, 1, 2); 140 } 141 testSerialization()142 public void testSerialization() { 143 assertSame( 144 Funnels.byteArrayFunnel(), SerializableTester.reserialize(Funnels.byteArrayFunnel())); 145 assertSame(Funnels.integerFunnel(), SerializableTester.reserialize(Funnels.integerFunnel())); 146 assertSame(Funnels.longFunnel(), SerializableTester.reserialize(Funnels.longFunnel())); 147 assertSame( 148 Funnels.unencodedCharsFunnel(), 149 SerializableTester.reserialize(Funnels.unencodedCharsFunnel())); 150 assertEquals( 151 Funnels.sequentialFunnel(Funnels.integerFunnel()), 152 SerializableTester.reserialize(Funnels.sequentialFunnel(Funnels.integerFunnel()))); 153 assertEquals( 154 Funnels.stringFunnel(Charsets.US_ASCII), 155 SerializableTester.reserialize(Funnels.stringFunnel(Charsets.US_ASCII))); 156 } 157 testEquals()158 public void testEquals() { 159 new EqualsTester() 160 .addEqualityGroup(Funnels.byteArrayFunnel()) 161 .addEqualityGroup(Funnels.integerFunnel()) 162 .addEqualityGroup(Funnels.longFunnel()) 163 .addEqualityGroup(Funnels.unencodedCharsFunnel()) 164 .addEqualityGroup(Funnels.stringFunnel(Charsets.UTF_8)) 165 .addEqualityGroup(Funnels.stringFunnel(Charsets.US_ASCII)) 166 .addEqualityGroup( 167 Funnels.sequentialFunnel(Funnels.integerFunnel()), 168 SerializableTester.reserialize(Funnels.sequentialFunnel(Funnels.integerFunnel()))) 169 .addEqualityGroup(Funnels.sequentialFunnel(Funnels.longFunnel())) 170 .testEquals(); 171 } 172 } 173