1 // Copyright 2011 Google Inc. All Rights Reserved. 2 3 package com.google.common.hash; 4 5 import com.google.common.hash.AbstractStreamingHashFunction.AbstractStreamingHasher; 6 7 import junit.framework.TestCase; 8 9 import org.easymock.EasyMock; 10 11 import java.nio.ByteBuffer; 12 13 /** 14 * Tests for HashExtractors. 15 * 16 * @author andreou@google.com (Dimitris Andreou) 17 */ 18 public class FunnelsTest extends TestCase { testForBytes()19 public void testForBytes() { 20 Sink byteSink = EasyMock.createMock(Sink.class); 21 22 EasyMock.expect(byteSink.putBytes(EasyMock.aryEq(new byte[] { 4, 3, 2, 1}))) 23 .andReturn(byteSink).once(); 24 EasyMock.replay(byteSink); 25 26 Funnels.byteArrayFunnel().funnel(new byte[]{4, 3, 2, 1}, byteSink); 27 28 EasyMock.verify(byteSink); 29 } 30 testForBytes_null()31 public void testForBytes_null() { 32 assertNullsThrowException(Funnels.byteArrayFunnel()); 33 } 34 testForStrings()35 public void testForStrings() { 36 37 Sink byteSink = EasyMock.createMock(Sink.class); 38 39 EasyMock.expect(byteSink.putString("test")).andReturn(byteSink).once(); 40 EasyMock.replay(byteSink); 41 42 Funnels.stringFunnel().funnel("test", byteSink); 43 44 EasyMock.verify(byteSink); 45 } 46 testForStrings_null()47 public void testForStrings_null() { 48 assertNullsThrowException(Funnels.stringFunnel()); 49 } 50 assertNullsThrowException(Funnel<?> funnel)51 private static void assertNullsThrowException(Funnel<?> funnel) { 52 Sink byteSink = new AbstractStreamingHasher(4, 4) { 53 @Override HashCode makeHash() { throw new UnsupportedOperationException(); } 54 55 @Override protected void process(ByteBuffer bb) { 56 while (bb.hasRemaining()) { 57 bb.get(); 58 } 59 } 60 }; 61 try { 62 funnel.funnel(null, byteSink); 63 fail(); 64 } catch (NullPointerException ok) {} 65 } 66 } 67