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.CharArrayReader; 21import java.io.FilterReader; 22import java.io.IOException; 23import java.io.PushbackReader; 24import java.io.Reader; 25import java.io.StringReader; 26 27import tests.support.Support_ASimpleReader; 28import dalvik.annotation.TestLevel; 29import dalvik.annotation.TestTargetClass; 30import dalvik.annotation.TestTargetNew; 31 32@TestTargetClass(PushbackReader.class) 33public class PushbackReaderTest extends junit.framework.TestCase { 34 35 Support_ASimpleReader underlying = new Support_ASimpleReader(); 36 PushbackReader pbr; 37 38 String pbString = "Hello World"; 39 40 /** 41 * @tests java.io.PushbackReader#PushbackReader(java.io.Reader) 42 */ 43 @TestTargetNew( 44 level = TestLevel.COMPLETE, 45 method = "PushbackReader", 46 args = {java.io.Reader.class} 47 ) 48 public void test_ConstructorLjava_io_Reader() { 49 // Test for method java.io.PushbackReader(java.io.Reader) 50 try { 51 pbr.close(); 52 pbr = new PushbackReader(new StringReader(pbString)); 53 char buf[] = new char[5]; 54 pbr.read(buf, 0, 5); 55 pbr.unread(buf); 56 fail("Created reader with buffer larger than 1");; 57 } catch (IOException e) { 58 // Expected 59 } 60 61 try { 62 pbr = new PushbackReader(null); 63 } catch (NullPointerException e) { 64 // EXpected 65 } 66 } 67 68 /** 69 * @throws IOException 70 * @tests java.io.PushbackReader#PushbackReader(java.io.Reader, int) 71 */ 72 @TestTargetNew( 73 level = TestLevel.COMPLETE, 74 method = "PushbackReader", 75 args = {java.io.Reader.class, int.class} 76 ) 77 public void test_ConstructorLjava_io_ReaderI() throws IOException { 78 PushbackReader tobj; 79 80 tobj = new PushbackReader(underlying, 10000); 81 tobj = new PushbackReader(underlying, 1); 82 83 try { 84 tobj = new PushbackReader(underlying, -1); 85 tobj.close(); 86 fail("IOException not thrown."); 87 } catch (IllegalArgumentException e) { 88 // expected 89 } 90 try { 91 tobj = new PushbackReader(underlying, 0); 92 tobj.close(); 93 fail("IOException not thrown."); 94 } catch (IllegalArgumentException e) { 95 // expected 96 } 97 } 98 99 /** 100 * @throws IOException 101 * @tests java.io.PushbackReader#close() 102 */ 103 @TestTargetNew( 104 level = TestLevel.COMPLETE, 105 method = "close", 106 args = {} 107 ) 108 public void test_close() throws IOException { 109 PushbackReader tobj; 110 111 tobj = new PushbackReader(underlying); 112 tobj.close(); 113 tobj.close(); 114 tobj = new PushbackReader(underlying); 115 underlying.throwExceptionOnNextUse = true; 116 try { 117 tobj.close(); 118 fail("IOException not thrown."); 119 } catch (IOException e) { 120 // expected 121 } 122 123 // Test for method void java.io.PushbackReader.close() 124 try { 125 pbr.close(); 126 pbr.read(); 127 } catch (Exception e) { 128 return; 129 } 130 fail("Failed to throw exception reading from closed reader"); 131 132 } 133 134 /** 135 * @tests java.io.PushbackReader#mark(int) 136 */ 137 @TestTargetNew( 138 level = TestLevel.COMPLETE, 139 method = "mark", 140 args = {int.class} 141 ) 142 public void test_markI() { 143 try { 144 pbr.mark(3); 145 fail("Test 1: IOException expected because marking is not supported."); 146 } catch (IOException e) { 147 // Expected. 148 } 149 } 150 151 /** 152 * @tests java.io.PushbackReader#markSupported() 153 */ 154 @TestTargetNew( 155 level = TestLevel.COMPLETE, 156 method = "markSupported", 157 args = {} 158 ) 159 public void test_markSupported() { 160 assertFalse("Test 1: markSupported() must return false.", 161 pbr.markSupported()); 162 } 163 164 /** 165 * @throws IOException 166 * @tests java.io.PushbackReader#read() 167 */ 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 method = "read", 171 args = {} 172 ) 173 public void test_read() throws IOException { 174 PushbackReader tobj; 175 176 tobj = new PushbackReader(underlying); 177 assertEquals("Wrong value read!", 66, tobj.read()); 178 underlying.throwExceptionOnNextUse = true; 179 try { 180 tobj.read(); 181 fail("IOException not thrown."); 182 } catch (IOException e) { 183 // expected 184 } 185 186 // Test for method int java.io.PushbackReader.read() 187 try { 188 char c; 189 pbr.read(); 190 c = (char) pbr.read(); 191 assertTrue("Failed to read char: " + c, c == pbString.charAt(1)); 192 Reader reader = new PushbackReader(new CharArrayReader( 193 new char[] { '\u8765' })); 194 assertTrue("Wrong double byte character", reader.read() == '\u8765'); 195 } catch (IOException e) { 196 fail("IOException during read test : " + e.getMessage()); 197 } 198 } 199 200 /** 201 * @throws IOException 202 * @tests java.io.PushbackReader#read(char[], int, int) 203 */ 204 @TestTargetNew( 205 level = TestLevel.PARTIAL_COMPLETE, 206 method = "read", 207 args = {char[].class, int.class, int.class} 208 ) 209 public void test_read$CII() throws IOException { 210 PushbackReader tobj; 211 char[] buf = ("01234567890123456789").toCharArray(); 212 213 tobj = new PushbackReader(underlying); 214 tobj.read(buf, 6, 5); 215 assertEquals("Wrong value read!", "BEGIN", new String(buf, 6, 5)); 216 assertEquals("Too much read!", "012345BEGIN123456789", new String(buf)); 217 underlying.throwExceptionOnNextUse = true; 218 try { 219 tobj.read(buf, 6, 5); 220 fail("IOException not thrown."); 221 } catch (IOException e) { 222 // expected 223 } 224 225 // Test for method int java.io.PushbackReader.read(char [], int, int) 226 try { 227 char[] c = new char[5]; 228 pbr.read(c, 0, 5); 229 assertTrue("Failed to read chars", new String(c).equals(pbString 230 .substring(0, 5))); 231 232 assertEquals(0, pbr.read(c, 0, 0)); 233 assertEquals(c.length, pbr.read(c, 0, c.length)); 234 assertEquals(0, pbr.read(c, c.length, 0)); 235 } catch (IOException e) { 236 fail("IOException during read test : " + e.getMessage()); 237 } 238 } 239 240 /** 241 * @tests java.io.PushbackReader#read(char[], int, int) 242 */ 243 @TestTargetNew( 244 level = TestLevel.PARTIAL_COMPLETE, 245 notes = "Checks exceptions.", 246 method = "read", 247 args = {char[].class, int.class, int.class} 248 ) 249 public void test_read_$CII_Exception() throws IOException { 250 pbr = new PushbackReader(new StringReader(pbString), 10); 251 252 char[] nullCharArray = null; 253 char[] charArray = new char[10]; 254 255 try { 256 pbr.read(nullCharArray, 0, 1); 257 fail("should throw NullPointerException"); 258 } catch (NullPointerException e) { 259 // expected 260 } 261 262 try { 263 pbr.read(charArray, 0, -1); 264 fail("should throw IndexOutOfBoundsException"); 265 } catch (IndexOutOfBoundsException e) { 266 // expected 267 } 268 269 try { 270 pbr.read(charArray, -1, 0); 271 fail("should throw IndexOutOfBoundsException"); 272 } catch (IndexOutOfBoundsException e) { 273 // expected 274 } 275 276 try { 277 pbr.read(charArray, charArray.length + 1, 0); 278 fail("should throw IndexOutOfBoundsException"); 279 } catch (IndexOutOfBoundsException e) { 280 // expected 281 } 282 283 try { 284 pbr.read(charArray, charArray.length, 1); 285 fail("should throw IndexOutOfBoundsException"); 286 } catch (IndexOutOfBoundsException e) { 287 // expected 288 } 289 290 try { 291 pbr.read(charArray, 1, charArray.length); 292 fail("should throw IndexOutOfBoundsException"); 293 } catch (IndexOutOfBoundsException e) { 294 // expected 295 } 296 297 try { 298 pbr.read(charArray, 0, charArray.length + 1); 299 fail("should throw IndexOutOfBoundsException"); 300 } catch (IndexOutOfBoundsException e) { 301 // expected 302 } 303 304 pbr.close(); 305 306 try { 307 pbr.read(charArray, 0, 1); 308 fail("should throw IOException"); 309 } catch (IOException e) { 310 // expected 311 } 312 } 313 314 /** 315 * @throws IOException 316 * @tests java.io.PushbackReader#ready() 317 */ 318 @TestTargetNew( 319 level = TestLevel.SUFFICIENT, 320 notes = "Could also add tests where underlying says no but push back buffer has contents.", 321 method = "ready", 322 args = {} 323 ) 324 public void test_ready() throws IOException { 325 PushbackReader tobj; 326 327 tobj = new PushbackReader(underlying); 328 assertTrue("Should be ready!", tobj.ready()); 329 underlying.throwExceptionOnNextUse = true; 330 try { 331 tobj.ready(); 332 fail("IOException not thrown."); 333 } catch (IOException e) { 334 // expected 335 } 336 337 // Test for method boolean java.io.PushbackReader.ready() 338 try { 339 char[] c = new char[11]; 340 if (c.length > 0) 341 ;// use c to avoid warning msg 342 assertTrue("Ready stream returned false to ready()", pbr.ready()); 343 } catch (IOException e) { 344 fail("IOException during ready() test : " + e.getMessage()); 345 } 346 } 347 348 /** 349 * @tests java.io.PushbackReader#reset() 350 */ 351 @TestTargetNew( 352 level = TestLevel.COMPLETE, 353 method = "reset", 354 args = {} 355 ) 356 public void test_reset() { 357 try { 358 pbr.reset(); 359 } catch (IOException e) { 360 // correct 361 return; 362 } 363 fail("reset failed to throw expected IOException"); 364 } 365 366 /** 367 * @throws IOException 368 * @tests java.io.PushbackReader#unread(char[]) 369 */ 370 @TestTargetNew( 371 level = TestLevel.COMPLETE, 372 method = "unread", 373 args = {char[].class} 374 ) 375 public void test_unread$C() throws IOException { 376 PushbackReader tobj; 377 String str2 = "0123456789"; 378 char[] buf2 = str2.toCharArray(); 379 char[] readBuf = new char[10]; 380 381 tobj = new PushbackReader(underlying, 10); 382 tobj.unread(buf2); 383 try { 384 tobj.unread(buf2); 385 fail("IOException not thrown."); 386 } catch (IOException e) { 387 // expected 388 } 389 tobj.read(readBuf); 390 assertEquals("Incorrect bytes read", str2, new String(readBuf)); 391 underlying.throwExceptionOnNextUse = true; 392 try { 393 tobj.read(buf2); 394 fail("IOException not thrown."); 395 } catch (IOException e) { 396 // expected 397 } 398 399 // Test for method void java.io.PushbackReader.unread(char []) 400 try { 401 char[] c = new char[5]; 402 pbr.read(c, 0, 5); 403 pbr.unread(c); 404 pbr.read(c, 0, 5); 405 assertTrue("Failed to unread chars", new String(c).equals(pbString 406 .substring(0, 5))); 407 } catch (IOException e) { 408 fail("IOException during read test : " + e.getMessage()); 409 } 410 } 411 412 /** 413 * @throws IOException 414 * @tests java.io.PushbackReader#skip(long) 415 */ 416 @TestTargetNew( 417 level = TestLevel.COMPLETE, 418 method = "skip", 419 args = {long.class} 420 ) 421 public void test_skip$J() throws IOException { 422 PushbackReader tobj; 423 424 tobj = new PushbackReader(underlying); 425 tobj.skip(6); 426 tobj.skip(1000000); 427 tobj.skip(1000000); 428 underlying.throwExceptionOnNextUse = true; 429 try { 430 tobj.skip(1); 431 fail("IOException not thrown."); 432 } catch (IOException e) { 433 // expected 434 } 435 436 char chars[] = new char[] { 'h', 'e', 'l', 'l', 'o' }; 437 for (int i = 0; i < 3; i++) { 438 Reader reader, reader2; 439 switch (i) { 440 case 0: 441 reader = new StringReader(new String(chars)); 442 reader2 = new StringReader(new String(chars)); 443 break; 444 case 1: 445 reader = new FilterReader(new StringReader(new String(chars))) { 446 }; 447 reader2 = new FilterReader(new StringReader(new String(chars))) { 448 }; 449 break; 450 default: 451 reader = new CharArrayReader(chars); 452 reader2 = new CharArrayReader(chars); 453 } 454 PushbackReader pReader = new PushbackReader(reader, 2); 455 PushbackReader pReader2 = new PushbackReader(reader2, 2); 456 boolean skipped = false; 457 long numSkipped = 0; 458 try { 459 numSkipped = pReader2.skip(3); 460 pReader2.unread('a'); 461 pReader2.unread('b'); 462 numSkipped += pReader2.skip(10); 463 numSkipped += pReader2.skip(10); 464 numSkipped += pReader2.skip(10); 465 numSkipped += pReader2.skip(10); 466 numSkipped += pReader2.skip(10); 467 numSkipped += pReader2.skip(10); 468 assertEquals("Did not skip correct number of characters", 469 7, numSkipped); 470 numSkipped = 0; 471 numSkipped += pReader.skip(2); 472 pReader.unread('i'); 473 numSkipped += pReader.skip(2); 474 numSkipped += pReader.skip(0); 475 skipped = true; 476 numSkipped += pReader.skip(-1); 477 fail("Failed to throw " 478 + new IllegalArgumentException().getClass().getName()); 479 } catch (IllegalArgumentException e) { 480 assertTrue("Failed to skip characters" + e, skipped); 481 } catch (IOException e) { 482 fail("Failed to skip characters" + e); 483 } 484 try { 485 numSkipped += pReader.skip(1); 486 numSkipped += pReader.skip(1); 487 numSkipped += pReader.skip(1); 488 assertEquals("Failed to skip all characters", 6, numSkipped); 489 long nextSkipped = pReader.skip(1); 490 assertEquals("skipped empty reader", 0, nextSkipped); 491 } catch (IOException e) { 492 fail("Failed to skip more characters" + e); 493 } 494 } 495 } 496 497 /** 498 * @throws IOException 499 * @tests java.io.PushbackReader#unread(char[], int, int) 500 */ 501 @TestTargetNew( 502 level = TestLevel.PARTIAL_COMPLETE, 503 method = "unread", 504 args = {char[].class, int.class, int.class} 505 ) 506 public void test_unread$CII() throws IOException { 507 PushbackReader tobj; 508 String str2 = "0123456789"; 509 char[] buf2 = (str2 + str2 + str2).toCharArray(); 510 char[] readBuf = new char[10]; 511 512 tobj = new PushbackReader(underlying, 10); 513 tobj.unread(buf2, 15, 10); 514 try { 515 tobj.unread(buf2, 15, 10); 516 fail("IOException not thrown."); 517 } catch (IOException e) { 518 // expected 519 } 520 tobj.read(readBuf); 521 assertEquals("Incorrect bytes read", "5678901234", new String(readBuf)); 522 underlying.throwExceptionOnNextUse = true; 523 try { 524 tobj.read(buf2, 15, 10); 525 fail("IOException not thrown."); 526 } catch (IOException e) { 527 // expected 528 } 529 530 // Test for method void java.io.PushbackReader.unread(char [], int, int) 531 try { 532 char[] c = new char[5]; 533 pbr.read(c, 0, 5); 534 pbr.unread(c, 0, 2); 535 pbr.read(c, 0, 5); 536 assertTrue("Failed to unread chars", new String(c).equals(pbString 537 .substring(0, 2) 538 + pbString.substring(5, 8))); 539 } catch (IOException e) { 540 fail("IOException during unread test : " + e.getMessage()); 541 } 542 } 543 544 /** 545 * @tests java.io.PushbackReader#unread(char[], int, int) 546 */ 547 @TestTargetNew( 548 level = TestLevel.PARTIAL_COMPLETE, 549 method = "unread", 550 args = {char[].class, int.class, int.class} 551 ) 552 public void test_unread_$CII_NullPointerException() throws IOException { 553 //a pushback reader with one character buffer 554 pbr = new PushbackReader(new StringReader(pbString)); 555 556 try { 557 pbr.unread(null, 0, 1); 558 fail("should throw NullPointerException"); 559 } catch (NullPointerException e) { 560 // expected 561 } 562 } 563 564 /** 565 * @tests java.io.PushbackReader#unread(char[], int, int) 566 */ 567 @TestTargetNew( 568 level = TestLevel.PARTIAL_COMPLETE, 569 method = "unread", 570 args = {char[].class, int.class, int.class} 571 ) 572 public void test_unread_$CII_Exception_InsufficientBuffer() throws IOException { 573 //a pushback reader with one character buffer 574 pbr = new PushbackReader(new StringReader(pbString)); 575 576 //if count > buffer's size , should throw IOException 577 try { 578 pbr.unread(new char[pbString.length()], 0, 2); 579 fail("should throw IOException"); 580 } catch (IOException e) { 581 // expected 582 } 583 } 584 585 /** 586 * @tests java.io.PushbackReader#unread(char[], int, int) 587 */ 588 @TestTargetNew( 589 level = TestLevel.PARTIAL_COMPLETE, 590 method = "unread", 591 args = {char[].class, int.class, int.class} 592 ) 593 public void test_unread_$CII_ArrayIndexOutOfBoundsException() throws IOException { 594 //a pushback reader with one character buffer 595 pbr = new PushbackReader(new StringReader(pbString)); 596 597 try { 598 pbr.unread(new char[pbString.length()], -1 , 1); 599 fail("should throw ArrayIndexOutOfBoundsException"); 600 } catch (IndexOutOfBoundsException e) { 601 // expected 602 } 603 try { 604 pbr.unread(new char[pbString.length()], 0 , -1); 605 fail("should throw ArrayIndexOutOfBoundsException"); 606 } catch (IndexOutOfBoundsException e) { 607 // expected 608 } 609 try { 610 pbr.unread(new char[10], 10 , 1); 611 fail("should throw ArrayIndexOutOfBoundsException"); 612 } catch (IndexOutOfBoundsException e) { 613 // expected 614 } 615 } 616 617 /** 618 * @throws IOException 619 * @tests java.io.PushbackReader#unread(int) 620 */ 621 @TestTargetNew( 622 level = TestLevel.COMPLETE, 623 notes = "", 624 method = "unread", 625 args = {int.class} 626 ) 627 public void test_unreadI() throws IOException { 628 PushbackReader tobj; 629 630 tobj = new PushbackReader(underlying); 631 tobj.unread(23); // Why does this work?!? 632 tobj.skip(2); 633 tobj.unread(23); 634 assertEquals("Wrong value read!", 23, tobj.read()); 635 tobj.unread(13); 636 try { 637 tobj.unread(13); 638 fail("IOException not thrown (ACTUALLY NOT SURE WHETHER IT REALLY MUST BE THROWN!)."); 639 } catch (IOException e) { 640 // expected 641 } 642 643 // Test for method void java.io.PushbackReader.unread(int) 644 try { 645 int c; 646 pbr.read(); 647 c = pbr.read(); 648 pbr.unread(c); 649 assertTrue("Failed to unread char", pbr.read() == c); 650 } catch (IOException e) { 651 fail("IOException during unread test : " + e.getMessage()); 652 } 653 } 654 655 /** 656 * Sets up the fixture, for example, open a network connection. This method 657 * is called before a test is executed. 658 */ 659 protected void setUp() { 660 pbr = new PushbackReader(new StringReader(pbString), 10); 661 } 662 663 /** 664 * Tears down the fixture, for example, close a network connection. This 665 * method is called after a test is executed. 666 */ 667 protected void tearDown() { 668 try { 669 pbr.close(); 670 } catch (IOException e) { 671 } 672 } 673} 674