/*
 * 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 static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.google.common.hash.AbstractStreamingHashFunction.AbstractStreamingHasher;

import junit.framework.TestCase;

import org.mockito.InOrder;

import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Tests for HashExtractors.
 *
 * @author Dimitris Andreou
 */
public class FunnelsTest extends TestCase {
  public void testForBytes() {
    PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
    Funnels.byteArrayFunnel().funnel(new byte[] { 4, 3, 2, 1 }, bytePrimitiveSink);
    verify(bytePrimitiveSink).putBytes(new byte[] { 4, 3, 2, 1 });
  }

  public void testForBytes_null() {
    assertNullsThrowException(Funnels.byteArrayFunnel());
  }

  public void testForStrings() {
    PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
    Funnels.stringFunnel().funnel("test", bytePrimitiveSink);
    verify(bytePrimitiveSink).putString("test");
  }

  public void testForStrings_null() {
    assertNullsThrowException(Funnels.stringFunnel());
  }

  public void testForInts() {
    Integer value = 1234;
    PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
    Funnels.integerFunnel().funnel(value, bytePrimitiveSink);
    verify(bytePrimitiveSink).putInt(1234);
  }

  public void testForInts_null() {
    assertNullsThrowException(Funnels.integerFunnel());
  }

  public void testForLongs() {
    Long value = 1234L;
    PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
    Funnels.longFunnel().funnel(value, bytePrimitiveSink);
    verify(bytePrimitiveSink).putLong(1234);
  }

  public void testForLongs_null() {
    assertNullsThrowException(Funnels.longFunnel());
  }

  public void testSequential() {
    @SuppressWarnings("unchecked")
    Funnel<Object> elementFunnel = mock(Funnel.class);
    PrimitiveSink primitiveSink = mock(PrimitiveSink.class);
    Funnel<Iterable<? extends Object>> sequential = Funnels.sequentialFunnel(elementFunnel);
    sequential.funnel(Arrays.asList("foo", "bar", "baz", "quux"), primitiveSink);
    InOrder inOrder = inOrder(elementFunnel);
    inOrder.verify(elementFunnel).funnel("foo", primitiveSink);
    inOrder.verify(elementFunnel).funnel("bar", primitiveSink);
    inOrder.verify(elementFunnel).funnel("baz", primitiveSink);
    inOrder.verify(elementFunnel).funnel("quux", primitiveSink);
  }

  private static void assertNullsThrowException(Funnel<?> funnel) {
    PrimitiveSink bytePrimitiveSink = new AbstractStreamingHasher(4, 4) {
      @Override HashCode makeHash() { throw new UnsupportedOperationException(); }

      @Override protected void process(ByteBuffer bb) {
        while (bb.hasRemaining()) {
          bb.get();
        }
      }
    };
    try {
      funnel.funnel(null, bytePrimitiveSink);
      fail();
    } catch (NullPointerException ok) {}
  }
  
  public void testAsOutputStream() throws Exception {
    PrimitiveSink sink = mock(PrimitiveSink.class);
    OutputStream out = Funnels.asOutputStream(sink);
    byte[] bytes = { 1, 2, 3, 4 };
    out.write(255);
    out.write(bytes);
    out.write(bytes, 1, 2);
    verify(sink).putByte((byte) 255);
    verify(sink).putBytes(bytes);
    verify(sink).putBytes(bytes, 1, 2);
  }
}

@@ -632,51 +632,8 @@
 h;%0A%0A
-import static org.mockito.Mockito.inOrder;%0A
 impo
@@ -668,16 +668,16 @@
 o.mock;%0A
+
 import s
@@ -835,37 +835,8 @@
 e;%0A%0A
-import org.mockito.InOrder;%0A%0A
 impo
@@ -891,33 +891,8 @@
 fer;
-%0Aimport java.util.Arrays;
 %0A%0A/*
@@ -2319,675 +2319,8 @@
  %7D%0A%0A
-  public void testSequential() %7B%0A    @SuppressWarnings(%22unchecked%22)%0A    Funnel%3CObject%3E elementFunnel = mock(Funnel.class);%0A    PrimitiveSink primitiveSink = mock(PrimitiveSink.class);%0A    Funnel%3CIterable%3C? extends Object%3E%3E sequential = Funnels.sequentialFunnel(elementFunnel);%0A    sequential.funnel(Arrays.asList(%22foo%22, %22bar%22, %22baz%22, %22quux%22), primitiveSink);%0A    InOrder inOrder = inOrder(elementFunnel);%0A    inOrder.verify(elementFunnel).funnel(%22foo%22, primitiveSink);%0A    inOrder.verify(elementFunnel).funnel(%22bar%22, primitiveSink);%0A    inOrder.verify(elementFunnel).funnel(%22baz%22, primitiveSink);%0A    inOrder.verify(elementFunnel).funnel(%22quux%22, primitiveSink);%0A  %7D%0A%0A
   pr
@@ -2793,18 +2793,16 @@
  %7B%7D%0A  %7D%0A
-  
 %0A  publi

Fix FunnelsTest to @GI Funnels.sequentialFunnel tests ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=43846130