View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.input;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  import org.junit.Test;
26  
27  /**
28   * Test case for {@link CharSequenceReader}.
29   *
30   */
31  public class CharSequenceReaderTest {
32      private static final char NONE = (new char[1])[0];
33  
34      @Test
35      public void testClose() throws IOException {
36          final Reader reader = new CharSequenceReader("FooBar");
37          checkRead(reader, "Foo");
38          reader.close();
39          checkRead(reader, "Foo");
40      }
41  
42      @Test
43      public void testMarkSupported() throws Exception {
44          final Reader reader = new CharSequenceReader("FooBar");
45          assertTrue(reader.markSupported());
46          reader.close();
47      }
48  
49      @Test
50      public void testMark() throws IOException {
51          final Reader reader = new CharSequenceReader("FooBar");
52          checkRead(reader, "Foo");
53          reader.mark(0);
54          checkRead(reader, "Bar");
55          reader.reset();
56          checkRead(reader, "Bar");
57          reader.close();
58          checkRead(reader, "Foo");
59          reader.reset();
60          checkRead(reader, "Foo");
61      }
62  
63      @Test
64      public void testSkip() throws IOException {
65          final Reader reader = new CharSequenceReader("FooBar");
66          assertEquals(3, reader.skip(3));
67          checkRead(reader, "Bar");
68          assertEquals(-1, reader.skip(3));
69          reader.reset();
70          assertEquals(2, reader.skip(2));
71          assertEquals(4, reader.skip(10));
72          assertEquals(-1, reader.skip(1));
73          reader.close();
74          assertEquals(6, reader.skip(20));
75          assertEquals(-1, reader.read());
76      }
77  
78      @Test
79      public void testRead() throws IOException {
80          final Reader reader = new CharSequenceReader("Foo");
81          assertEquals('F', reader.read());
82          assertEquals('o', reader.read());
83          assertEquals('o', reader.read());
84          assertEquals(-1, reader.read());
85          assertEquals(-1, reader.read());
86          reader.close();
87      }
88  
89      @Test
90      public void testReadCharArray() throws IOException {
91          final Reader reader = new CharSequenceReader("FooBar");
92          char[] chars = new char[2];
93          assertEquals(2, reader.read(chars));
94          checkArray(new char[] {'F', 'o'}, chars);
95          chars = new char[3];
96          assertEquals(3, reader.read(chars));
97          checkArray(new char[] {'o', 'B', 'a'}, chars);
98          chars = new char[3];
99          assertEquals(1, reader.read(chars));
100         checkArray(new char[] {'r', NONE, NONE}, chars);
101         assertEquals(-1, reader.read(chars));
102         reader.close();
103     }
104 
105     @Test
106     public void testReadCharArrayPortion() throws IOException {
107         final char[] chars = new char[10];
108         final Reader reader = new CharSequenceReader("FooBar");
109         assertEquals(3, reader.read(chars, 3, 3));
110         checkArray(new char[] {NONE, NONE, NONE, 'F', 'o', 'o'}, chars);
111         assertEquals(3, reader.read(chars, 0, 3));
112         checkArray(new char[] {'B', 'a', 'r', 'F', 'o', 'o', NONE}, chars);
113         assertEquals(-1, reader.read(chars));
114         reader.close();
115     }
116 
117     private void checkRead(final Reader reader, final String expected) throws IOException {
118         for (int i = 0; i < expected.length(); i++) {
119             assertEquals("Read[" + i + "] of '" + expected + "'", 
120                     expected.charAt(i), (char)reader.read());
121         }
122     }
123 
124     private void checkArray(final char[] expected, final char[] actual) {
125         for (int i = 0; i < expected.length; i++) {
126             assertEquals("Compare[" +i + "]", expected[i], actual[i]);
127         }
128     }
129 }