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