Path: blob/master/test/jdk/java/nio/file/Files/DeleteOnClose.java
41153 views
/*1* Copyright (c) 2008, 2017, 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/*24* @test25* @bug 431388726* @summary Unit test for DELETE_ON_CLOSE open option27* @library /test/lib ..28* @build jdk.test.lib.Utils29* jdk.test.lib.Asserts30* jdk.test.lib.JDKToolFinder31* jdk.test.lib.JDKToolLauncher32* jdk.test.lib.Platform33* jdk.test.lib.process.*34* @run main DeleteOnClose35*/3637import java.io.IOException;38import java.nio.file.DirectoryStream;39import java.nio.file.Files;40import java.nio.file.OpenOption;41import java.nio.file.Path;42import java.nio.file.Paths;43import java.nio.file.SecureDirectoryStream;44import java.util.HashSet;45import java.util.Set;4647import jdk.test.lib.process.ProcessTools;4849import static java.nio.file.StandardOpenOption.READ;50import static java.nio.file.StandardOpenOption.WRITE;51import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;5253public class DeleteOnClose {5455public static void main(String[] args) throws Exception {56if (args.length == 0) {57Path file = Files.createTempFile("blah", "tmp");58ProcessTools.executeTestJava(DeleteOnClose.class.getName(),59file.toAbsolutePath().toString())60.shouldHaveExitValue(0);61runTest(file);62} else {63// open file but do not close it. Its existance will be checked by64// the caller.65Files.newByteChannel(Paths.get(args[0]), READ, WRITE, DELETE_ON_CLOSE);66}67}6869public static void runTest(Path path) throws Exception {70// check temporary file has been deleted after jvm termination71if (Files.exists(path)) {72throw new RuntimeException("Temporary file was not deleted");73}7475// check temporary file has been deleted after closing it76Path file = Files.createTempFile("blep", "tmp");77Files.newByteChannel(file, READ, WRITE, DELETE_ON_CLOSE).close();78if (Files.exists(file))79throw new RuntimeException("Temporary file was not deleted");8081Path dir = Files.createTempDirectory("blah");82try {83// check that DELETE_ON_CLOSE fails when file is a sym link84if (TestUtil.supportsLinks(dir)) {85file = dir.resolve("foo");86Files.createFile(file);87Path link = dir.resolve("link");88Files.createSymbolicLink(link, file);89try {90Files.newByteChannel(link, READ, WRITE, DELETE_ON_CLOSE);91throw new RuntimeException("IOException expected");92} catch (IOException ignore) { }93}9495// check that DELETE_ON_CLOSE works with files created via open96// directories97try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {98if (stream instanceof SecureDirectoryStream) {99SecureDirectoryStream<Path> secure = (SecureDirectoryStream<Path>)stream;100file = Paths.get("foo");101102Set<OpenOption> opts = new HashSet<>();103opts.add(WRITE);104opts.add(DELETE_ON_CLOSE);105secure.newByteChannel(file, opts).close();106107if (Files.exists(dir.resolve(file)))108throw new RuntimeException("File not deleted");109}110}111} finally {112TestUtil.removeAll(dir);113}114}115}116117118