1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.hash; 16 17 import com.google.common.testing.NullPointerTester; 18 19 import junit.framework.TestCase; 20 21 import org.easymock.EasyMock; 22 23 import java.io.ByteArrayOutputStream; 24 25 /** 26 * Tests for {@link HashingOutputStream}. 27 * 28 * @author Nick Piepmeier 29 */ 30 public class HashingOutputStreamTest extends TestCase { 31 private Hasher hasher; 32 private HashFunction hashFunction; 33 private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 34 35 @Override protected void setUp() throws Exception { 36 super.setUp(); 37 hasher = EasyMock.createMock(Hasher.class); 38 hashFunction = EasyMock.createMock(HashFunction.class); 39 40 EasyMock.expect(hashFunction.newHasher()).andReturn(hasher).once(); 41 EasyMock.replay(hashFunction); 42 } 43 44 public void testWrite_putSingleByte() throws Exception { 45 int b = 'q'; 46 EasyMock.expect(hasher.putByte((byte) b)).andReturn(hasher).once(); 47 EasyMock.replay(hasher); 48 HashingOutputStream out = new HashingOutputStream(hashFunction, buffer); 49 50 out.write(b); 51 52 EasyMock.verify(hashFunction); 53 EasyMock.verify(hasher); 54 } 55 56 public void testWrite_putByteArray() throws Exception { 57 byte[] buf = new byte[] {'y', 'a', 'm', 's'}; 58 EasyMock.expect(hasher.putBytes(buf, 0, buf.length)).andReturn(hasher).once(); 59 EasyMock.replay(hasher); 60 HashingOutputStream out = new HashingOutputStream(hashFunction, buffer); 61 62 out.write(buf); 63 64 EasyMock.verify(hashFunction); 65 EasyMock.verify(hasher); 66 } 67 68 public void testWrite_putByteArrayAtPos() throws Exception { 69 byte[] buf = new byte[] {'y', 'a', 'm', 's'}; 70 EasyMock.expect(hasher.putBytes(buf, 0, 3)).andReturn(hasher).once(); 71 EasyMock.replay(hasher); 72 HashingOutputStream out = new HashingOutputStream(hashFunction, buffer); 73 74 out.write(buf, 0, 3); 75 76 EasyMock.verify(hashFunction); 77 EasyMock.verify(hasher); 78 } 79 80 public void testHash_hashesCorrectly() throws Exception { 81 byte[] buf = new byte[] {'y', 'a', 'm', 's'}; 82 HashCode expectedHash = Hashing.md5().hashBytes(buf); 83 HashingOutputStream out = new HashingOutputStream(Hashing.md5(), buffer); 84 85 out.write(buf); 86 87 assertEquals(expectedHash, out.hash()); 88 } 89 90 public void testChecksForNull() throws Exception { 91 NullPointerTester tester = new NullPointerTester(); 92 tester.testAllPublicInstanceMethods( 93 new HashingOutputStream(Hashing.md5(), new ByteArrayOutputStream())); 94 tester.testAllPublicStaticMethods(HashingOutputStream.class); 95 tester.testAllPublicConstructors(HashingOutputStream.class); 96 } 97 } 98