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