1 /*
   2  * Copyright (c) 2002, 2019, 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 8193072
  26    @summary Basic test for deleteOnExit method
  27    @author kladko
  28  */
  29 
  30 
  31 import java.io.File;
  32 
  33 public 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     // used to test cancelDeleteOnExit()
  50     static File file8 = new File(tmpdir + "cancelledDeleteOnExit8");
  51     static File file9 = new File(tmpdir + "uncancelledDeleteOnExit9");
  52 
  53     public static void main (String args[]) throws Exception{
  54         if (args.length == 0) {
  55             String cmd = java + " -classpath " + System.getProperty("test.classes")
  56                 + " DeleteOnExit -test";
  57             Runtime.getRuntime().exec(cmd).waitFor();
  58             if (file1.exists() || file2.exists() || file3.exists() ||
  59                 dir.exists()   || file4.exists() || file5.exists() ||
  60                 file6.exists() || file7.exists() || file9.exists())  {
  61 
  62                 System.out.println(file1 + ", exists = " + file1.exists());
  63                 System.out.println(file2 + ", exists = " + file2.exists());
  64                 System.out.println(file3 + ", exists = " + file3.exists());
  65                 System.out.println(dir + ", exists = " + dir.exists());
  66                 System.out.println(file4 + ", exists = " + file4.exists());
  67                 System.out.println(file5 + ", exists = " + file5.exists());
  68                 System.out.println(file6 + ", exists = " + file6.exists());
  69                 System.out.println(file7 + ", exists = " + file7.exists());
  70                 System.out.println(file9 + ", exists = " + file9.exists());
  71 
  72                 // cleanup undeleted dir if test fails
  73                 dir.delete();
  74 
  75                 throw new Exception("File exists");
  76             } else if (!file8.exists()) {
  77                 System.out.println(file8 + ", exists = " + file8.exists());
  78 
  79                 throw new Exception("File does not exist");
  80             }
  81         } else {
  82             file1.createNewFile();
  83             file2.createNewFile();
  84             file3.createNewFile();
  85             file1.deleteOnExit();
  86             file2.deleteOnExit();
  87             file3.deleteOnExit();
  88 
  89             // verify that deleting a File marked deleteOnExit will not cause a problem
  90             // during shutdown.
  91             file3.delete();
  92 
  93             // verify that calling deleteOnExit multiple times on a File does not cause
  94             // a problem during shutdown.
  95             file2.deleteOnExit();
  96             file2.deleteOnExit();
  97             file2.deleteOnExit();
  98 
  99             // Verify DeleteOnExit Internal implementation deletion order.
 100             if (dir.mkdir()) {
 101                 dir.deleteOnExit();
 102 
 103                 file4.createNewFile();
 104                 file5.createNewFile();
 105                 file6.createNewFile();
 106                 file7.createNewFile();
 107 
 108                 file4.deleteOnExit();
 109                 file5.deleteOnExit();
 110                 file6.deleteOnExit();
 111                 file7.deleteOnExit();
 112             }
 113 
 114             // Check cancelDeleteOnExit
 115             file8.createNewFile();
 116             file8.deleteOnExit();
 117             file8.cancelDeleteOnExit();
 118             file9.createNewFile();
 119             file9.deleteOnExit();
 120             file9.deleteOnExit();
 121             file9.cancelDeleteOnExit();
 122         }
 123     }
 124 }