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