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 
18 package libcore.java.io;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import junit.framework.TestCase;
25 import static tests.support.Support_Exec.execAndGetOutput;
26 import static tests.support.Support_Exec.javaProcessBuilder;
27 
28 public class OldFileTest extends TestCase {
29     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";
30 
31     private static String platformId = "Android"
32             + System.getProperty("java.vm.version").replace('.', '-');
33 
34     {
35         // Delete all old temporary files
36         File tempDir = new File(System.getProperty("java.io.tmpdir"));
37         String[] files = tempDir.list();
38         for (int i = 0; i < files.length; i++) {
39             File f = new File(tempDir, files[i]);
40             if (f.isDirectory()) {
41                 if (files[i].startsWith("hyts_resources"))
42                     deleteTempFolder(f);
43             }
44             if (files[i].startsWith("hyts_") || files[i].startsWith("hyjar_"))
45                 new File(tempDir, files[i]).delete();
46         }
47     }
48 
deleteTempFolder(File dir)49     private void deleteTempFolder(File dir) {
50         String files[] = dir.list();
51         for (int i = 0; i < files.length; i++) {
52             File f = new File(dir, files[i]);
53             if (f.isDirectory())
54                 deleteTempFolder(f);
55             else {
56                 f.delete();
57             }
58         }
59         dir.delete();
60 
61     }
62 
test_ConstructorLjava_io_FileLjava_lang_String()63     public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception {
64         String tmpDirName = System.getProperty("java.io.tmpdir");
65 
66         File d = new File(tmpDirName);
67         File f = new File(d, "input.tst");
68 
69         if (!tmpDirName.endsWith(File.separator)) {
70             tmpDirName += File.separator;
71         }
72         tmpDirName += "input.tst";
73 
74         assertEquals(tmpDirName, f.getPath());
75 
76         String fileName = null;
77         try {
78             f = new File(d, fileName);
79             fail();
80         } catch (NullPointerException expected) {
81         }
82 
83         f = new File(d, "input.tst");
84         assertEquals(tmpDirName, f.getAbsolutePath());
85 
86         // Regression test for Harmony-382
87         File s = null;
88         f = new File("/abc");
89         d = new File(s, "/abc");
90         assertEquals(f.getAbsolutePath(), d.getAbsolutePath());
91     }
92 
test_ConstructorLjava_lang_StringLjava_lang_String()93     public void test_ConstructorLjava_lang_StringLjava_lang_String() throws IOException {
94         String tmpDir = System.getProperty("java.io.tmpdir");
95         if (!tmpDir.endsWith(File.separator)) {
96             tmpDir += File.separator;
97         }
98 
99         String dirName = tmpDir;
100         String fileName = "input.tst";
101         File f = new File(tmpDir, fileName);
102         tmpDir += "input.tst";
103         assertEquals(tmpDir, f.getAbsolutePath());
104 
105         dirName = System.getProperty("java.io.tmpdir");
106         fileName = null;
107         try {
108             f = new File(dirName, fileName);
109             fail();
110         } catch (NullPointerException expected) {
111             // Expected.
112         }
113 
114         fileName = "input.tst";
115         f = new File(dirName, fileName);
116         assertEquals(tmpDir, f.getPath());
117 
118         // Regression test for Harmony-382
119         String s = null;
120         f = new File("/abc");
121         File d = new File(s, "/abc");
122         assertEquals(d.getAbsolutePath(), f.getAbsolutePath());
123         assertEquals("/abc", f.getAbsolutePath());
124     }
125 
test_createTempFileLjava_lang_StringLjava_lang_String()126     public void test_createTempFileLjava_lang_StringLjava_lang_String() {
127         try {
128             // Providing an illegal file prefix.
129             File f3 = File.createTempFile("/../../../../../", null);
130             f3.delete();
131             fail("IOException not thrown");
132         } catch (IOException e) {
133         }
134     }
135 
test_renameToLjava_io_File()136     public void test_renameToLjava_io_File() {
137         String base = System.getProperty("java.io.tmpdir");
138         File dir = new File(base, platformId);
139         dir.mkdir();
140         File f = new File(dir, "xxx.xxx");
141         try {
142             f.renameTo(null);
143             fail("Test 1: NullPointerException expected.");
144         } catch (NullPointerException e) {
145             // Expected.
146         }
147     }
148 
test_toURL3()149     public void test_toURL3() throws MalformedURLException {
150         File dir = new File(""); // current directory
151         String newDirURL = dir.toURL().toString();
152         assertTrue("Test 1: URL does not end with slash.", newDirURL.endsWith(File.separator));
153     }
154 
test_deleteOnExit()155     public void test_deleteOnExit() throws IOException, InterruptedException {
156         String cts = System.getProperty("java.io.tmpdir");
157         File dir = new File(cts + "/hello");
158         dir.mkdir();
159         assertTrue(dir.exists());
160         File subDir = new File(cts + "/hello/world");
161         subDir.mkdir();
162         assertTrue(subDir.exists());
163 
164         URL url = getClass().getResource("/HelloWorld.txt");
165         String classPath = url.toString();
166         int idx = classPath.indexOf("!");
167         assertTrue("could not find the path of the test jar/apk", idx > 0);
168         classPath = classPath.substring(9, idx); // cutting off jar:file:
169 
170         ProcessBuilder builder = javaProcessBuilder();
171         builder.command().add("-cp");
172         builder.command().add(System.getProperty("java.class.path"));
173         builder.command().add("tests.support.Support_DeleteOnExitTest");
174         builder.command().add(dir.getAbsolutePath());
175         builder.command().add(subDir.getAbsolutePath());
176         execAndGetOutput(builder);
177 
178         assertFalse(dir.exists());
179         assertFalse(subDir.exists());
180     }
181 }
182