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