View Javadoc
1   /*
2    * Copyright (C) 2013 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 org.davidmoten.text.utils;
18  
19  import java.io.IOException;
20  import java.nio.CharBuffer;
21  
22  import junit.framework.TestCase;
23  
24  public class CharSequenceReaderTest extends TestCase {
25  
26      public void testReadEmptyString() throws IOException {
27          assertReadsCorrectly("");
28      }
29  
30      public void testReady() throws IOException {
31          String string = "abcdefghijklmnopqrstuvwxyz";
32          try (CharSequenceReader reader = new CharSequenceReader(string)) {
33              assertTrue(reader.ready());
34          }
35      }
36  
37      public void testReadsStringsCorrectly() throws IOException {
38          assertReadsCorrectly("abc");
39          assertReadsCorrectly("abcde");
40          assertReadsCorrectly("abcdefghijkl");
41          assertReadsCorrectly("" + "abcdefghijklmnopqrstuvwxyz\n" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r"
42                  + "0123456789\r\n" + "!@#$%^&*()-=_+\t[]{};':\",./<>?\\| ");
43      }
44  
45      public void testMarkAndReset() throws IOException {
46          String string = "abcdefghijklmnopqrstuvwxyz";
47          CharSequenceReader reader = new CharSequenceReader(string);
48          assertTrue(reader.markSupported());
49  
50          assertEquals(string, readFully(reader));
51          assertFullyRead(reader);
52  
53          // reset and read again
54          reader.reset();
55          assertEquals(string, readFully(reader));
56          assertFullyRead(reader);
57  
58          // reset, skip, mark, then read the rest
59          reader.reset();
60          assertEquals(5, reader.skip(5));
61          reader.mark(Integer.MAX_VALUE);
62          assertEquals(string.substring(5), readFully(reader));
63          assertFullyRead(reader);
64  
65          // reset to the mark and then read the rest
66          reader.reset();
67          assertEquals(string.substring(5), readFully(reader));
68          assertFullyRead(reader);
69      }
70  
71      public void testIllegalArguments() throws IOException {
72          try (CharSequenceReader reader = new CharSequenceReader("12345")) {
73  
74              char[] buf = new char[10];
75              try {
76                  reader.read(buf, 0, 11);
77                  fail();
78              } catch (IndexOutOfBoundsException expected) {
79              }
80  
81              try {
82                  reader.read(buf, 10, 1);
83                  fail();
84              } catch (IndexOutOfBoundsException expected) {
85              }
86  
87              try {
88                  reader.read(buf, 11, 0);
89                  fail();
90              } catch (IndexOutOfBoundsException expected) {
91              }
92  
93              try {
94                  reader.read(buf, -1, 5);
95                  fail();
96              } catch (IndexOutOfBoundsException expected) {
97              }
98  
99              try {
100                 reader.read(buf, 5, -1);
101                 fail();
102             } catch (IndexOutOfBoundsException expected) {
103             }
104 
105             try {
106                 reader.read(buf, 0, 11);
107                 fail();
108             } catch (IndexOutOfBoundsException expected) {
109             }
110 
111             try {
112                 reader.skip(-1);
113                 fail();
114             } catch (IllegalArgumentException expected) {
115             }
116 
117             try {
118                 reader.mark(-1);
119                 fail();
120             } catch (IllegalArgumentException expected) {
121             }
122         }
123     }
124 
125     public void testMethodsThrowWhenClosed() throws IOException {
126         CharSequenceReader reader = new CharSequenceReader("");
127         reader.close();
128 
129         try {
130             reader.read();
131             fail();
132         } catch (IOException expected) {
133         }
134 
135         try {
136             reader.read(new char[10]);
137             fail();
138         } catch (IOException expected) {
139         }
140 
141         try {
142             reader.read(new char[10], 0, 10);
143             fail();
144         } catch (IOException expected) {
145         }
146 
147         try {
148             reader.read(CharBuffer.allocate(10));
149             fail();
150         } catch (IOException expected) {
151         }
152 
153         try {
154             reader.skip(10);
155             fail();
156         } catch (IOException expected) {
157         }
158 
159         try {
160             reader.ready();
161             fail();
162         } catch (IOException expected) {
163         }
164 
165         try {
166             reader.mark(10);
167             fail();
168         } catch (IOException expected) {
169         }
170 
171         try {
172             reader.reset();
173             fail();
174         } catch (IOException expected) {
175         }
176     }
177 
178     /**
179      * Creates a CharSequenceReader wrapping the given CharSequence and tests that
180      * the reader produces the same sequence when read using each type of read
181      * method it provides.
182      */
183     private static void assertReadsCorrectly(CharSequence charSequence) throws IOException {
184         String expected = charSequence.toString();
185 
186         // read char by char
187         CharSequenceReader reader = new CharSequenceReader(charSequence);
188         for (int i = 0; i < expected.length(); i++) {
189             assertEquals(expected.charAt(i), reader.read());
190         }
191         assertFullyRead(reader);
192 
193         // read all to one array
194         reader = new CharSequenceReader(charSequence);
195         char[] buf = new char[expected.length()];
196         assertEquals(expected.length() == 0 ? -1 : expected.length(), reader.read(buf));
197         assertEquals(expected, new String(buf));
198         assertFullyRead(reader);
199 
200         // read in chunks to fixed array
201         reader = new CharSequenceReader(charSequence);
202         buf = new char[5];
203         StringBuilder builder = new StringBuilder();
204         int read;
205         while ((read = reader.read(buf, 0, buf.length)) != -1) {
206             builder.append(buf, 0, read);
207         }
208         assertEquals(expected, builder.toString());
209         assertFullyRead(reader);
210 
211         // read all to one CharBuffer
212         reader = new CharSequenceReader(charSequence);
213         CharBuffer buf2 = CharBuffer.allocate(expected.length());
214         assertEquals(expected.length() == 0 ? -1 : expected.length(), reader.read(buf2));
215         buf2.flip();
216         assertEquals(expected, buf2.toString());
217         assertFullyRead(reader);
218 
219         // read in chunks to fixed CharBuffer
220         reader = new CharSequenceReader(charSequence);
221         buf2 = CharBuffer.allocate(5);
222         builder = new StringBuilder();
223         while (reader.read(buf2) != -1) {
224             buf2.flip();
225             builder.append(buf2);
226             buf2.clear();
227         }
228         assertEquals(expected, builder.toString());
229         assertFullyRead(reader);
230 
231         // skip fully
232         reader = new CharSequenceReader(charSequence);
233         assertEquals(expected.length(), reader.skip(Long.MAX_VALUE));
234         assertFullyRead(reader);
235 
236         // skip 5 and read the rest
237         if (expected.length() > 5) {
238             reader = new CharSequenceReader(charSequence);
239             assertEquals(5, reader.skip(5));
240 
241             buf = new char[expected.length() - 5];
242             assertEquals(buf.length, reader.read(buf, 0, buf.length));
243             assertEquals(expected.substring(5), new String(buf));
244             assertFullyRead(reader);
245         }
246     }
247 
248     private static void assertFullyRead(CharSequenceReader reader) throws IOException {
249         assertEquals(-1, reader.read());
250         assertEquals(-1, reader.read(new char[10], 0, 10));
251         assertEquals(-1, reader.read(CharBuffer.allocate(10)));
252         assertEquals(0, reader.skip(10));
253     }
254 
255     private static String readFully(CharSequenceReader reader) throws IOException {
256         StringBuilder builder = new StringBuilder();
257         int read;
258         while ((read = reader.read()) != -1) {
259             builder.append((char) read);
260         }
261         return builder.toString();
262     }
263 }