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   * @version $Id: CharSequenceReaderTest.java 1680650 2015-05-20 18:36:40Z britter $
31   */
32  public class CharSequenceReaderTest {
33      private static final char NONE = (new char[1])[0];
34  
35      @Test
36      public void testClose() throws IOException {
37          final Reader reader = new CharSequenceReader("FooBar");
38          checkRead(reader, "Foo");
39          reader.close();
40          checkRead(reader, "Foo");
41      }
42  
43      @Test
44      public void testMarkSupported() throws Exception {
45          final Reader reader = new CharSequenceReader("FooBar");
46          assertTrue(reader.markSupported());
47          reader.close();
48      }
49  
50      @Test
51      public void testMark() throws IOException {
52          final Reader reader = new CharSequenceReader("FooBar");
53          checkRead(reader, "Foo");
54          reader.mark(0);
55          checkRead(reader, "Bar");
56          reader.reset();
57          checkRead(reader, "Bar");
58          reader.close();
59          checkRead(reader, "Foo");
60          reader.reset();
61          checkRead(reader, "Foo");
62      }
63  
64      @Test
65      public void testSkip() throws IOException {
66          final Reader reader = new CharSequenceReader("FooBar");
67          assertEquals(3, reader.skip(3));
68          checkRead(reader, "Bar");
69          assertEquals(-1, reader.skip(3));
70          reader.reset();
71          assertEquals(2, reader.skip(2));
72          assertEquals(4, reader.skip(10));
73          assertEquals(-1, reader.skip(1));
74          reader.close();
75          assertEquals(6, reader.skip(20));
76          assertEquals(-1, reader.read());
77      }
78  
79      @Test
80      public void testRead() throws IOException {
81          final Reader reader = new CharSequenceReader("Foo");
82          assertEquals('F', reader.read());
83          assertEquals('o', reader.read());
84          assertEquals('o', reader.read());
85          assertEquals(-1, reader.read());
86          assertEquals(-1, reader.read());
87          reader.close();
88      }
89  
90      @Test
91      public void testReadCharArray() throws IOException {
92          final Reader reader = new CharSequenceReader("FooBar");
93          char[] chars = new char[2];
94          assertEquals(2, reader.read(chars));
95          checkArray(new char[] {'F', 'o'}, chars);
96          chars = new char[3];
97          assertEquals(3, reader.read(chars));
98          checkArray(new char[] {'o', 'B', 'a'}, chars);
99          chars = new char[3];
100         assertEquals(1, reader.read(chars));
101         checkArray(new char[] {'r', NONE, NONE}, chars);
102         assertEquals(-1, reader.read(chars));
103         reader.close();
104     }
105 
106     @Test
107     public void testReadCharArrayPortion() throws IOException {
108         final char[] chars = new char[10];
109         final Reader reader = new CharSequenceReader("FooBar");
110         assertEquals(3, reader.read(chars, 3, 3));
111         checkArray(new char[] {NONE, NONE, NONE, 'F', 'o', 'o'}, chars);
112         assertEquals(3, reader.read(chars, 0, 3));
113         checkArray(new char[] {'B', 'a', 'r', 'F', 'o', 'o', NONE}, chars);
114         assertEquals(-1, reader.read(chars));
115         reader.close();
116     }
117 
118     private void checkRead(final Reader reader, final String expected) throws IOException {
119         for (int i = 0; i < expected.length(); i++) {
120             assertEquals("Read[" + i + "] of '" + expected + "'", 
121                     expected.charAt(i), (char)reader.read());
122         }
123     }
124 
125     private void checkArray(final char[] expected, final char[] actual) {
126         for (int i = 0; i < expected.length; i++) {
127             assertEquals("Compare[" +i + "]", expected[i], actual[i]);
128         }
129     }
130 }