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