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 tests.api.java.io; 19 20import java.io.BufferedReader; 21import java.io.ByteArrayInputStream; 22import java.io.CharArrayReader; 23import java.io.IOException; 24import java.io.InputStreamReader; 25import java.io.PipedReader; 26import java.io.Reader; 27import java.io.StringReader; 28 29import tests.support.Support_ASimpleReader; 30import tests.support.Support_StringReader; 31import dalvik.annotation.TestLevel; 32import dalvik.annotation.TestTargetClass; 33import dalvik.annotation.TestTargetNew; 34import tests.support.ThrowingReader; 35 36@TestTargetClass(BufferedReader.class) 37public class BufferedReaderTest extends junit.framework.TestCase { 38 39 BufferedReader br; 40 41 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"; 42 43 /** 44 * @tests java.io.BufferedReader#BufferedReader(java.io.Reader) 45 */ 46 @TestTargetNew( 47 level = TestLevel.COMPLETE, 48 method = "BufferedReader", 49 args = {java.io.Reader.class} 50 ) 51 public void test_ConstructorLjava_io_Reader() { 52 // Test for method java.io.BufferedReader(java.io.Reader) 53 br = new BufferedReader(new Support_StringReader(testString)); 54 assertNotNull(br); 55 } 56 57 /** 58 * @tests java.io.BufferedReader#BufferedReader(java.io.Reader, int) 59 */ 60 @TestTargetNew( 61 level = TestLevel.COMPLETE, 62 method = "BufferedReader", 63 args = {java.io.Reader.class, int.class} 64 ) 65 public void test_ConstructorLjava_io_ReaderI() { 66 // Illegal negative size argument test. 67 try { 68 br = new BufferedReader(new Support_StringReader(testString), 0); 69 fail("IllegalArgumentException expected"); 70 } catch (IllegalArgumentException expected) { 71 } 72 br = new BufferedReader(new Support_StringReader(testString), 1024); 73 assertNotNull(br); 74 } 75 76 /** 77 * @tests java.io.BufferedReader#close() 78 */ 79 @TestTargetNew( 80 level = TestLevel.COMPLETE, 81 method = "close", 82 args = {} 83 ) 84 public void test_close() { 85 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 86 try { 87 br = new BufferedReader(new Support_StringReader(testString)); 88 br.close(); 89 br.read(); 90 fail("Test 1: Read on closed stream."); 91 } catch (IOException x) { 92 // Expected. 93 } catch (Exception e) { 94 fail("Exception during close test " + e.toString()); 95 } 96 97 br = new BufferedReader(ssr); 98 try { 99 br.close(); 100 fail("Test 2: IOException expected."); 101 } catch (IOException e) { 102 // Expected. 103 } 104 // Avoid IOException in tearDown(). 105 ssr.throwExceptionOnNextUse = false; 106 } 107 108 /** 109 * @tests java.io.BufferedReader#mark(int) 110 */ 111 @TestTargetNew( 112 level = TestLevel.PARTIAL_COMPLETE, 113 method = "mark", 114 args = {int.class} 115 ) 116 public void test_markI() { 117 // Test for method void java.io.BufferedReader.mark(int) 118 char[] buf = null; 119 try { 120 br = new BufferedReader(new Support_StringReader(testString)); 121 br.skip(500); 122 br.mark(1000); 123 br.skip(250); 124 br.reset(); 125 buf = new char[testString.length()]; 126 br.read(buf, 0, 500); 127 assertTrue("Failed to set mark properly", testString.substring(500, 128 1000).equals(new String(buf, 0, 500))); 129 } catch (java.io.IOException e) { 130 fail("Exception during mark test"); 131 } 132 try { 133 br = new BufferedReader(new Support_StringReader(testString), 800); 134 br.skip(500); 135 br.mark(250); 136 br.read(buf, 0, 1000); 137 br.reset(); 138 fail("Failed to invalidate mark properly"); 139 } catch (IOException x) { 140 } 141 142 char[] chars = new char[256]; 143 for (int i = 0; i < 256; i++) 144 chars[i] = (char) i; 145 Reader in = new BufferedReader(new Support_StringReader(new String( 146 chars)), 12); 147 try { 148 in.skip(6); 149 in.mark(14); 150 in.read(new char[14], 0, 14); 151 in.reset(); 152 assertTrue("Wrong chars", in.read() == (char) 6 153 && in.read() == (char) 7); 154 } catch (IOException e) { 155 fail("Exception during mark test 2:" + e); 156 } 157 158 in = new BufferedReader(new Support_StringReader(new String(chars)), 12); 159 try { 160 in.skip(6); 161 in.mark(8); 162 in.skip(7); 163 in.reset(); 164 assertTrue("Wrong chars 2", in.read() == (char) 6 165 && in.read() == (char) 7); 166 } catch (IOException e) { 167 fail("Exception during mark test 3:" + e); 168 } 169 } 170 171 /** 172 * @tests java.io.BufferedReader#markSupported() 173 */ 174 @TestTargetNew( 175 level = TestLevel.COMPLETE, 176 method = "markSupported", 177 args = {} 178 ) 179 public void test_markSupported() { 180 // Test for method boolean java.io.BufferedReader.markSupported() 181 br = new BufferedReader(new Support_StringReader(testString)); 182 assertTrue("markSupported returned false.", br.markSupported()); 183 } 184 185 /** 186 * @tests java.io.BufferedReader#read() 187 */ 188 @TestTargetNew( 189 level = TestLevel.COMPLETE, 190 method = "read", 191 args = {} 192 ) 193 public void test_read() throws IOException { 194 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 195 try { 196 br = new BufferedReader(new Support_StringReader(testString)); 197 int r = br.read(); 198 assertTrue("Char read improperly", testString.charAt(0) == r); 199 br = new BufferedReader(new Support_StringReader(new String( 200 new char[] { '\u8765' }))); 201 assertTrue("Wrong double byte character", br.read() == '\u8765'); 202 } catch (java.io.IOException e) { 203 fail("Exception during read test"); 204 } 205 206 char[] chars = new char[256]; 207 for (int i = 0; i < 256; i++) 208 chars[i] = (char) i; 209 Reader in = new BufferedReader(new Support_StringReader(new String( 210 chars)), 12); 211 try { 212 assertEquals("Wrong initial char", 0, in.read()); // Fill the 213 // buffer 214 char[] buf = new char[14]; 215 in.read(buf, 0, 14); // Read greater than the buffer 216 assertTrue("Wrong block read data", new String(buf) 217 .equals(new String(chars, 1, 14))); 218 assertEquals("Wrong chars", 15, in.read()); // Check next byte 219 } catch (IOException e) { 220 fail("Exception during read test 2:" + e); 221 } 222 223 // regression test for HARMONY-841 224 assertTrue(new BufferedReader(new CharArrayReader(new char[5], 1, 0), 2).read() == -1); 225 226 br.close(); 227 br = new BufferedReader(ssr); 228 try { 229 br.read(); 230 fail("IOException expected."); 231 } catch (IOException e) { 232 // Expected. 233 } 234 // Avoid IOException in tearDown(). 235 ssr.throwExceptionOnNextUse = false; 236 } 237 238 /** 239 * @tests java.io.BufferedReader#read(char[], int, int) 240 */ 241 @TestTargetNew( 242 level = TestLevel.PARTIAL_COMPLETE, 243 notes = "The test verifies read(char[] cbuf, int off, int len) method.", 244 method = "read", 245 args = {char[].class, int.class, int.class} 246 ) 247 public void test_read$CII() throws Exception { 248 char[] ca = new char[2]; 249 BufferedReader toRet = new BufferedReader(new InputStreamReader( 250 new ByteArrayInputStream(new byte[0]))); 251 try { 252 toRet.close(); 253 } catch (IOException e) { 254 fail("unexpected 1: " + e); 255 } 256 257 /* Closed reader should throw IOException reading zero bytes */ 258 try { 259 toRet.read(ca, 0, 0); 260 fail("Reading zero bytes on a closed reader should not work"); 261 } catch (IOException e) { 262 // expected 263 } 264 265 // Test to ensure that a drained stream returns 0 at EOF 266 toRet = new BufferedReader(new InputStreamReader( 267 new ByteArrayInputStream(new byte[2]))); 268 try { 269 assertEquals("Emptying the reader should return two bytes", 2, 270 toRet.read(ca, 0, 2)); 271 assertEquals("EOF on a reader should be -1", -1, toRet.read(ca, 0, 272 2)); 273 assertEquals("Reading zero bytes at EOF should work", 0, toRet 274 .read(ca, 0, 0)); 275 } catch (IOException ex) { 276 fail("Unexpected IOException : " + ex.getLocalizedMessage()); 277 } 278 279 // Test for method int java.io.BufferedReader.read(char [], int, int) 280 try { 281 char[] buf = new char[testString.length()]; 282 br = new BufferedReader(new Support_StringReader(testString)); 283 br.read(buf, 50, 500); 284 assertTrue("Chars read improperly", new String(buf, 50, 500) 285 .equals(testString.substring(0, 500))); 286 287 br = new BufferedReader(new Support_StringReader(testString)); 288 assertEquals(0, br.read(buf, 0, 0)); 289 assertEquals(buf.length, br.read(buf, 0, buf.length)); 290 assertEquals(0, br.read(buf, buf.length, 0)); 291 } catch (java.io.IOException e) { 292 fail("Exception during read test"); 293 } 294 295 BufferedReader bufin = new BufferedReader(new Reader() { 296 int size = 2, pos = 0; 297 298 char[] contents = new char[size]; 299 300 public int read() throws IOException { 301 if (pos >= size) 302 throw new IOException("Read past end of data"); 303 return contents[pos++]; 304 } 305 306 public int read(char[] buf, int off, int len) throws IOException { 307 if (pos >= size) 308 throw new IOException("Read past end of data"); 309 int toRead = len; 310 if (toRead > (size - pos)) 311 toRead = size - pos; 312 System.arraycopy(contents, pos, buf, off, toRead); 313 pos += toRead; 314 return toRead; 315 } 316 317 public boolean ready() throws IOException { 318 return size - pos > 0; 319 } 320 321 public void close() throws IOException { 322 } 323 }); 324 try { 325 bufin.read(); 326 int result = bufin.read(new char[2], 0, 2); 327 assertTrue("Incorrect result: " + result, result == 1); 328 } catch (IOException e) { 329 fail("Unexpected: " + e); 330 } 331 } 332 333 /** 334 * @tests java.io.BufferedReader#read(char[], int, int) 335 */ 336 @TestTargetNew( 337 level = TestLevel.PARTIAL_COMPLETE, 338 notes = "The test verifies read(char[] cbuf, int off, int len) method.", 339 method = "read", 340 args = {char[].class, int.class, int.class} 341 ) 342 public void test_read$CII_Exception() throws Exception { 343 br = new BufferedReader(new Support_StringReader(testString)); 344 try{ 345 br.read(new char[10], -1, 1); 346 fail("should throw IndexOutOfBoundsException"); 347 } catch (IndexOutOfBoundsException e) { 348 // Expected 349 } 350 351 try{ 352 br.read(new char[10], 0, -1); 353 fail("should throw IndexOutOfBoundsException"); 354 } catch (IndexOutOfBoundsException e) { 355 // Expected 356 } 357 358 try{ 359 br.read(new char[10], 10, 1); 360 fail("should throw IndexOutOfBoundsException"); 361 } catch (IndexOutOfBoundsException e) { 362 // Expected 363 } 364 365 //regression for HARMONY-831 366 try{ 367 new BufferedReader(new PipedReader(), 9).read(new char[] {}, 7, 0); 368 fail("should throw IndexOutOfBoundsException"); 369 }catch(IndexOutOfBoundsException e){ 370 } 371 } 372 373 /** 374 * @tests java.io.BufferedReader#readLine() 375 */ 376 @TestTargetNew( 377 level = TestLevel.COMPLETE, 378 method = "readLine", 379 args = {} 380 ) 381 public void test_readLine() throws IOException { 382 String line; 383 br = new BufferedReader(new Support_StringReader("Lorem\nipsum\rdolor sit amet...")); 384 385 line = br.readLine(); 386 assertTrue("Test 1: Incorrect line written or read: " + line, 387 line.equals("Lorem")); 388 line = br.readLine(); 389 assertTrue("Test 2: Incorrect line written or read: " + line, 390 line.equals("ipsum")); 391 line = br.readLine(); 392 assertTrue("Test 3: Incorrect line written or read: " + line, 393 line.equals("dolor sit amet...")); 394 395 br.close(); 396 try { 397 br.readLine(); 398 fail("Test 4: IOException expected."); 399 } catch (IOException e) { 400 // Expected. 401 } 402 } 403 404 /** 405 * @tests java.io.BufferedReader#ready() 406 */ 407 @TestTargetNew( 408 level = TestLevel.COMPLETE, 409 method = "ready", 410 args = {} 411 ) 412 public void test_ready() throws IOException { 413 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 414 try { 415 br = new BufferedReader(new Support_StringReader(testString)); 416 assertTrue("Test 1: ready() returned false", br.ready()); 417 } catch (java.io.IOException e) { 418 fail("Exception during ready test" + e.toString()); 419 } 420 421 br.close(); 422 br = new BufferedReader(ssr); 423 try { 424 br.close(); 425 fail("Test 2: IOException expected."); 426 } catch (IOException e) { 427 // Expected. 428 } 429 // Avoid IOException in tearDown(). 430 ssr.throwExceptionOnNextUse = false; 431 } 432 433 /** 434 * @tests java.io.BufferedReader#reset() 435 */ 436 @TestTargetNew( 437 level = TestLevel.COMPLETE, 438 notes = "The test verifies reset() method.", 439 method = "reset", 440 args = {} 441 ) 442 public void test_reset() { 443 // Test for method void java.io.BufferedReader.reset() 444 try { 445 br = new BufferedReader(new Support_StringReader(testString)); 446 br.skip(500); 447 br.mark(900); 448 br.skip(500); 449 br.reset(); 450 char[] buf = new char[testString.length()]; 451 br.read(buf, 0, 500); 452 assertTrue("Failed to reset properly", testString.substring(500, 453 1000).equals(new String(buf, 0, 500))); 454 } catch (java.io.IOException e) { 455 fail("Exception during reset test"); 456 } 457 try { 458 br = new BufferedReader(new Support_StringReader(testString)); 459 br.skip(500); 460 br.reset(); 461 fail("Reset succeeded on unmarked stream"); 462 } catch (IOException x) { 463 return; 464 465 } 466 467 } 468 469 /** 470 * @tests java.io.BufferedReader#skip(long) 471 */ 472 @TestTargetNew( 473 level = TestLevel.COMPLETE, 474 method = "skip", 475 args = {long.class} 476 ) 477 public void test_skipJ() throws IOException { 478 Support_ASimpleReader ssr = new Support_ASimpleReader(true); 479 br = new BufferedReader(new Support_StringReader(testString)); 480 481 try { 482 br.skip(-1); 483 fail("Test 1: IllegalArgumentException expected."); 484 } catch (IllegalArgumentException e) { 485 // Expected. 486 } 487 488 br.skip(500); 489 char[] buf = new char[testString.length()]; 490 br.read(buf, 0, 500); 491 assertTrue("Test 2: Failed to set skip properly.", 492 testString.substring(500, 1000).equals( 493 new String(buf, 0, 500))); 494 495 br.close(); 496 br = new BufferedReader(ssr); 497 try { 498 br.skip(1); 499 fail("Test 3: IOException expected."); 500 } catch (IOException e) { 501 // Expected. 502 } 503 // Avoid IOException in tearDown(). 504 ssr.throwExceptionOnNextUse = false; 505 } 506 507 public void testReadZeroLengthArray() throws IOException { 508 br = new BufferedReader(new Support_StringReader("ABCDEF")); 509 br.read(); 510 br.read(); 511 assertEquals(0, br.read(new char[6], 3, 0)); 512 } 513 514 public void testSourceThrowsWithMark() throws IOException { 515 br = new BufferedReader(new ThrowingReader( 516 new StringReader("ABCDEFGHI"), 4)); 517 518 br.read(); 519 br.read(); 520 br.mark(10); 521 br.read(); 522 br.read(); 523 524 try { 525 br.read(); 526 fail(); 527 } catch (IOException fromThrowingReader) { 528 } 529 530 assertEquals('E', br.read()); 531 assertEquals('F', br.read()); 532 } 533 534 /** 535 * Tears down the fixture, for example, close a network connection. This 536 * method is called after a test is executed. 537 */ 538 protected void tearDown() { 539 try { 540 br.close(); 541 } catch (Exception e) { 542 } 543 } 544} 545