alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (HashingOutputStreamTest.java)

This example Java source code file (HashingOutputStreamTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

bytearrayoutputstream, exception, hashcode, hasher, hashfunction, hashingoutputstream, hashingoutputstreamtest, nullpointertester, override, testcase

The HashingOutputStreamTest.java Java example source code

/*
 * Copyright (C) 2011 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.common.hash;

import com.google.common.testing.NullPointerTester;

import junit.framework.TestCase;

import org.easymock.EasyMock;

import java.io.ByteArrayOutputStream;

/**
 * Tests for {@link HashingOutputStream}.
 *
 * @author Nick Piepmeier
 */
public class HashingOutputStreamTest extends TestCase {
  private Hasher hasher;
  private HashFunction hashFunction;
  private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

  @Override protected void setUp() throws Exception {
    super.setUp();
    hasher = EasyMock.createMock(Hasher.class);
    hashFunction = EasyMock.createMock(HashFunction.class);

    EasyMock.expect(hashFunction.newHasher()).andReturn(hasher).once();
    EasyMock.replay(hashFunction);
  }

  public void testWrite_putSingleByte() throws Exception {
    int b = 'q';
    EasyMock.expect(hasher.putByte((byte) b)).andReturn(hasher).once();
    EasyMock.replay(hasher);
    HashingOutputStream out = new HashingOutputStream(hashFunction, buffer);

    out.write(b);

    EasyMock.verify(hashFunction);
    EasyMock.verify(hasher);
  }

  public void testWrite_putByteArray() throws Exception {
    byte[] buf = new byte[] {'y', 'a', 'm', 's'};
    EasyMock.expect(hasher.putBytes(buf, 0, buf.length)).andReturn(hasher).once();
    EasyMock.replay(hasher);
    HashingOutputStream out = new HashingOutputStream(hashFunction, buffer);

    out.write(buf);

    EasyMock.verify(hashFunction);
    EasyMock.verify(hasher);
  }

  public void testWrite_putByteArrayAtPos() throws Exception {
    byte[] buf = new byte[] {'y', 'a', 'm', 's'};
    EasyMock.expect(hasher.putBytes(buf, 0, 3)).andReturn(hasher).once();
    EasyMock.replay(hasher);
    HashingOutputStream out = new HashingOutputStream(hashFunction, buffer);

    out.write(buf, 0, 3);

    EasyMock.verify(hashFunction);
    EasyMock.verify(hasher);
  }

  public void testHash_hashesCorrectly() throws Exception {
    byte[] buf = new byte[] {'y', 'a', 'm', 's'};
    HashCode expectedHash = Hashing.md5().hashBytes(buf);
    HashingOutputStream out = new HashingOutputStream(Hashing.md5(), buffer);

    out.write(buf);

    assertEquals(expectedHash, out.hash());
  }

  public void testChecksForNull() throws Exception {
    NullPointerTester tester = new NullPointerTester();
    tester.testAllPublicInstanceMethods(
        new HashingOutputStream(Hashing.md5(), new ByteArrayOutputStream()));
    tester.testAllPublicStaticMethods(HashingOutputStream.class);
    tester.testAllPublicConstructors(HashingOutputStream.class);
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java HashingOutputStreamTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.