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