/*
 * Copyright (C) 2008 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.io;

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

/**
 * @author ricebin
 */
public class MultiReaderTest extends IoTestCase {

  public void testOnlyOneOpen() throws Exception {
    String testString = "abcdefgh";
    final InputSupplier<Reader> supplier = newReader(testString);
    final int[] counter = new int[1];
    InputSupplier<Reader> reader = new InputSupplier<Reader>() {
      @Override
      public Reader getInput() throws IOException {
        if (counter[0]++ != 0) {
          throw new IllegalStateException("More than one supplier open");
        }
        return new FilterReader(supplier.getInput()) {
          @Override public void close() throws IOException {
            super.close();
            counter[0]--;
          }
        };
      }
    };
    @SuppressWarnings("unchecked")
    Reader joinedReader = CharStreams.join(reader, reader, reader).getInput();
    String result = CharStreams.toString(joinedReader);
    assertEquals(testString.length() * 3, result.length());
  }

  public void testSimple() throws Exception {
    String testString = "abcdefgh";
    InputSupplier<Reader> supplier = newReader(testString);
    @SuppressWarnings("unchecked")
    Reader joinedReader = CharStreams.join(supplier, supplier).getInput();

    String expectedString = testString + testString;
    assertEquals(expectedString, CharStreams.toString(joinedReader));
  }

  private static InputSupplier<Reader> newReader(final String text) {
    return new InputSupplier<Reader>() {
      @Override
      public Reader getInput() {
        return new StringReader(text);
      }
    };
  }

  public void testSkip() throws Exception {
    String begin = "abcde";
    String end = "fghij";
    @SuppressWarnings("unchecked")
    Reader joinedReader =
        CharStreams.join(newReader(begin), newReader(end)).getInput();

    String expected = begin + end;
    assertEquals(expected.charAt(0), joinedReader.read());
    CharStreams.skipFully(joinedReader, 1);
    assertEquals(expected.charAt(2), joinedReader.read());
    CharStreams.skipFully(joinedReader, 4);
    assertEquals(expected.charAt(7), joinedReader.read());
    CharStreams.skipFully(joinedReader, 1);
    assertEquals(expected.charAt(9), joinedReader.read());
    assertEquals(-1, joinedReader.read());
  }

}

@@ -626,16 +626,99 @@
 on.io;%0A%0A
+import com.google.common.collect.ImmutableList;%0A%0Aimport junit.framework.TestCase;%0A%0A
 import j
@@ -730,32 +730,32 @@
 o.FilterReader;%0A
-
 import java.io.I
@@ -887,10 +887,8 @@
 nds 
-Io
 Test
@@ -1774,24 +1774,502 @@
 th());%0A  %7D%0A%0A
+  public void testReady() throws Exception %7B%0A    InputSupplier%3CReader%3E supplier = newReader(%22a%22);%0A    Iterable%3C? extends InputSupplier%3C? extends Reader%3E%3E list = ImmutableList.of(supplier, supplier);%0A    Reader joinedReader = CharStreams.join(list).getInput();%0A%0A    assertTrue(joinedReader.ready());%0A    assertEquals('a', joinedReader.read());%0A    assertEquals('a', joinedReader.read());%0A    assertEquals(-1, joinedReader.read());%0A    assertFalse(joinedReader.ready());    %0A  %7D%0A%0A
   public voi
@@ -2637,16 +2637,19 @@
 );%0A  %7D%0A%0A
+  %0A
   privat
@@ -3485,32 +3485,32 @@
 Reader.read());%0A
-
     assertEquals
@@ -3539,12 +3539,370 @@
 ());%0A  %7D
+%0A  %0A  public void testSkipZero() throws Exception %7B%0A    InputSupplier%3CReader%3E supplier = newReader(%22a%22);%0A    Iterable%3C? extends InputSupplier%3C? extends Reader%3E%3E list = ImmutableList.of(supplier, supplier);%0A    Reader joinedReader = CharStreams.join(list).getInput();%0A%0A    assertEquals(0, joinedReader.skip(0));%0A    assertEquals('a', joinedReader.read());%0A  %7D
 %0A%0A%7D%0A

test coverage for ready and skip ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=25242419