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    *     https://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  
18  package org.apache.commons.io.input;
19  
20  import static org.apache.commons.io.IOUtils.EOF;
21  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.CharArrayReader;
29  import java.io.IOException;
30  import java.io.InputStreamReader;
31  import java.io.PipedReader;
32  import java.io.Reader;
33  import java.io.StringReader;
34  import java.util.function.Supplier;
35  
36  import org.apache.commons.io.IOUtils;
37  import org.junit.jupiter.api.AfterEach;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Tests {@link UnsynchronizedBufferedReader}.
42   * <p>
43   * Provenance: Apache Harmony {@code BufferedReaderTest}, copied, and modified.
44   * </p>
45   */
46  class UnsynchronizedBufferedReaderTest {
47  
48      private UnsynchronizedBufferedReader br;
49  
50      private final String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\n"
51              + "Test_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\n"
52              + "Test_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\n"
53              + "Test_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\n"
54              + "Test_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\n"
55              + "Test_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\n"
56              + "Test_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\n"
57              + "Test_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\n"
58              + "Test_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\n"
59              + "Test_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\n"
60              + "Test_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\n"
61              + "Test_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\n"
62              + "Test_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\n"
63              + "Test_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\n"
64              + "Test_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\n"
65              + "Test_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\n"
66              + "Test_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\n"
67              + "Test_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\n"
68              + "Test_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\n"
69              + "Test_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\n"
70              + "Test_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\n"
71              + "Test_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\n"
72              + "Test_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\n"
73              + "Test_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\n"
74              + "Test_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\n"
75              + "Test_java_util_tm\nTest_java_util_Vector\n";
76  
77      /**
78       * Tears down the fixture, for example, close a network connection. This method is called after a test is executed.
79       */
80      @AfterEach
81      protected void afterEach() {
82          IOUtils.closeQuietly(br);
83      }
84  
85      private void assertLines(final String input, final String... lines) throws IOException {
86          assertReadLines(input, lines);
87          assertPeek(input, lines);
88      }
89  
90      private void assertPeek(final String input, final String... lines) throws IOException {
91          try (UnsynchronizedBufferedReader bufferedReader = new UnsynchronizedBufferedReader(new StringReader(input))) {
92              for (final String line : lines) {
93                  // all
94                  final char[] bufAFull = new char[line.length()];
95                  assertEquals(bufAFull.length, bufferedReader.peek(bufAFull));
96                  assertArrayEquals(line.toCharArray(), bufAFull);
97                  if (!line.isEmpty()) {
98                      // one
99                      assertEquals(line.charAt(0), bufferedReader.peek());
100                     // array
101                     for (int peekLen = 0; peekLen < line.length(); peekLen++) {
102                         assertPeekArray(bufferedReader, peekLen, line);
103                     }
104                 }
105                 // move test to the next fixture
106                 assertEquals(line, bufferedReader.readLine());
107             }
108             assertNull(bufferedReader.readLine());
109         }
110     }
111 
112     private void assertPeekArray(final UnsynchronizedBufferedReader bufferedReader, final int peekLen, final String line) throws IOException {
113         final char[] expectedBuf = new char[peekLen];
114         final int srcPeekLen = Math.min(peekLen, line.length());
115         line.getChars(0, srcPeekLen, expectedBuf, 0);
116         final char[] actualBuf = new char[peekLen];
117         final Supplier<String> msg = () -> String.format("len=%,d, line='%s'", peekLen, line);
118         assertEquals(actualBuf.length, bufferedReader.peek(actualBuf), msg);
119         assertArrayEquals(expectedBuf, actualBuf, msg);
120     }
121 
122     private void assertReadLines(final String input, final String... lines) throws IOException {
123         try (UnsynchronizedBufferedReader bufferedReader = new UnsynchronizedBufferedReader(new StringReader(input))) {
124             for (final String line : lines) {
125                 assertEquals(line, bufferedReader.readLine());
126             }
127             assertNull(bufferedReader.readLine());
128         }
129     }
130 
131     /**
132      * Tests {@link UnsynchronizedBufferedReader#close()}.
133      *
134      * @throws IOException test failure.
135      */
136     @Test
137     void testClose() throws IOException {
138         // Test for method void UnsynchronizedBufferedReader.close()
139         br = new UnsynchronizedBufferedReader(new StringReader(testString));
140         br.close();
141         assertThrows(IOException.class, br::read);
142     }
143 
144     @Test
145     void testEmptyInput() throws Exception {
146         try (UnsynchronizedBufferedReader br = new UnsynchronizedBufferedReader(new StringReader(""))) {
147             assertEquals(EOF, br.read());
148             assertEquals(EOF, br.peek());
149             assertNull(br.readLine());
150             assertEquals(0, br.read(new char[10], 0, 0));
151         }
152     }
153 
154     @Test
155     void testIllegalSize() throws Exception {
156         assertThrows(IllegalArgumentException.class, () -> new UnsynchronizedBufferedReader(new StringReader(""), 0));
157         assertThrows(IllegalArgumentException.class, () -> new UnsynchronizedBufferedReader(new StringReader(""), -1));
158     }
159 
160     /**
161      * Tests {@link UnsynchronizedBufferedReader#mark(int)}.
162      *
163      * @throws IOException test failure.
164      */
165     @Test
166     void testMark() throws IOException {
167         // Test for method void UnsynchronizedBufferedReader.mark(int)
168         char[] buf = null;
169         br = new UnsynchronizedBufferedReader(new StringReader(testString));
170         br.skip(500);
171         br.mark(1000);
172         br.skip(250);
173         br.reset();
174         buf = new char[testString.length()];
175         br.read(buf, 0, 500);
176         assertTrue(testString.substring(500, 1000).equals(new String(buf, 0, 500)));
177 
178         br = new UnsynchronizedBufferedReader(new StringReader(testString), 800);
179         br.skip(500);
180         br.mark(250);
181         br.read(buf, 0, 1000);
182         assertThrows(IOException.class, br::reset);
183 
184         final char[] chars = new char[256];
185         for (int i = 0; i < 256; i++) {
186             chars[i] = (char) i;
187         }
188 
189         try (Reader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) {
190             in.skip(6);
191             in.mark(14);
192             in.read(new char[14], 0, 14);
193             in.reset();
194             assertTrue(in.read() == (char) 6 && in.read() == (char) 7);
195         }
196         try (Reader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) {
197             in.skip(6);
198             in.mark(8);
199             in.skip(7);
200             in.reset();
201             assertTrue(in.read() == (char) 6 && in.read() == (char) 7);
202         }
203         try (UnsynchronizedBufferedReader br = new UnsynchronizedBufferedReader(new StringReader("01234"), 2)) {
204             br.mark(3);
205             final char[] carray = new char[3];
206             final int result = br.read(carray);
207             assertEquals(3, result);
208             assertEquals('0', carray[0]);
209             assertEquals('1', carray[1]);
210             assertEquals('2', carray[2]);
211             assertEquals('3', br.read());
212         }
213         try (UnsynchronizedBufferedReader br = new UnsynchronizedBufferedReader(new StringReader("01234"), 2)) {
214             br.mark(3);
215             final char[] carray = new char[4];
216             final int result = br.read(carray);
217             assertEquals(4, result);
218             assertEquals('0', carray[0]);
219             assertEquals('1', carray[1]);
220             assertEquals('2', carray[2]);
221             assertEquals('3', carray[3]);
222             assertEquals('4', br.read());
223             assertEquals(-1, br.read());
224         }
225         try (UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new StringReader("01234"))) {
226             reader.mark(Integer.MAX_VALUE);
227             reader.read();
228         }
229     }
230 
231     /**
232      * Tests {@link UnsynchronizedBufferedReader#mark(int)}.
233      *
234      * @throws IOException test failure.
235      */
236     @Test
237     void testMarkIllegal() throws IOException {
238         br = new UnsynchronizedBufferedReader(new StringReader(testString));
239         assertThrows(IllegalArgumentException.class, () -> br.mark(-1));
240     }
241 
242     /**
243      * Tests {@link UnsynchronizedBufferedReader#markSupported()}.
244      */
245     @Test
246     void testMarkSupported() {
247         // Test for method boolean UnsynchronizedBufferedReader.markSupported()
248         br = new UnsynchronizedBufferedReader(new StringReader(testString));
249         assertTrue(br.markSupported());
250     }
251 
252     /**
253      * Tests {@link UnsynchronizedBufferedReader#peek()}.
254      *
255      * @throws IOException test failure.
256      */
257     @Test
258     void testPeek() throws IOException {
259         // Test for method int UnsynchronizedBufferedReader.read()
260         br = new UnsynchronizedBufferedReader(new StringReader(testString));
261         final int p = br.peek();
262         assertEquals(testString.charAt(0), p);
263         final int r = br.read();
264         assertEquals(testString.charAt(0), r);
265         br = new UnsynchronizedBufferedReader(new StringReader(new String(new char[] { '\u8765' })));
266         assertEquals(br.peek(), '\u8765');
267         assertEquals(br.read(), '\u8765');
268         // chars '\0'...'\255'
269         final char[] chars = new char[256];
270         for (int i = 0; i < 256; i++) {
271             chars[i] = (char) i;
272         }
273         try (UnsynchronizedBufferedReader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) {
274             assertEquals(0, in.peek()); // Fill the buffer
275             assertEquals(0, in.read()); // Fill the buffer
276             final char[] buf = new char[14];
277             in.read(buf, 0, 14); // Read greater than the buffer
278             assertTrue(new String(buf).equals(new String(chars, 1, 14)));
279             assertEquals(15, in.peek()); // Check next byte
280             assertEquals(15, in.read()); // Check next byte
281         }
282         //
283         // regression test for HARMONY-841
284         try (UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new CharArrayReader(new char[5], 1, 0), 2)) {
285             assertEquals(reader.peek(), -1);
286             assertEquals(reader.read(), -1);
287         }
288     }
289 
290     /**
291      * Tests {@link UnsynchronizedBufferedReader#peek()}.
292      *
293      * @throws IOException test failure.
294      */
295     @Test
296     void testPeekArray() throws IOException {
297         // Test for method int UnsynchronizedBufferedReader.read()
298         final char[] peekBuf1 = new char[1];
299         br = new UnsynchronizedBufferedReader(new StringReader(testString));
300         assertEquals(peekBuf1.length, br.peek(peekBuf1));
301         assertEquals(testString.charAt(0), peekBuf1[0]);
302         final int r = br.read();
303         assertEquals(testString.charAt(0), r);
304         br = new UnsynchronizedBufferedReader(new StringReader(new String(new char[] { '\u8765' })));
305         assertEquals(peekBuf1.length, br.peek(peekBuf1));
306         assertEquals(peekBuf1[0], '\u8765');
307         assertEquals(br.read(), '\u8765');
308         // chars '\0'...'\255'
309         final char[] chars = new char[256];
310         for (int i = 0; i < 256; i++) {
311             chars[i] = (char) i;
312         }
313         try (UnsynchronizedBufferedReader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) {
314             assertEquals(peekBuf1.length, in.peek(peekBuf1)); // Fill the buffer
315             assertEquals(peekBuf1[0], 0);
316             assertEquals(0, in.read()); // Fill the buffer
317             final char[] peekBuf14 = new char[14];
318             assertEquals(peekBuf14.length, in.peek(peekBuf14)); // Peek greater than the buffer
319             assertTrue(new String(peekBuf14).equals(new String(chars, 1, 14)));
320             final char[] buf = new char[14];
321             in.read(buf, 0, 14); // Read greater than the buffer
322             assertTrue(new String(buf).equals(new String(chars, 1, 14)));
323             assertEquals(peekBuf1.length, in.peek(peekBuf1)); // Check next byte
324             assertEquals(15, peekBuf1[0]);
325             assertEquals(15, in.read()); // Check next byte
326         }
327         //
328         // regression test for HARMONY-841
329         try (UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new CharArrayReader(new char[5], 1, 0), 2)) {
330             assertEquals(reader.peek(), -1);
331             assertEquals(reader.read(), -1);
332         }
333     }
334 
335     /**
336      * Tests {@link UnsynchronizedBufferedReader#read()}.
337      *
338      * @throws IOException test failure.
339      */
340     @Test
341     void testRead() throws IOException {
342         // Test for method int UnsynchronizedBufferedReader.read()
343         br = new UnsynchronizedBufferedReader(new StringReader(testString));
344         final int r = br.read();
345         assertEquals(testString.charAt(0), r);
346         br = new UnsynchronizedBufferedReader(new StringReader(new String(new char[] { '\u8765' })));
347         assertEquals(br.read(), '\u8765');
348         //
349         final char[] chars = new char[256];
350         for (int i = 0; i < 256; i++) {
351             chars[i] = (char) i;
352         }
353         try (Reader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) {
354             assertEquals(0, in.read()); // Fill the buffer
355             final char[] buf = new char[14];
356             in.read(buf, 0, 14); // Read greater than the buffer
357             assertTrue(new String(buf).equals(new String(chars, 1, 14)));
358             assertEquals(15, in.read()); // Check next byte
359         }
360         //
361         // regression test for HARMONY-841
362         try (Reader reader = new UnsynchronizedBufferedReader(new CharArrayReader(new char[5], 1, 0), 2)) {
363             assertEquals(reader.read(), -1);
364         }
365     }
366 
367     @Test
368     void testReadArray_HARMONY_54() throws IOException {
369         // Regression for HARMONY-54
370         final char[] ch = {};
371         @SuppressWarnings("resource")
372         final UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new CharArrayReader(ch));
373         // Check exception thrown when the reader is open.
374         assertThrows(NullPointerException.class, () -> reader.read(null, 1, 0));
375 
376         // Now check IOException is thrown in preference to
377         // NullPointerexception when the reader is closed.
378         reader.close();
379         assertThrows(IOException.class, () -> reader.read(null, 1, 0));
380 
381         // And check that the IOException is thrown before
382         // ArrayIndexOutOfBoundException
383         assertThrows(IOException.class, () -> reader.read(ch, 0, 42));
384     }
385 
386     @Test
387     void testReadArray_HARMONY_831() throws IOException {
388         // regression for HARMONY-831
389         try (Reader reader = new UnsynchronizedBufferedReader(new PipedReader(), 9)) {
390             assertThrows(IndexOutOfBoundsException.class, () -> reader.read(new char[] {}, 7, 0));
391         }
392     }
393 
394     /**
395      * Tests {@link UnsynchronizedBufferedReader#read(char[], int, int)}.
396      *
397      * @throws IOException test failure.
398      */
399     @Test
400     void testReadArray1() throws IOException {
401         final char[] ca = new char[2];
402         try (UnsynchronizedBufferedReader toRet = new UnsynchronizedBufferedReader(new InputStreamReader(new ByteArrayInputStream(new byte[0])))) {
403             /* Validate parameters, before returning 0 */
404             assertThrows(NullPointerException.class, () -> toRet.read(null, 1, 0));
405             assertThrows(IndexOutOfBoundsException.class, () -> toRet.read(ca, 1, 5));
406             /* Read zero bytes should return 0 */
407             assertEquals(0, toRet.read(ca, 0, 0));
408             toRet.close();
409             /*
410              * After close, readers in java.io consistently throw IOException before checking parameters or returning 0.
411              */
412             assertThrows(IOException.class, () -> toRet.read(null, 1, 0));
413             assertThrows(IOException.class, () -> toRet.read(ca, 1, 5));
414             assertThrows(IOException.class, () -> toRet.read(ca, 0, 0));
415         }
416     }
417 
418     @Test
419     void testReadArray2() throws IOException {
420         final char[] ca = new char[2];
421         // Test to ensure that a drained stream returns 0 at EOF
422         try (UnsynchronizedBufferedReader toRet2 = new UnsynchronizedBufferedReader(new InputStreamReader(new ByteArrayInputStream(new byte[2])))) {
423             assertEquals(2, toRet2.read(ca, 0, 2));
424             assertEquals(-1, toRet2.read(ca, 0, 2));
425             assertEquals(0, toRet2.read(ca, 0, 0));
426         }
427     }
428 
429     @Test
430     void testReadArray3() throws IOException {
431         // Test for method int UnsynchronizedBufferedReader.read(char [], int, int)
432         final char[] buf = new char[testString.length()];
433         br = new UnsynchronizedBufferedReader(new StringReader(testString));
434         br.read(buf, 50, 500);
435         assertTrue(new String(buf, 50, 500).equals(testString.substring(0, 500)));
436     }
437 
438     @Test
439     void testReadArray4() throws IOException {
440         try (UnsynchronizedBufferedReader bufin = new UnsynchronizedBufferedReader(new Reader() {
441             int size = 2;
442             int pos;
443 
444             char[] contents = new char[size];
445 
446             @Override
447             public void close() throws IOException {
448                 // Empty
449             }
450 
451             @Override
452             public int read() throws IOException {
453                 if (pos >= size) {
454                     throw new IOException("Read past end of data");
455                 }
456                 return contents[pos++];
457             }
458 
459             @Override
460             public int read(final char[] buf, final int off, final int len) throws IOException {
461                 if (pos >= size) {
462                     throw new IOException("Read past end of data");
463                 }
464                 int toRead = len;
465                 if (toRead > size - pos) {
466                     toRead = size - pos;
467                 }
468                 System.arraycopy(contents, pos, buf, off, toRead);
469                 pos += toRead;
470                 return toRead;
471             }
472 
473             @Override
474             public boolean ready() throws IOException {
475                 return size - pos > 0;
476             }
477         })) {
478             bufin.read();
479             final int result = bufin.read(new char[2], 0, 2);
480             assertEquals(result, 1);
481         }
482     }
483 
484     /**
485      * Tests {@link UnsynchronizedBufferedReader#read(char[], int, int)}.
486      *
487      * @throws IOException test failure.
488      */
489     @Test
490     void testReadArrayException() throws IOException {
491         br = new UnsynchronizedBufferedReader(new StringReader(testString));
492         final char[] nullCharArray = null;
493         final char[] charArray = testString.toCharArray();
494         assertThrows(NullPointerException.class, () -> br.read(nullCharArray, -1, 0));
495         assertThrows(NullPointerException.class, () -> br.read(nullCharArray, 0, -1));
496         assertThrows(NullPointerException.class, () -> br.read(nullCharArray, 1, 1));
497         assertThrows(IndexOutOfBoundsException.class, () -> br.read(charArray, -1, 0));
498         assertThrows(IndexOutOfBoundsException.class, () -> br.read(charArray, 0, -1));
499         assertThrows(IndexOutOfBoundsException.class, () -> br.read(charArray, charArray.length, 1));
500 
501         br.read(charArray, 0, 0);
502         br.read(charArray, 0, charArray.length);
503         br.read(charArray, charArray.length, 0);
504 
505         assertThrows(IndexOutOfBoundsException.class, () -> br.read(charArray, charArray.length + 1, 0));
506         assertThrows(IndexOutOfBoundsException.class, () -> br.read(charArray, charArray.length + 1, 1));
507 
508         br.close();
509 
510         assertThrows(IOException.class, () -> br.read(nullCharArray, -1, -1));
511         assertThrows(IOException.class, () -> br.read(charArray, -1, 0));
512         assertThrows(IOException.class, () -> br.read(charArray, 0, -1));
513     }
514 
515     /**
516      * Tests {@link UnsynchronizedBufferedReader#readLine()}.
517      *
518      * @throws IOException test failure.
519      */
520     @Test
521     void testReadLine() throws IOException {
522         // Test for method java.lang.String UnsynchronizedBufferedReader.readLine()
523         br = new UnsynchronizedBufferedReader(new StringReader(testString));
524         final String r = br.readLine();
525         assertEquals("Test_All_Tests", r);
526     }
527 
528     /**
529      * The spec says that BufferedReader.readLine() considers only "\r", "\n" and "\r\n" to be line separators. We must not permit additional separator
530      * characters.
531      *
532      * @throws IOException test failure.
533      */
534     @Test
535     void testReadLineIgnoresEbcdic85Characters() throws IOException {
536         assertLines("A\u0085B", "A\u0085B");
537     }
538 
539     @Test
540     void testReadLineSeparators() throws IOException {
541         assertLines("A\nB\nC", "A", "B", "C");
542         assertLines("A\rB\rC", "A", "B", "C");
543         assertLines("A\r\nB\r\nC", "A", "B", "C");
544         assertLines("A\n\rB\n\rC", "A", "", "B", "", "C");
545         assertLines("A\n\nB\n\nC", "A", "", "B", "", "C");
546         assertLines("A\r\rB\r\rC", "A", "", "B", "", "C");
547         assertLines("A\n\n", "A", "");
548         assertLines("A\n\r", "A", "");
549         assertLines("A\r\r", "A", "");
550         assertLines("A\r\n", "A");
551         assertLines("A\r\n\r\n", "A", "");
552     }
553 
554     /**
555      * Tests {@link UnsynchronizedBufferedReader#ready()}.
556      *
557      * @throws IOException test failure.
558      */
559     @Test
560     void testReady() throws IOException {
561         // Test for method boolean UnsynchronizedBufferedReader.ready()
562         br = new UnsynchronizedBufferedReader(new StringReader(testString));
563         assertTrue(br.ready());
564     }
565 
566     /**
567      * Tests {@link UnsynchronizedBufferedReader#reset()}.
568      *
569      * @throws IOException test failure.
570      */
571     @Test
572     void testReset() throws IOException {
573         // Test for method void UnsynchronizedBufferedReader.reset()
574         br = new UnsynchronizedBufferedReader(new StringReader(testString));
575         br.skip(500);
576         br.mark(900);
577         br.skip(500);
578         br.reset();
579         final char[] buf = new char[testString.length()];
580         br.read(buf, 0, 500);
581         assertTrue(testString.substring(500, 1000).equals(new String(buf, 0, 500)));
582         br = new UnsynchronizedBufferedReader(new StringReader(testString));
583         br.skip(500);
584         assertThrows(IOException.class, br::reset);
585     }
586 
587     @Test
588     void testReset_IOException() throws Exception {
589         final int[] expected = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', -1 };
590         br = new UnsynchronizedBufferedReader(new StringReader("1234567890"), 9);
591         br.mark(9);
592         for (int i = 0; i < 11; i++) {
593             assertEquals(expected[i], br.read());
594         }
595         assertThrows(IOException.class, br::reset);
596         for (int i = 0; i < 11; i++) {
597             assertEquals(-1, br.read());
598         }
599 
600         br = new UnsynchronizedBufferedReader(new StringReader("1234567890"));
601         br.mark(10);
602         for (int i = 0; i < 10; i++) {
603             assertEquals(expected[i], br.read());
604         }
605         br.reset();
606         for (int i = 0; i < 11; i++) {
607             assertEquals(expected[i], br.read());
608         }
609     }
610 
611     /**
612      * Tests {@link UnsynchronizedBufferedReader#skip(long)}.
613      *
614      * @throws IOException test failure.
615      */
616     @Test
617     void testSkip() throws IOException {
618         // Test for method long UnsynchronizedBufferedReader.skip(long)
619         br = new UnsynchronizedBufferedReader(new StringReader(testString));
620         br.skip(500);
621         final char[] buf = new char[testString.length()];
622         br.read(buf, 0, 500);
623         assertTrue(testString.substring(500, 1000).equals(new String(buf, 0, 500)));
624     }
625 
626     /**
627      * Tests {@link UnsynchronizedBufferedReader#skip(int)}.
628      *
629      * @throws IOException test failure.
630      */
631     @Test
632     void testSkipIllegal() throws IOException {
633         br = new UnsynchronizedBufferedReader(new StringReader(testString));
634         assertThrows(IllegalArgumentException.class, () -> br.skip(-1));
635     }
636 }