FileOutputStreamTest.java revision 3087daf498cdd64fb87f4f54b3e65661d0a6bf59
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, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.io; 19 20import java.io.File; 21import java.io.FileDescriptor; 22import java.io.FileInputStream; 23import java.io.FileNotFoundException; 24import java.io.FileOutputStream; 25import java.io.IOException; 26 27import junit.framework.TestCase; 28 29public class FileOutputStreamTest extends TestCase { 30 31 public String fileName; 32 33 FileOutputStream fos; 34 35 FileInputStream fis; 36 37 File f; 38 39 byte[] ibuf = new byte[4096]; 40 41 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_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"; 42 43 byte[] bytes; 44 45 /** 46 * @tests java.io.FileOutputStream#FileOutputStream(java.io.File) 47 */ 48 public void test_ConstructorLjava_io_File() throws IOException { 49 f = new File(fileName = System.getProperty("user.home"), "fos.tst"); 50 fos = new FileOutputStream(f); 51 } 52 53 /** 54 * @tests java.io.FileOutputStream#FileOutputStream(java.io.FileDescriptor) 55 */ 56 public void test_ConstructorLjava_io_FileDescriptor() throws IOException { 57 f = new File(fileName = System.getProperty("user.home"), "fos.tst"); 58 fileName = f.getAbsolutePath(); 59 fos = new FileOutputStream(fileName); 60 fos.write('l'); 61 fos.close(); 62 fis = new FileInputStream(fileName); 63 fos = new FileOutputStream(fis.getFD()); 64 fos.close(); 65 fis.close(); 66 } 67 68 /** 69 * @tests java.io.FileOutputStream#FileOutputStream(java.lang.String) 70 */ 71 public void test_ConstructorLjava_lang_String() throws IOException { 72 f = new File(fileName = System.getProperty("user.home"), "fos.tst"); 73 fileName = f.getAbsolutePath(); 74 fos = new FileOutputStream(fileName); 75 76 // Regression test for HARMONY-4012 77 new FileOutputStream("nul"); 78 } 79 80 /** 81 * @tests java.io.FileOutputStream#FileOutputStream(java.lang.String, 82 * boolean) 83 */ 84 public void test_ConstructorLjava_lang_StringZ() throws IOException { 85 f = new File(System.getProperty("user.home"), "fos.tst"); 86 fos = new FileOutputStream(f.getPath(), false); 87 fos.write("HI".getBytes(), 0, 2); 88 fos.close(); 89 fos = new FileOutputStream(f.getPath(), true); 90 fos.write(fileString.getBytes()); 91 fos.close(); 92 byte[] buf = new byte[fileString.length() + 2]; 93 fis = new FileInputStream(f.getPath()); 94 fis.read(buf, 0, buf.length); 95 assertTrue("Failed to create appending stream", new String(buf, 0, 96 buf.length).equals("HI" + fileString)); 97 } 98 99 /** 100 * @tests java.io.FileOutputStream#FileOutputStream(java.lang.String) 101 */ 102 public void test_ConstructorLjava_lang_String_I() throws IOException { 103 try { 104 fos = new FileOutputStream(""); 105 fail("should throw FileNotFoundException."); 106 } catch (FileNotFoundException e) { 107 // Expected 108 } finally { 109 if (fos != null) { 110 fos.close(); 111 } 112 } 113 try { 114 fos = new FileOutputStream(new File("")); 115 fail("should throw FileNotFoundException."); 116 } catch (FileNotFoundException e) { 117 // Expected 118 } finally { 119 if (fos != null) { 120 fos.close(); 121 } 122 } 123 } 124 125 /** 126 * @tests java.io.FileOutputStream#close() 127 */ 128 public void test_close() throws IOException { 129 f = new File(System.getProperty("user.home"), "output.tst"); 130 fos = new FileOutputStream(f.getPath()); 131 fos.close(); 132 133 try { 134 fos.write(fileString.getBytes()); 135 fail("Close test failed - wrote to closed stream"); 136 } catch (IOException e) { 137 // Expected 138 } 139 } 140 141 /** 142 * @tests java.io.FileOutputStream#getFD() 143 */ 144 public void test_getFD() throws IOException { 145 f = new File(fileName = System.getProperty("user.home"), "testfd"); 146 fileName = f.getAbsolutePath(); 147 fos = new FileOutputStream(f); 148 assertTrue("Returned invalid fd", fos.getFD().valid()); 149 fos.close(); 150 assertTrue("Returned invalid fd", !fos.getFD().valid()); 151 } 152 153 /** 154 * @tests java.io.FileOutputStream#write(byte[]) 155 */ 156 public void test_write$B() throws IOException { 157 f = new File(System.getProperty("user.home"), "output.tst"); 158 fos = new FileOutputStream(f.getPath()); 159 fos.write(fileString.getBytes()); 160 fis = new FileInputStream(f.getPath()); 161 byte rbytes[] = new byte[4000]; 162 fis.read(rbytes, 0, fileString.length()); 163 assertTrue("Incorrect string returned", new String(rbytes, 0, 164 fileString.length()).equals(fileString)); 165 } 166 167 /** 168 * @tests java.io.FileOutputStream#write(byte[], int, int) 169 */ 170 public void test_write$BII() throws IOException { 171 f = new File(System.getProperty("user.home"), "output.tst"); 172 fos = new FileOutputStream(f.getPath()); 173 fos.write(fileString.getBytes(), 0, fileString.length()); 174 fis = new FileInputStream(f.getPath()); 175 byte rbytes[] = new byte[4000]; 176 fis.read(rbytes, 0, fileString.length()); 177 assertTrue("Incorrect bytes written", new String(rbytes, 0, fileString 178 .length()).equals(fileString)); 179 180 // Regression test for HARMONY-285 181 File file = new File("FileOutputStream.tmp"); 182 file.deleteOnExit(); 183 FileOutputStream out = new FileOutputStream(file); 184 try { 185 out.write(null, 0, 0); 186 fail("Should throw NullPointerException"); 187 } catch (NullPointerException e) { 188 // Expected 189 } finally { 190 out.close(); 191 file.delete(); 192 } 193 } 194 195 /** 196 * @tests java.io.FileOutputStream#write(int) 197 */ 198 public void test_writeI() throws IOException { 199 f = new File(System.getProperty("user.home"), "output.tst"); 200 fos = new FileOutputStream(f.getPath()); 201 fos.write('t'); 202 fis = new FileInputStream(f.getPath()); 203 assertEquals("Incorrect char written", 't', fis.read()); 204 } 205 206 /** 207 * @tests java.io.FileOutputStream#write(byte[], int, int) 208 */ 209 public void test_write$BII2() throws IOException { 210 // Regression for HARMONY-437 211 f = new File(System.getProperty("user.home"), "output.tst"); 212 fos = new FileOutputStream(f.getPath()); 213 214 try { 215 fos.write(null, 1, 1); 216 fail("NullPointerException must be thrown"); 217 } catch (NullPointerException e) { 218 } 219 220 try { 221 fos.write(new byte[1], -1, 1); 222 fail("IndexOutOfBoundsException must be thrown if off <0"); 223 } catch (IndexOutOfBoundsException e) { 224 } 225 226 try { 227 fos.write(new byte[1], 0, -1); 228 fail("IndexOutOfBoundsException must be thrown if len <0"); 229 } catch (IndexOutOfBoundsException e) { 230 } 231 232 try { 233 fos.write(new byte[1], 0, 5); 234 fail("IndexOutOfBoundsException must be thrown if off+len > b.length"); 235 } catch (IndexOutOfBoundsException e) { 236 } 237 238 try { 239 fos.write(new byte[10], Integer.MAX_VALUE, 5); 240 fail("IndexOutOfBoundsException expected"); 241 } catch (IndexOutOfBoundsException e) { 242 } 243 244 try { 245 fos.write(new byte[10], 5, Integer.MAX_VALUE); 246 fail("IndexOutOfBoundsException expected"); 247 } catch (IndexOutOfBoundsException e) { 248 } 249 fos.close(); 250 } 251 252 /** 253 * @tests java.io.FileOutputStream#write(byte[], int, int) 254 */ 255 public void test_write$BII3() throws IOException { 256 // Regression for HARMONY-834 257 // no exception expected 258 new FileOutputStream(new FileDescriptor()).write(new byte[1], 0, 0); 259 } 260 261 /** 262 * @tests java.io.FileOutputStream#getChannel() 263 */ 264 public void test_getChannel() throws IOException { 265 // Regression for HARMONY-508 266 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 267 tmpfile.deleteOnExit(); 268 FileOutputStream fos = new FileOutputStream(tmpfile); 269 fos.write(bytes); 270 fos.flush(); 271 fos.close(); 272 FileOutputStream f = new FileOutputStream(tmpfile, true); 273 // Harmony expected 10, but the RI and Android report 0. 274 assertEquals(0, f.getChannel().position()); 275 } 276 277 public void test_getChannel_Append() throws IOException { 278 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 279 tmpfile.deleteOnExit(); 280 FileOutputStream fos = new FileOutputStream(tmpfile, true); 281 assertEquals(0, fos.getChannel().position()); 282 fos.write(bytes); 283 assertEquals(10, fos.getChannel().position()); 284 fos.write(bytes); 285 assertEquals(20, fos.getChannel().position()); 286 fos.write(bytes); 287 assertEquals(30, fos.getChannel().position()); 288 fos.close(); 289 290 try { 291 fos.getChannel().position(); 292 fail("should throw ClosedChannelException"); 293 } catch (java.nio.channels.ClosedChannelException e){ 294 // Expected 295 } 296 } 297 298 public void test_getChannel_UnAppend() throws IOException { 299 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 300 tmpfile.deleteOnExit(); 301 FileOutputStream fos = new FileOutputStream(tmpfile, false); 302 assertEquals(0, fos.getChannel().position()); 303 fos.write(bytes); 304 assertEquals(10, fos.getChannel().position()); 305 fos.write(bytes); 306 assertEquals(20, fos.getChannel().position()); 307 fos.write(bytes); 308 assertEquals(30, fos.getChannel().position()); 309 fos.close(); 310 311 try { 312 fos.getChannel().position(); 313 fail("should throw ClosedChannelException"); 314 } catch (java.nio.channels.ClosedChannelException e){ 315 // Expected 316 } 317 } 318 319 public void test_getChannel_Unappend_Unappend() throws IOException { 320 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 321 tmpfile.deleteOnExit(); 322 FileOutputStream fos = new FileOutputStream(tmpfile, false); 323 assertEquals(0, fos.getChannel().position()); 324 fos.write(bytes); 325 assertEquals(10, fos.getChannel().position()); 326 fos.close(); 327 328 fos = new FileOutputStream(tmpfile, false); 329 assertEquals(0, fos.getChannel().position()); 330 fos.close(); 331 } 332 333 public void test_getChannel_Unappend_Append() throws IOException { 334 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 335 tmpfile.deleteOnExit(); 336 FileOutputStream fos = new FileOutputStream(tmpfile, false); 337 assertEquals(0, fos.getChannel().position()); 338 fos.write(bytes); 339 assertEquals(10, fos.getChannel().position()); 340 fos.close(); 341 342 fos = new FileOutputStream(tmpfile, true); 343 // Harmony expected 10, but the RI and Android report 0. 344 assertEquals(0, fos.getChannel().position()); 345 fos.close(); 346 } 347 348 public void test_getChannel_Append_Unappend() throws IOException { 349 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 350 tmpfile.deleteOnExit(); 351 FileOutputStream fos = new FileOutputStream(tmpfile, true); 352 assertEquals(0, fos.getChannel().position()); 353 fos.write(bytes); 354 assertEquals(10, fos.getChannel().position()); 355 fos.close(); 356 357 fos = new FileOutputStream(tmpfile, false); 358 assertEquals(0, fos.getChannel().position()); 359 fos.close(); 360 } 361 362 public void test_getChanne_Append_Append() throws IOException { 363 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 364 tmpfile.deleteOnExit(); 365 FileOutputStream fos = new FileOutputStream(tmpfile, true); 366 assertEquals(0, fos.getChannel().position()); 367 fos.write(bytes); 368 assertEquals(10, fos.getChannel().position()); 369 fos.close(); 370 371 fos = new FileOutputStream(tmpfile, true); 372 // Harmony expected 10, but the RI and Android report 0. 373 assertEquals(0, fos.getChannel().position()); 374 fos.close(); 375 } 376 377 protected void setUp() { 378 bytes = new byte[10]; 379 for (int i = 0; i < bytes.length; i++) { 380 bytes[i] = (byte) i; 381 } 382 } 383 384 /** 385 * Tears down the fixture, for example, close a network connection. This 386 * method is called after a test is executed. 387 */ 388 @Override 389 protected void tearDown() throws Exception { 390 super.tearDown(); 391 if (f != null) { 392 f.delete(); 393 } 394 if (fis != null) { 395 fis.close(); 396 } 397 if (fos != null) { 398 fos.close(); 399 } 400 } 401} 402