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 java.io.IOException;
20  import java.io.Reader;
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * Test case for {@link CharSequenceReader}.
26   *
27   * @version $Revision: 609152 $ $Date: 2008-01-05 15:24:10 +0000 (Sat, 05 Jan 2008) $
28   */
29  public class CharSequenceReaderTest extends TestCase {
30      private static final char NONE = (new char[1])[0];
31  
32      /**
33       * Contruct a new test case.
34       * @param name The name of the test
35       */
36      public CharSequenceReaderTest(String name) {
37          super(name);
38      }
39  
40      /** Test {@link Reader#close()}. */
41      public void testClose() throws IOException {
42          Reader reader = new CharSequenceReader("FooBar");
43          checkRead(reader, "Foo");
44          reader.close();
45          checkRead(reader, "Foo");
46      }
47  
48      /** Test {@link Reader#markSupported()}. */
49      public void testMarkSupported() throws IOException {
50          Reader reader = new CharSequenceReader("FooBar");
51          assertTrue(reader.markSupported());
52      }
53  
54      /** Test {@link Reader#mark(int)}. */
55      public void testMark() throws IOException {
56          Reader reader = new CharSequenceReader("FooBar");
57          checkRead(reader, "Foo");
58          reader.mark(0);
59          checkRead(reader, "Bar");
60          reader.reset();
61          checkRead(reader, "Bar");
62          reader.close();
63          checkRead(reader, "Foo");
64          reader.reset();
65          checkRead(reader, "Foo");
66      }
67  
68      /** Test {@link Reader#skip(int)}. */
69      public void testSkip() throws IOException {
70          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      public void testRead() throws IOException {
85          Reader reader = new CharSequenceReader("Foo");
86          assertEquals('F', reader.read());
87          assertEquals('o', reader.read());
88          assertEquals('o', reader.read());
89          assertEquals(-1, reader.read());
90          assertEquals(-1, reader.read());
91      }
92  
93      /** Test {@link Reader#read(char[])}. */
94      public void testReadCharArray() throws IOException {
95          Reader reader = new CharSequenceReader("FooBar");
96          char[] chars = new char[2];
97          assertEquals(2, reader.read(chars));
98          checkArray(new char[] {'F', 'o'}, chars);
99          chars = new char[3];
100         assertEquals(3, reader.read(chars));
101         checkArray(new char[] {'o', 'B', 'a'}, chars);
102         chars = new char[3];
103         assertEquals(1, reader.read(chars));
104         checkArray(new char[] {'r', NONE, NONE}, chars);
105         assertEquals(-1, reader.read(chars));
106     }
107 
108     /** Test {@link Reader#read(char[], int, int)}. */
109     public void testReadCharArrayPortion() throws IOException {
110         char[] chars = new char[10];
111         Reader reader = new CharSequenceReader("FooBar");
112         assertEquals(3, reader.read(chars, 3, 3));
113         checkArray(new char[] {NONE, NONE, NONE, 'F', 'o', 'o'}, chars);
114         assertEquals(3, reader.read(chars, 0, 3));
115         checkArray(new char[] {'B', 'a', 'r', 'F', 'o', 'o', NONE}, chars);
116         assertEquals(-1, reader.read(chars));
117     }
118 
119     private void checkRead(Reader reader, String expected) throws IOException {
120         for (int i = 0; i < expected.length(); i++) {
121             assertEquals("Read[" + i + "] of '" + expected + "'", 
122                     (char)expected.charAt(i), (char)reader.read());
123         }
124     }
125     private void checkArray(char[] expected, char[] actual) throws IOException {
126         for (int i = 0; i < expected.length; i++) {
127             assertEquals("Compare[" +i + "]", expected[i], actual[i]);
128         }
129     }
130 }