DeleteOnExit.java revision 2362:00cd9dc3c2b5
1/*
2 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @bug  4614121 4809375 6437591
26   @summary Basic test for deleteOnExit method
27   @author kladko
28 */
29
30
31import java.io.File;
32
33public class DeleteOnExit  {
34
35    static String tmpdir = System.getProperty("java.io.tmpdir");
36    static String java = System.getProperty("java.home") + File.separator +
37                         "bin" + File.separator + "java";
38    static File file1 = new File(tmpdir + "deletedOnExit1");
39    static File file2 = new File(tmpdir + "deletedOnExit2");
40    static File file3 = new File(tmpdir + "deletedOnExit3");
41
42    // used to verify deletion order
43    static File dir = new File(tmpdir + "deletedOnExitDir");
44    static File file4 = new File(dir + File.separator + "deletedOnExit4");
45    static File file5 = new File(dir + File.separator + "dxnsdnguidfgejngognrogn");
46    static File file6 = new File(dir + File.separator + "mmmmmmsdmfgmdsmfgmdsfgm");
47    static File file7 = new File(dir + File.separator + "12345566777");
48
49    public static void main (String args[]) throws Exception{
50        if (args.length == 0) {
51            Runtime.getRuntime().exec(java +  " DeleteOnExit -test").waitFor();
52            if (file1.exists() || file2.exists() || file3.exists() ||
53                dir.exists()   || file4.exists() || file5.exists() ||
54                file6.exists() || file7.exists())  {
55
56                System.out.println(file1 + ", exists = " + file1.exists());
57                System.out.println(file2 + ", exists = " + file2.exists());
58                System.out.println(file3 + ", exists = " + file3.exists());
59                System.out.println(dir + ", exists = " + dir.exists());
60                System.out.println(file4 + ", exists = " + file4.exists());
61                System.out.println(file5 + ", exists = " + file5.exists());
62                System.out.println(file6 + ", exists = " + file6.exists());
63                System.out.println(file7 + ", exists = " + file7.exists());
64
65                // cleanup undeleted dir if test fails
66                dir.delete();
67
68                throw new Exception("File exists");
69            }
70        } else {
71            file1.createNewFile();
72            file2.createNewFile();
73            file3.createNewFile();
74            file1.deleteOnExit();
75            file2.deleteOnExit();
76            file3.deleteOnExit();
77
78            // verify that deleting a File marked deleteOnExit will not cause a problem
79            // during shutdown.
80            file3.delete();
81
82            // verify that calling deleteOnExit multiple times on a File does not cause
83            // a problem during shutdown.
84            file2.deleteOnExit();
85            file2.deleteOnExit();
86            file2.deleteOnExit();
87
88            // Verify DeleteOnExit Internal implementation deletion order.
89            if (dir.mkdir()) {
90                dir.deleteOnExit();
91
92                file4.createNewFile();
93                file5.createNewFile();
94                file6.createNewFile();
95                file7.createNewFile();
96
97                file4.deleteOnExit();
98                file5.deleteOnExit();
99                file6.deleteOnExit();
100                file7.deleteOnExit();
101            }
102        }
103    }
104}
105