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 18package libcore.java.io; 19 20import java.io.BufferedReader; 21import java.io.CharArrayReader; 22import java.io.InputStreamReader; 23import java.io.IOException; 24import java.io.OutputStreamWriter; 25import java.io.PipedInputStream; 26import java.io.PipedOutputStream; 27import java.io.PipedReader; 28import java.io.PrintWriter; 29import java.io.Reader; 30import java.io.StringReader; 31import tests.support.Support_ASimpleReader; 32import tests.support.Support_StringReader; 33import tests.support.ThrowingReader; 34 35public class OldBufferedReaderTest extends junit.framework.TestCase { 36 37 BufferedReader br; 38 39 String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n"; 40 41 public void test_ConstructorLjava_io_Reader() { 42 // Test for method java.io.BufferedReader(java.io.Reader) 43 br = new BufferedReader(new Support_StringReader(testString)); 44 assertNotNull(br); 45 } 46 47 public void test_ConstructorLjava_io_ReaderI() { 48 // Illegal negative size argument test. 49 try { 50 br = new BufferedReader(new Support_StringReader(testString), 0); 51 fail("IllegalArgumentException expected"); 52 } catch (IllegalArgumentException expected) { 53 } 54 br = new BufferedReader(new Support_StringReader(testString), 1024); 55 assertNotNull(br); 56 } 57 58 public void test_close() { 59 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 60 try { 61 br = new BufferedReader(new Support_StringReader(testString)); 62 br.close(); 63 br.read(); 64 fail("Test 1: Read on closed stream."); 65 } catch (IOException x) { 66 // Expected. 67 } catch (Exception e) { 68 fail("Exception during close test " + e.toString()); 69 } 70 71 br = new BufferedReader(ssr); 72 try { 73 br.close(); 74 fail("Test 2: IOException expected."); 75 } catch (IOException e) { 76 // Expected. 77 } 78 // Avoid IOException in tearDown(). 79 ssr.throwExceptionOnNextUse = false; 80 } 81 82 public void test_markI() throws IOException { 83 // Test for method void java.io.BufferedReader.mark(int) 84 char[] buf = null; 85 try { 86 br = new BufferedReader(new Support_StringReader(testString)); 87 br.skip(500); 88 br.mark(1000); 89 br.skip(250); 90 br.reset(); 91 buf = new char[testString.length()]; 92 br.read(buf, 0, 500); 93 assertTrue("Failed to set mark properly", testString.substring(500, 94 1000).equals(new String(buf, 0, 500))); 95 } catch (java.io.IOException e) { 96 fail("Exception during mark test"); 97 } 98 try { 99 br = new BufferedReader(new Support_StringReader(testString), 800); 100 br.skip(500); 101 br.mark(250); 102 br.read(buf, 0, 1000); 103 br.reset(); 104 fail("Failed to invalidate mark properly"); 105 } catch (IOException x) { 106 } 107 108 char[] chars = new char[256]; 109 for (int i = 0; i < 256; i++) 110 chars[i] = (char) i; 111 Reader in = new BufferedReader(new Support_StringReader(new String( 112 chars)), 12); 113 try { 114 in.skip(6); 115 in.mark(14); 116 in.read(new char[14], 0, 14); 117 in.reset(); 118 assertTrue("Wrong chars", in.read() == (char) 6 119 && in.read() == (char) 7); 120 } catch (IOException e) { 121 fail("Exception during mark test 2:" + e); 122 } 123 124 in = new BufferedReader(new Support_StringReader(new String(chars)), 12); 125 try { 126 in.skip(6); 127 in.mark(8); 128 in.skip(7); 129 in.reset(); 130 assertTrue("Wrong chars 2", in.read() == (char) 6 131 && in.read() == (char) 7); 132 } catch (IOException e) { 133 fail("Exception during mark test 3:" + e); 134 } 135 } 136 137 public void test_markSupported() { 138 // Test for method boolean java.io.BufferedReader.markSupported() 139 br = new BufferedReader(new Support_StringReader(testString)); 140 assertTrue("markSupported returned false.", br.markSupported()); 141 } 142 143 public void test_read() throws IOException { 144 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 145 try { 146 br = new BufferedReader(new Support_StringReader(testString)); 147 int r = br.read(); 148 assertTrue("Char read improperly", testString.charAt(0) == r); 149 br = new BufferedReader(new Support_StringReader(new String( 150 new char[] { '\u8765' }))); 151 assertTrue("Wrong double byte character", br.read() == '\u8765'); 152 } catch (java.io.IOException e) { 153 fail("Exception during read test"); 154 } 155 156 char[] chars = new char[256]; 157 for (int i = 0; i < 256; i++) 158 chars[i] = (char) i; 159 Reader in = new BufferedReader(new Support_StringReader(new String( 160 chars)), 12); 161 try { 162 assertEquals("Wrong initial char", 0, in.read()); // Fill the 163 // buffer 164 char[] buf = new char[14]; 165 in.read(buf, 0, 14); // Read greater than the buffer 166 assertTrue("Wrong block read data", new String(buf) 167 .equals(new String(chars, 1, 14))); 168 assertEquals("Wrong chars", 15, in.read()); // Check next byte 169 } catch (IOException e) { 170 fail("Exception during read test 2:" + e); 171 } 172 173 // regression test for HARMONY-841 174 assertTrue(new BufferedReader(new CharArrayReader(new char[5], 1, 0), 2).read() == -1); 175 176 br.close(); 177 br = new BufferedReader(ssr); 178 try { 179 br.read(); 180 fail("IOException expected."); 181 } catch (IOException e) { 182 // Expected. 183 } 184 // Avoid IOException in tearDown(). 185 ssr.throwExceptionOnNextUse = false; 186 } 187 188 public void test_read$CII_Exception() throws Exception { 189 br = new BufferedReader(new Support_StringReader(testString)); 190 try{ 191 br.read(new char[10], -1, 1); 192 fail("should throw IndexOutOfBoundsException"); 193 } catch (IndexOutOfBoundsException e) { 194 // Expected 195 } 196 197 try{ 198 br.read(new char[10], 0, -1); 199 fail("should throw IndexOutOfBoundsException"); 200 } catch (IndexOutOfBoundsException e) { 201 // Expected 202 } 203 204 try{ 205 br.read(new char[10], 10, 1); 206 fail("should throw IndexOutOfBoundsException"); 207 } catch (IndexOutOfBoundsException e) { 208 // Expected 209 } 210 211 //regression for HARMONY-831 212 try{ 213 new BufferedReader(new PipedReader(), 9).read(new char[] {}, 7, 0); 214 fail("should throw IndexOutOfBoundsException"); 215 }catch(IndexOutOfBoundsException e){ 216 } 217 } 218 219 public void test_readLine() throws IOException { 220 String line; 221 br = new BufferedReader(new Support_StringReader("Lorem\nipsum\rdolor sit amet...")); 222 223 line = br.readLine(); 224 assertTrue("Test 1: Incorrect line written or read: " + line, 225 line.equals("Lorem")); 226 line = br.readLine(); 227 assertTrue("Test 2: Incorrect line written or read: " + line, 228 line.equals("ipsum")); 229 line = br.readLine(); 230 assertTrue("Test 3: Incorrect line written or read: " + line, 231 line.equals("dolor sit amet...")); 232 233 br.close(); 234 try { 235 br.readLine(); 236 fail("Test 4: IOException expected."); 237 } catch (IOException e) { 238 // Expected. 239 } 240 } 241 242 public void test_ready() throws IOException { 243 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 244 try { 245 br = new BufferedReader(new Support_StringReader(testString)); 246 assertTrue("Test 1: ready() returned false", br.ready()); 247 } catch (java.io.IOException e) { 248 fail("Exception during ready test" + e.toString()); 249 } 250 251 br.close(); 252 br = new BufferedReader(ssr); 253 try { 254 br.close(); 255 fail("Test 2: IOException expected."); 256 } catch (IOException e) { 257 // Expected. 258 } 259 // Avoid IOException in tearDown(). 260 ssr.throwExceptionOnNextUse = false; 261 } 262 263 public void test_skipJ() throws IOException { 264 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 265 br = new BufferedReader(new Support_StringReader(testString)); 266 267 try { 268 br.skip(-1); 269 fail("Test 1: IllegalArgumentException expected."); 270 } catch (IllegalArgumentException e) { 271 // Expected. 272 } 273 274 br.skip(500); 275 char[] buf = new char[testString.length()]; 276 br.read(buf, 0, 500); 277 assertTrue("Test 2: Failed to set skip properly.", 278 testString.substring(500, 1000).equals( 279 new String(buf, 0, 500))); 280 281 br.close(); 282 br = new BufferedReader(ssr); 283 try { 284 br.skip(1); 285 fail("Test 3: IOException expected."); 286 } catch (IOException e) { 287 // Expected. 288 } 289 // Avoid IOException in tearDown(). 290 ssr.throwExceptionOnNextUse = false; 291 } 292 293 public void testReadZeroLengthArray() throws IOException { 294 br = new BufferedReader(new Support_StringReader("ABCDEF")); 295 br.read(); 296 br.read(); 297 assertEquals(0, br.read(new char[6], 3, 0)); 298 } 299 300 public void testSourceThrowsWithMark() throws IOException { 301 br = new BufferedReader(new ThrowingReader( 302 new StringReader("ABCDEFGHI"), 4)); 303 304 br.read(); 305 br.read(); 306 br.mark(10); 307 br.read(); 308 br.read(); 309 310 try { 311 br.read(); 312 fail(); 313 } catch (IOException fromThrowingReader) { 314 } 315 316 assertEquals('E', br.read()); 317 assertEquals('F', br.read()); 318 } 319 320 protected void tearDown() { 321 try { 322 br.close(); 323 } catch (Exception e) { 324 } 325 } 326 327 public void test_readLine_all_line_endings() throws Exception { 328 BufferedReader r = new BufferedReader(new StringReader("1\r2\n3\r\n4")); 329 assertEquals("1", r.readLine()); 330 assertEquals("2", r.readLine()); 331 assertEquals("3", r.readLine()); 332 assertEquals("4", r.readLine()); 333 assertNull(r.readLine()); 334 } 335 336 public void test_readLine_interaction_with_read() throws Exception { 337 BufferedReader r = new BufferedReader(new StringReader("1\r\n2")); 338 assertEquals('1', r.read()); 339 assertEquals('\r', r.read()); 340 assertEquals("", r.readLine()); // The '\r' we read() didn't count. 341 assertEquals("2", r.readLine()); 342 assertNull(r.readLine()); 343 } 344 345 public void test_readLine_interaction_with_array_read_1() throws Exception { 346 BufferedReader r = new BufferedReader(new StringReader("1\r\n2")); 347 assertEquals(2, r.read(new char[2], 0, 2)); 348 assertEquals("", r.readLine()); // The '\r' we read() didn't count. 349 assertEquals("2", r.readLine()); 350 assertNull(r.readLine()); 351 } 352 353 public void test_readLine_interaction_with_array_read_2() throws Exception { 354 BufferedReader r = new BufferedReader(new StringReader("1\r\n2")); 355 assertEquals("1", r.readLine()); 356 char[] chars = new char[1]; 357 assertEquals(1, r.read(chars, 0, 1)); // This read skips the '\n'. 358 assertEquals('2', chars[0]); 359 assertNull(r.readLine()); 360 } 361 362 public void test_readLine_interaction_with_skip() throws Exception { 363 BufferedReader r = new BufferedReader(new StringReader("1\r\n2")); 364 assertEquals(2, r.skip(2)); 365 assertEquals("", r.readLine()); // The '\r' we skip()ed didn't count. 366 assertEquals("2", r.readLine()); 367 assertNull(r.readLine()); 368 } 369 370 public void test_readLine_interaction_with_mark_and_reset() throws Exception { 371 BufferedReader r = new BufferedReader(new StringReader("1\r\n2\n3")); 372 assertEquals("1", r.readLine()); 373 r.mark(256); 374 assertEquals('2', r.read()); // This read skips the '\n'. 375 assertEquals("", r.readLine()); 376 r.reset(); // Now we're back half-way through the "\r\n". 377 assertEquals("2", r.readLine()); 378 assertEquals("3", r.readLine()); 379 assertNull(r.readLine()); 380 } 381 382 public void test_8778372() throws Exception { 383 final PipedInputStream pis = new PipedInputStream(); 384 final PipedOutputStream pos = new PipedOutputStream(pis); 385 final Thread t = new Thread() { 386 @Override public void run() { 387 PrintWriter pw = new PrintWriter(new OutputStreamWriter(pos)); 388 pw.print("hello, world\r"); 389 pw.flush(); 390 try { 391 Thread.sleep(2*60*1000); 392 } catch (InterruptedException ex) { 393 fail(); 394 } 395 } 396 }; 397 t.start(); 398 BufferedReader br = new BufferedReader(new InputStreamReader(pis)); 399 assertEquals("hello, world", br.readLine()); 400 } 401} 402