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