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.ByteArrayInputStream; 21import java.io.IOException; 22import java.io.PushbackInputStream; 23 24import tests.support.Support_ASimpleInputStream; 25import dalvik.annotation.TestLevel; 26import dalvik.annotation.TestTargetClass; 27import dalvik.annotation.TestTargetNew; 28 29@TestTargetClass(PushbackInputStream.class) 30public class PushbackInputStreamTest extends junit.framework.TestCase { 31 32 Support_ASimpleInputStream underlying = new Support_ASimpleInputStream(); 33 PushbackInputStream pis; 34 35 public String fileString = "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"; 36 37 /** 38 * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream) 39 */ 40 @TestTargetNew( 41 level = TestLevel.COMPLETE, 42 notes = "", 43 method = "PushbackInputStream", 44 args = {java.io.InputStream.class} 45 ) 46 public void test_ConstructorLjava_io_InputStream() { 47 // Test for method java.io.PushbackInputStream(java.io.InputStream) 48 try { 49 pis = new PushbackInputStream(new ByteArrayInputStream("Hello" 50 .getBytes())); 51 pis.unread("He".getBytes()); 52 } catch (IOException e) { 53 // Correct 54 // Pushback buffer should be full 55 return; 56 57 } 58 fail("Failed to throw exception on unread when buffer full"); 59 } 60 61 /** 62 * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream, 63 * int) 64 */ 65 @TestTargetNew( 66 level = TestLevel.COMPLETE, 67 method = "PushbackInputStream", 68 args = {java.io.InputStream.class, int.class} 69 ) 70 public void test_ConstructorLjava_io_InputStreamI() { 71 ByteArrayInputStream bas = new ByteArrayInputStream("Hello".getBytes()); 72 try { 73 pis = new PushbackInputStream(bas, 0); 74 fail("Test 1: IllegalArgumentException expected."); 75 } catch (IllegalArgumentException e) { 76 // Expected. 77 } 78 try { 79 pis = new PushbackInputStream(bas, -1); 80 fail("Test 2: IllegalArgumentException expected."); 81 } catch (IllegalArgumentException e) { 82 // Expected. 83 } 84 85 pis = new PushbackInputStream(bas , 5); 86 try { 87 pis.unread("Hello world".getBytes()); 88 fail("Test 3: IOException expected when the unread buffer is full."); 89 } catch (IOException e) { 90 // Expected. 91 } 92 } 93 94 @TestTargetNew( 95 level = TestLevel.COMPLETE, 96 notes = "", 97 method = "close", 98 args = {} 99 ) 100 public void test_close() throws IOException { 101 PushbackInputStream tobj; 102 103 tobj = new PushbackInputStream(underlying); 104 tobj.close(); 105 tobj.close(); 106 tobj = new PushbackInputStream(underlying); 107 underlying.throwExceptionOnNextUse = true; 108 try { 109 tobj.close(); 110 fail("IOException not thrown."); 111 } catch (IOException e) { 112 // expected 113 } 114 } 115 116 /** 117 * @throws IOException 118 * @tests java.io.PushbackInputStream#available() 119 */ 120 @TestTargetNew( 121 level = TestLevel.SUFFICIENT, 122 notes = "", 123 method = "available", 124 args = {} 125 ) 126 public void test_available() throws IOException { 127 PushbackInputStream tobj; 128 129 tobj = new PushbackInputStream(underlying); 130 assertEquals("Wrong number!", 30, tobj.available()); 131 underlying.throwExceptionOnNextUse = true; 132 try { 133 tobj.available(); 134 fail("IOException not thrown."); 135 } catch (IOException e) { 136 // expected 137 } 138 } 139 140 /** 141 * @tests java.io.PushbackInputStream#markSupported() 142 */ 143 @TestTargetNew( 144 level = TestLevel.COMPLETE, 145 notes = "", 146 method = "markSupported", 147 args = {} 148 ) 149 public void test_markSupported() { 150 // Test for method boolean java.io.PushbackInputStream.markSupported() 151 assertTrue("markSupported returned true", !pis.markSupported()); 152 } 153 154 /** 155 * @tests java.io.PushbackInputStream#read() 156 */ 157 @TestTargetNew( 158 level = TestLevel.PARTIAL_COMPLETE, 159 notes = "", 160 method = "read", 161 args = {} 162 ) 163 public void test_read() throws IOException { 164 PushbackInputStream tobj; 165 166 tobj = new PushbackInputStream(underlying); 167 assertEquals("Test 1: Incorrect byte read;", 66, tobj.read()); 168 underlying.throwExceptionOnNextUse = true; 169 try { 170 tobj.read(); 171 fail("Test 2: IOException expected."); 172 } catch (IOException e) { 173 // expected 174 } 175 176 assertEquals("Test 3: Incorrect byte read;", 177 fileString.getBytes()[0], pis.read()); 178 } 179 180 /** 181 * @tests java.io.PushbackInputStream#read(byte[], int, int) 182 */ 183 @TestTargetNew( 184 level = TestLevel.PARTIAL_COMPLETE, 185 notes = "", 186 method = "read", 187 args = {byte[].class, int.class, int.class} 188 ) 189 public void test_read$BII() throws IOException { 190 PushbackInputStream tobj; 191 byte[] buf = ("01234567890123456789").getBytes(); 192 193 tobj = new PushbackInputStream(underlying); 194 tobj.read(buf, 6, 5); 195 assertEquals("Wrong value read!", "BEGIN", new String(buf, 6, 5)); 196 assertEquals("Too much read!", "012345BEGIN123456789", new String(buf)); 197 underlying.throwExceptionOnNextUse = true; 198 try { 199 tobj.read(buf, 6, 5); 200 fail("IOException not thrown."); 201 } catch (IOException e) { 202 // expected 203 } 204 } 205 206 /** 207 * @tests java.io.PushbackInputStream#read(byte[], int, int) 208 */ 209 @TestTargetNew( 210 level = TestLevel.PARTIAL_COMPLETE, 211 notes = "", 212 method = "read", 213 args = {byte[].class, int.class, int.class} 214 ) 215 public void test_read$BII_Exception() throws IOException { 216 PushbackInputStream tobj; 217 byte[] buf = new byte[10]; 218 219 tobj = new PushbackInputStream(underlying); 220 try { 221 tobj.read(buf, -1, 1); 222 fail("IndexOutOfBoundsException was not thrown"); 223 } catch (IndexOutOfBoundsException e) { 224 // Expected 225 } 226 try { 227 tobj.read(buf, 0, -1); 228 fail("IndexOutOfBoundsException was not thrown"); 229 } catch (IndexOutOfBoundsException e) { 230 // Expected 231 } 232 try { 233 tobj.read(buf, 10, 1); 234 fail("IndexOutOfBoundsException was not thrown"); 235 } catch (IndexOutOfBoundsException e) { 236 // Expected 237 } 238 } 239 240 /** 241 * @tests java.io.PushbackInputStream#skip(long) 242 */ 243 @TestTargetNew( 244 level = TestLevel.COMPLETE, 245 notes = "", 246 method = "skip", 247 args = {long.class} 248 ) 249 public void test_skipJ() throws IOException { 250 PushbackInputStream tobj; 251 byte[] buf = ("01234567890123456789").getBytes(); 252 253 tobj = new PushbackInputStream(underlying); 254 tobj.skip(6); 255 assertEquals("Wrong number!", 30 - 6, tobj.available()); 256 tobj.skip(1000000); 257 tobj.skip(1000000); 258 underlying.throwExceptionOnNextUse = true; 259 try { 260 tobj.skip(1); 261 fail("IOException not thrown."); 262 } catch (IOException e) { 263 // expected 264 } 265 266 // Test for method long java.io.PushbackInputStream.skip(long) 267 try { 268 buf = new byte[50]; 269 pis.skip(50); 270 pis.read(buf, 0, buf.length); 271 assertTrue("a) Incorrect bytes read", new String(buf) 272 .equals(fileString.substring(50, 100))); 273 pis.unread(buf); 274 pis.skip(25); 275 byte[] buf2 = new byte[25]; 276 pis.read(buf2, 0, buf2.length); 277 assertTrue("b) Incorrect bytes read", new String(buf2) 278 .equals(fileString.substring(75, 100))); 279 } catch (Exception e) { 280 fail("Exception during test : " + e.getMessage()); 281 } 282 } 283 284 /** 285 * @tests java.io.PushbackInputStream#unread(byte[]) 286 */ 287 @TestTargetNew( 288 level = TestLevel.COMPLETE, 289 notes = "", 290 method = "unread", 291 args = {byte[].class} 292 ) 293 public void test_unread$B() throws IOException { 294 PushbackInputStream tobj; 295 String str2 = "0123456789"; 296 byte[] buf2 = str2.getBytes(); 297 byte[] readBuf = new byte[10]; 298 299 tobj = new PushbackInputStream(underlying, 10); 300 tobj.unread(buf2); 301 assertEquals("Wrong number!", 30 + 10, tobj.available()); 302 try { 303 tobj.unread(buf2); 304 fail("IOException not thrown."); 305 } catch (IOException e) { 306 // expected 307 } 308 tobj.read(readBuf); 309 assertEquals("Incorrect bytes read", str2, new String(readBuf)); 310 underlying.throwExceptionOnNextUse = true; 311 try { 312 tobj.read(buf2); 313 fail("IOException not thrown."); 314 } catch (IOException e) { 315 // expected 316 } 317 318 // Test for method void java.io.PushbackInputStream.unread(byte []) 319 try { 320 byte[] buf = new byte[100]; 321 pis.read(buf, 0, buf.length); 322 assertTrue("Incorrect bytes read", new String(buf) 323 .equals(fileString.substring(0, 100))); 324 pis.unread(buf); 325 pis.read(buf, 0, 50); 326 assertTrue("Failed to unread bytes", new String(buf, 0, 50) 327 .equals(fileString.substring(0, 50))); 328 } catch (IOException e) { 329 fail("IOException during unread test : " + e.getMessage()); 330 } 331 } 332 333 /** 334 * @tests java.io.PushbackInputStream#unread(byte[], int, int) 335 */ 336 @TestTargetNew( 337 level = TestLevel.COMPLETE, 338 notes = "", 339 method = "unread", 340 args = {byte[].class, int.class, int.class} 341 ) 342 public void test_unread$BII() throws IOException { 343 PushbackInputStream tobj; 344 String str2 = "0123456789"; 345 byte[] buf2 = (str2 + str2 + str2).getBytes(); 346 byte[] readBuf = new byte[10]; 347 348 tobj = new PushbackInputStream(underlying, 10); 349 tobj.unread(buf2, 15, 10); 350 assertEquals("Wrong number!", 30 + 10, tobj.available()); 351 try { 352 tobj.unread(buf2, 15, 10); 353 fail("IOException not thrown."); 354 } catch (IOException e) { 355 // expected 356 } 357 tobj.read(readBuf); 358 assertEquals("Incorrect bytes read", "5678901234", new String(readBuf)); 359 underlying.throwExceptionOnNextUse = true; 360 try { 361 tobj.read(buf2, 15, 10); 362 fail("IOException not thrown."); 363 } catch (IOException e) { 364 // expected 365 } 366 367 // Test for method void java.io.PushbackInputStream.unread(byte [], int, 368 // int) 369 try { 370 byte[] buf = new byte[100]; 371 pis.read(buf, 0, buf.length); 372 assertTrue("Incorrect bytes read", new String(buf) 373 .equals(fileString.substring(0, 100))); 374 pis.unread(buf, 50, 50); 375 pis.read(buf, 0, 50); 376 assertTrue("Failed to unread bytes", new String(buf, 0, 50) 377 .equals(fileString.substring(50, 100))); 378 } catch (IOException e) { 379 fail("IOException during unread test : " + e.getMessage()); 380 } 381 382 try { 383 byte[] buf = new byte[10]; 384 pis.unread(buf, 0, -1); 385 fail("IndexOutOfBoundsException was not thrown"); 386 } catch (IndexOutOfBoundsException e) { 387 // Expected 388 } 389 390 try { 391 byte[] buf = new byte[10]; 392 pis.unread(buf, -1, 1); 393 fail("IndexOutOfBoundsException was not thrown"); 394 } catch (IndexOutOfBoundsException e) { 395 // Expected 396 } 397 398 try { 399 byte[] buf = new byte[10]; 400 pis.unread(buf, 10, 1); 401 fail("IndexOutOfBoundsException was not thrown"); 402 } catch (IndexOutOfBoundsException e) { 403 // Expected 404 } 405 } 406 407 /** 408 * @tests java.io.PushbackInputStream#unread(int) 409 */ 410 @TestTargetNew( 411 level = TestLevel.COMPLETE, 412 notes = "", 413 method = "unread", 414 args = {int.class} 415 ) 416 public void test_unreadI() throws IOException { 417 PushbackInputStream tobj; 418 419 tobj = new PushbackInputStream(underlying); 420 tobj.unread(23); // Why does this work?!? 421 tobj.skip(2); 422 tobj.unread(23); 423 assertEquals("Wrong number!", 30, tobj.available()); 424 assertEquals("Wrong value read!", 23, tobj.read()); 425 tobj.unread(13); 426 try { 427 tobj.unread(13); 428 fail("IOException not thrown (ACTUALLY NOT SURE WHETHER IT REALLY MUST BE THROWN!)."); 429 } catch (IOException e) { 430 // expected 431 } 432 433 // Test for method void java.io.PushbackInputStream.unread(int) 434 try { 435 int x; 436 assertTrue("Incorrect byte read", (x = pis.read()) == fileString 437 .getBytes()[0]); 438 pis.unread(x); 439 assertTrue("Failed to unread", pis.read() == x); 440 } catch (IOException e) { 441 fail("IOException during read test : " + e.getMessage()); 442 } 443 } 444 445 /** 446 * Sets up the fixture, for example, open a network connection. This method 447 * is called before a test is executed. 448 */ 449 protected void setUp() { 450 451 pis = new PushbackInputStream(new ByteArrayInputStream(fileString 452 .getBytes()), 65535); 453 } 454 455 /** 456 * Tears down the fixture, for example, close a network connection. This 457 * method is called after a test is executed. 458 */ 459 protected void tearDown() { 460 try { 461 pis.close(); 462 } catch (IOException e) { 463 fail("IOException during tearDown : " + e.getMessage()); 464 } 465 } 466} 467