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.File; 21import java.io.FileOutputStream; 22import java.io.IOException; 23import java.net.MalformedURLException; 24import java.net.URL; 25import junit.framework.TestCase; 26import static tests.support.Support_Exec.execAndGetOutput; 27import static tests.support.Support_Exec.javaProcessBuilder; 28 29public class OldFileTest extends TestCase { 30 31 /** Location to store tests in */ 32 private File tempDirectory; 33 34 /** Temp file that does exist */ 35 private File tempFile; 36 37 /** File separator */ 38 private String slash = File.separator; 39 40 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_File\nTest_FileDescriptor\nTest_FileInputStream\nTest_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"; 41 42 private static String platformId = "Android" 43 + System.getProperty("java.vm.version").replace('.', '-'); 44 45 { 46 // Delete all old temporary files 47 File tempDir = new File(System.getProperty("java.io.tmpdir")); 48 String[] files = tempDir.list(); 49 for (int i = 0; i < files.length; i++) { 50 File f = new File(tempDir, files[i]); 51 if (f.isDirectory()) { 52 if (files[i].startsWith("hyts_resources")) 53 deleteTempFolder(f); 54 } 55 if (files[i].startsWith("hyts_") || files[i].startsWith("hyjar_")) 56 new File(tempDir, files[i]).delete(); 57 } 58 } 59 60 private void deleteTempFolder(File dir) { 61 String files[] = dir.list(); 62 for (int i = 0; i < files.length; i++) { 63 File f = new File(dir, files[i]); 64 if (f.isDirectory()) 65 deleteTempFolder(f); 66 else { 67 f.delete(); 68 } 69 } 70 dir.delete(); 71 72 } 73 74 public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception { 75 String error; 76 String dirName = System.getProperty("java.io.tmpdir"); 77 System.setProperty("user.dir", dirName); 78 79 File d = new File(dirName); 80 File f = new File(d, "input.tst"); 81 if (!dirName.regionMatches((dirName.length() - 1), slash, 0, 1)) 82 dirName += slash; 83 dirName += "input.tst"; 84 error = String.format("Test 1: Incorrect file created: %s; %s expected.", f.getPath(), dirName); 85 assertTrue(error, f.getPath().equals(dirName)); 86 87 String fileName = null; 88 try { 89 f = new File(d, fileName); 90 fail("Test 2: NullPointerException expected."); 91 } catch (NullPointerException e) { 92 } 93 94 d = null; 95 f = new File(d, "input.tst"); 96 error = String.format("Test 3: Incorrect file created: %s; %s expected.", 97 f.getAbsolutePath(), dirName); 98 assertTrue(error, f.getAbsolutePath().equals(dirName)); 99 100 // Regression test for Harmony-382 101 File s = null; 102 f = new File("/abc"); 103 d = new File(s, "/abc"); 104 assertEquals("Test 4: Incorrect file created;", 105 f.getAbsolutePath(), d.getAbsolutePath()); 106 } 107 108 public void test_ConstructorLjava_lang_StringLjava_lang_String() throws IOException { 109 String dirName = null; 110 String fileName = "input.tst"; 111 112 String userDir = System.getProperty("java.io.tmpdir"); 113 System.setProperty("user.dir", userDir); 114 115 File f = new File(dirName, fileName); 116 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1)) 117 userDir += slash; 118 userDir += "input.tst"; 119 String error = String.format("Test 1: Incorrect file created: %s; %s expected.", 120 f.getAbsolutePath(), userDir); 121 assertTrue(error, f.getAbsolutePath().equals(userDir)); 122 123 dirName = System.getProperty("java.io.tmpdir"); 124 fileName = null; 125 try { 126 f = new File(dirName, fileName); 127 fail("Test 2: NullPointerException expected."); 128 } catch (NullPointerException e) { 129 // Expected. 130 } 131 132 fileName = "input.tst"; 133 f = new File(dirName, fileName); 134 assertTrue("Test 3: Incorrect file created.", f.getPath() 135 .equals(userDir)); 136 137 // Regression test for Harmony-382 138 String s = null; 139 f = new File("/abc"); 140 File d = new File(s, "/abc"); 141 assertEquals("Test 4: Incorrect file created;", d.getAbsolutePath(), f 142 .getAbsolutePath()); 143 assertEquals("Test3: Created Incorrect File", "/abc", f 144 .getAbsolutePath()); 145 } 146 147 public void test_createTempFileLjava_lang_StringLjava_lang_String() { 148 try { 149 // Providing an illegal file prefix. 150 File f3 = File.createTempFile("/../../../../../", null); 151 f3.delete(); 152 fail("IOException not thrown"); 153 } catch (IOException e) { 154 } 155 } 156 157 public void test_renameToLjava_io_File() { 158 String base = System.getProperty("java.io.tmpdir"); 159 File dir = new File(base, platformId); 160 dir.mkdir(); 161 File f = new File(dir, "xxx.xxx"); 162 try { 163 f.renameTo(null); 164 fail("Test 1: NullPointerException expected."); 165 } catch (NullPointerException e) { 166 // Expected. 167 } 168 } 169 170 public void test_toURL3() throws MalformedURLException { 171 File dir = new File(""); // current directory 172 String newDirURL = dir.toURL().toString(); 173 assertTrue("Test 1: URL does not end with slash.", 174 newDirURL.endsWith("/")); 175 } 176 177 public void test_deleteOnExit() throws IOException, InterruptedException { 178 String cts = System.getProperty("java.io.tmpdir"); 179 File dir = new File(cts + "/hello"); 180 dir.mkdir(); 181 assertTrue(dir.exists()); 182 File subDir = new File(cts + "/hello/world"); 183 subDir.mkdir(); 184 assertTrue(subDir.exists()); 185 186 URL url = getClass().getResource("/HelloWorld.txt"); 187 String classPath = url.toString(); 188 int idx = classPath.indexOf("!"); 189 assertTrue("could not find the path of the test jar/apk", idx > 0); 190 classPath = classPath.substring(9, idx); // cutting off jar:file: 191 192 ProcessBuilder builder = javaProcessBuilder(); 193 builder.command().add("-cp"); 194 builder.command().add(System.getProperty("java.class.path")); 195 builder.command().add("tests.support.Support_DeleteOnExitTest"); 196 builder.command().add(dir.getAbsolutePath()); 197 builder.command().add(subDir.getAbsolutePath()); 198 execAndGetOutput(builder); 199 200 assertFalse(dir.exists()); 201 assertFalse(subDir.exists()); 202 } 203 204 protected void setUp() throws Exception { 205 super.setUp(); 206 207 // Make sure that system properties are set correctly 208 String userDir = System.getProperty("java.io.tmpdir"); 209 if (userDir == null) 210 throw new Exception("System property java.io.tmpdir not defined."); 211 System.setProperty("java.io.tmpdir", userDir); 212 213 /** Setup the temporary directory */ 214 if (!userDir.regionMatches((userDir.length() - 1), slash, 0, 1)) 215 userDir += slash; 216 tempDirectory = new File(userDir + "tempDir" 217 + String.valueOf(System.currentTimeMillis())); 218 if (!tempDirectory.mkdir()) 219 System.out.println("Setup for OldFileTest failed (1)."); 220 221 /** Setup the temporary file */ 222 tempFile = new File(tempDirectory, "tempfile"); 223 FileOutputStream tempStream; 224 try { 225 tempStream = new FileOutputStream(tempFile.getPath(), false); 226 tempStream.close(); 227 } catch (IOException e) { 228 System.out.println("Setup for OldFileTest failed (2)."); 229 return; 230 } 231 } 232 233 protected void tearDown() { 234 if (tempFile.exists() && !tempFile.delete()) 235 System.out 236 .println("OldFileTest.tearDown() failed, could not delete file!"); 237 if (!tempDirectory.delete()) 238 System.out 239 .println("OldFileTest.tearDown() failed, could not delete directory!"); 240 } 241} 242