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