• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.io;
18 
19 import com.google.common.collect.ImmutableList;
20 
21 import junit.framework.TestCase;
22 
23 import java.io.FilterReader;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
27 
28 /**
29  * @author ricebin
30  */
31 public class MultiReaderTest extends TestCase {
32 
testOnlyOneOpen()33   public void testOnlyOneOpen() throws Exception {
34     String testString = "abcdefgh";
35     final InputSupplier<Reader> supplier = newReader(testString);
36     final int[] counter = new int[1];
37     InputSupplier<Reader> reader = new InputSupplier<Reader>() {
38       @Override
39       public Reader getInput() throws IOException {
40         if (counter[0]++ != 0) {
41           throw new IllegalStateException("More than one supplier open");
42         }
43         return new FilterReader(supplier.getInput()) {
44           @Override public void close() throws IOException {
45             super.close();
46             counter[0]--;
47           }
48         };
49       }
50     };
51     @SuppressWarnings("unchecked")
52     Reader joinedReader = CharStreams.join(reader, reader, reader).getInput();
53     String result = CharStreams.toString(joinedReader);
54     assertEquals(testString.length() * 3, result.length());
55   }
56 
testReady()57   public void testReady() throws Exception {
58     InputSupplier<Reader> supplier = newReader("a");
59     Iterable<? extends InputSupplier<? extends Reader>> list = ImmutableList.of(supplier, supplier);
60     Reader joinedReader = CharStreams.join(list).getInput();
61 
62     assertTrue(joinedReader.ready());
63     assertEquals('a', joinedReader.read());
64     assertEquals('a', joinedReader.read());
65     assertEquals(-1, joinedReader.read());
66     assertFalse(joinedReader.ready());
67   }
68 
testSimple()69   public void testSimple() throws Exception {
70     String testString = "abcdefgh";
71     InputSupplier<Reader> supplier = newReader(testString);
72     @SuppressWarnings("unchecked")
73     Reader joinedReader = CharStreams.join(supplier, supplier).getInput();
74 
75     String expectedString = testString + testString;
76     assertEquals(expectedString, CharStreams.toString(joinedReader));
77   }
78 
79 
newReader(final String text)80   private static InputSupplier<Reader> newReader(final String text) {
81     return new InputSupplier<Reader>() {
82       @Override
83       public Reader getInput() {
84         return new StringReader(text);
85       }
86     };
87   }
88 
89   public void testSkip() throws Exception {
90     String begin = "abcde";
91     String end = "fghij";
92     @SuppressWarnings("unchecked")
93     Reader joinedReader =
94         CharStreams.join(newReader(begin), newReader(end)).getInput();
95 
96     String expected = begin + end;
97     assertEquals(expected.charAt(0), joinedReader.read());
98     CharStreams.skipFully(joinedReader, 1);
99     assertEquals(expected.charAt(2), joinedReader.read());
100     CharStreams.skipFully(joinedReader, 4);
101     assertEquals(expected.charAt(7), joinedReader.read());
102     CharStreams.skipFully(joinedReader, 1);
103     assertEquals(expected.charAt(9), joinedReader.read());
104     assertEquals(-1, joinedReader.read());
105   }
106 
107   public void testSkipZero() throws Exception {
108     InputSupplier<Reader> supplier = newReader("a");
109     Iterable<? extends InputSupplier<? extends Reader>> list = ImmutableList.of(supplier, supplier);
110     Reader joinedReader = CharStreams.join(list).getInput();
111 
112     assertEquals(0, joinedReader.skip(0));
113     assertEquals('a', joinedReader.read());
114   }
115 
116 }
117