• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
20 import static org.mockito.Mockito.when;
21 
22 import com.google.common.testing.NullPointerTester;
23 import java.io.ByteArrayOutputStream;
24 import junit.framework.TestCase;
25 
26 /**
27  * Tests for {@link HashingOutputStream}.
28  *
29  * @author Nick Piepmeier
30  */
31 public class HashingOutputStreamTest extends TestCase {
32   private Hasher hasher;
33   private HashFunction hashFunction;
34   private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
35 
36   @SuppressWarnings("DoNotMock")
37   @Override
setUp()38   protected void setUp() throws Exception {
39     super.setUp();
40     hasher = mock(Hasher.class);
41     hashFunction = mock(HashFunction.class);
42 
43     when(hashFunction.newHasher()).thenReturn(hasher);
44   }
45 
testWrite_putSingleByte()46   public void testWrite_putSingleByte() throws Exception {
47     int b = 'q';
48     HashingOutputStream out = new HashingOutputStream(hashFunction, buffer);
49     out.write(b);
50 
51     verify(hashFunction).newHasher();
52     verify(hasher).putByte((byte) b);
53     verifyNoMoreInteractions(hashFunction, hasher);
54   }
55 
testWrite_putByteArray()56   public void testWrite_putByteArray() throws Exception {
57     byte[] buf = new byte[] {'y', 'a', 'm', 's'};
58     HashingOutputStream out = new HashingOutputStream(hashFunction, buffer);
59     out.write(buf);
60 
61     verify(hashFunction).newHasher();
62     verify(hasher).putBytes(buf, 0, buf.length);
63     verifyNoMoreInteractions(hashFunction, hasher);
64   }
65 
testWrite_putByteArrayAtPos()66   public void testWrite_putByteArrayAtPos() throws Exception {
67     byte[] buf = new byte[] {'y', 'a', 'm', 's'};
68     HashingOutputStream out = new HashingOutputStream(hashFunction, buffer);
69     out.write(buf, 0, 3);
70 
71     verify(hashFunction).newHasher();
72     verify(hasher).putBytes(buf, 0, 3);
73     verifyNoMoreInteractions(hashFunction, hasher);
74   }
75 
testHash_hashesCorrectly()76   public void testHash_hashesCorrectly() throws Exception {
77     byte[] buf = new byte[] {'y', 'a', 'm', 's'};
78     HashCode expectedHash = Hashing.md5().hashBytes(buf);
79     HashingOutputStream out = new HashingOutputStream(Hashing.md5(), buffer);
80 
81     out.write(buf);
82 
83     assertEquals(expectedHash, out.hash());
84   }
85 
testChecksForNull()86   public void testChecksForNull() throws Exception {
87     NullPointerTester tester = new NullPointerTester();
88     tester.testAllPublicInstanceMethods(
89         new HashingOutputStream(Hashing.md5(), new ByteArrayOutputStream()));
90     tester.testAllPublicStaticMethods(HashingOutputStream.class);
91     tester.testAllPublicConstructors(HashingOutputStream.class);
92   }
93 }
94