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 import java.io.FilterReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.io.StringReader;
24 import junit.framework.TestCase;
25 
26 /** @author ricebin */
27 public class MultiReaderTest extends TestCase {
28 
testOnlyOneOpen()29   public void testOnlyOneOpen() throws Exception {
30     String testString = "abcdefgh";
31     final CharSource source = newCharSource(testString);
32     final int[] counter = new int[1];
33     CharSource reader =
34         new CharSource() {
35           @Override
36           public Reader openStream() throws IOException {
37             if (counter[0]++ != 0) {
38               throw new IllegalStateException("More than one source open");
39             }
40             return new FilterReader(source.openStream()) {
41               @Override
42               public void close() throws IOException {
43                 super.close();
44                 counter[0]--;
45               }
46             };
47           }
48         };
49     Reader joinedReader = CharSource.concat(reader, reader, reader).openStream();
50     String result = CharStreams.toString(joinedReader);
51     assertEquals(testString.length() * 3, result.length());
52   }
53 
testReady()54   public void testReady() throws Exception {
55     CharSource source = newCharSource("a");
56     Iterable<? extends CharSource> list = ImmutableList.of(source, source);
57     Reader joinedReader = CharSource.concat(list).openStream();
58 
59     assertTrue(joinedReader.ready());
60     assertEquals('a', joinedReader.read());
61     assertEquals('a', joinedReader.read());
62     assertEquals(-1, joinedReader.read());
63     assertFalse(joinedReader.ready());
64   }
65 
testSimple()66   public void testSimple() throws Exception {
67     String testString = "abcdefgh";
68     CharSource source = newCharSource(testString);
69     Reader joinedReader = CharSource.concat(source, source).openStream();
70 
71     String expectedString = testString + testString;
72     assertEquals(expectedString, CharStreams.toString(joinedReader));
73   }
74 
newCharSource(final String text)75   private static CharSource newCharSource(final String text) {
76     return new CharSource() {
77       @Override
78       public Reader openStream() {
79         return new StringReader(text);
80       }
81     };
82   }
83 
84   public void testSkip() throws Exception {
85     String begin = "abcde";
86     String end = "fghij";
87     Reader joinedReader = CharSource.concat(newCharSource(begin), newCharSource(end)).openStream();
88 
89     String expected = begin + end;
90     assertEquals(expected.charAt(0), joinedReader.read());
91     CharStreams.skipFully(joinedReader, 1);
92     assertEquals(expected.charAt(2), joinedReader.read());
93     CharStreams.skipFully(joinedReader, 4);
94     assertEquals(expected.charAt(7), joinedReader.read());
95     CharStreams.skipFully(joinedReader, 1);
96     assertEquals(expected.charAt(9), joinedReader.read());
97     assertEquals(-1, joinedReader.read());
98   }
99 
100   public void testSkipZero() throws Exception {
101     CharSource source = newCharSource("a");
102     Iterable<CharSource> list = ImmutableList.of(source, source);
103     Reader joinedReader = CharSource.concat(list).openStream();
104 
105     assertEquals(0, joinedReader.skip(0));
106     assertEquals('a', joinedReader.read());
107   }
108 }
109