Path: blob/master/test/jdk/java/io/File/DeleteOnExit.java
41149 views
/*1* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4614121 4809375 643759125@summary Basic test for deleteOnExit method26@author kladko27*/282930import java.io.File;3132public class DeleteOnExit {3334static String tmpdir = System.getProperty("java.io.tmpdir");35static String java = System.getProperty("java.home") + File.separator +36"bin" + File.separator + "java";37static File file1 = new File(tmpdir + "deletedOnExit1");38static File file2 = new File(tmpdir + "deletedOnExit2");39static File file3 = new File(tmpdir + "deletedOnExit3");4041// used to verify deletion order42static File dir = new File(tmpdir + "deletedOnExitDir");43static File file4 = new File(dir + File.separator + "deletedOnExit4");44static File file5 = new File(dir + File.separator + "dxnsdnguidfgejngognrogn");45static File file6 = new File(dir + File.separator + "mmmmmmsdmfgmdsmfgmdsfgm");46static File file7 = new File(dir + File.separator + "12345566777");4748public static void main (String args[]) throws Exception{49if (args.length == 0) {50String cmd = java + " -classpath " + System.getProperty("test.classes")51+ " DeleteOnExit -test";52Runtime.getRuntime().exec(cmd).waitFor();53if (file1.exists() || file2.exists() || file3.exists() ||54dir.exists() || file4.exists() || file5.exists() ||55file6.exists() || file7.exists()) {5657System.out.println(file1 + ", exists = " + file1.exists());58System.out.println(file2 + ", exists = " + file2.exists());59System.out.println(file3 + ", exists = " + file3.exists());60System.out.println(dir + ", exists = " + dir.exists());61System.out.println(file4 + ", exists = " + file4.exists());62System.out.println(file5 + ", exists = " + file5.exists());63System.out.println(file6 + ", exists = " + file6.exists());64System.out.println(file7 + ", exists = " + file7.exists());6566// cleanup undeleted dir if test fails67dir.delete();6869throw new Exception("File exists");70}71} else {72file1.createNewFile();73file2.createNewFile();74file3.createNewFile();75file1.deleteOnExit();76file2.deleteOnExit();77file3.deleteOnExit();7879// verify that deleting a File marked deleteOnExit will not cause a problem80// during shutdown.81file3.delete();8283// verify that calling deleteOnExit multiple times on a File does not cause84// a problem during shutdown.85file2.deleteOnExit();86file2.deleteOnExit();87file2.deleteOnExit();8889// Verify DeleteOnExit Internal implementation deletion order.90if (dir.mkdir()) {91dir.deleteOnExit();9293file4.createNewFile();94file5.createNewFile();95file6.createNewFile();96file7.createNewFile();9798file4.deleteOnExit();99file5.deleteOnExit();100file6.deleteOnExit();101file7.deleteOnExit();102}103}104}105}106107108