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