Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/DeleteOnClose.java
41153 views
1
/*
2
* Copyright (c) 2008, 2017, 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
/*
25
* @test
26
* @bug 4313887
27
* @summary Unit test for DELETE_ON_CLOSE open option
28
* @library /test/lib ..
29
* @build jdk.test.lib.Utils
30
* jdk.test.lib.Asserts
31
* jdk.test.lib.JDKToolFinder
32
* jdk.test.lib.JDKToolLauncher
33
* jdk.test.lib.Platform
34
* jdk.test.lib.process.*
35
* @run main DeleteOnClose
36
*/
37
38
import java.io.IOException;
39
import java.nio.file.DirectoryStream;
40
import java.nio.file.Files;
41
import java.nio.file.OpenOption;
42
import java.nio.file.Path;
43
import java.nio.file.Paths;
44
import java.nio.file.SecureDirectoryStream;
45
import java.util.HashSet;
46
import java.util.Set;
47
48
import jdk.test.lib.process.ProcessTools;
49
50
import static java.nio.file.StandardOpenOption.READ;
51
import static java.nio.file.StandardOpenOption.WRITE;
52
import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;
53
54
public class DeleteOnClose {
55
56
public static void main(String[] args) throws Exception {
57
if (args.length == 0) {
58
Path file = Files.createTempFile("blah", "tmp");
59
ProcessTools.executeTestJava(DeleteOnClose.class.getName(),
60
file.toAbsolutePath().toString())
61
.shouldHaveExitValue(0);
62
runTest(file);
63
} else {
64
// open file but do not close it. Its existance will be checked by
65
// the caller.
66
Files.newByteChannel(Paths.get(args[0]), READ, WRITE, DELETE_ON_CLOSE);
67
}
68
}
69
70
public static void runTest(Path path) throws Exception {
71
// check temporary file has been deleted after jvm termination
72
if (Files.exists(path)) {
73
throw new RuntimeException("Temporary file was not deleted");
74
}
75
76
// check temporary file has been deleted after closing it
77
Path file = Files.createTempFile("blep", "tmp");
78
Files.newByteChannel(file, READ, WRITE, DELETE_ON_CLOSE).close();
79
if (Files.exists(file))
80
throw new RuntimeException("Temporary file was not deleted");
81
82
Path dir = Files.createTempDirectory("blah");
83
try {
84
// check that DELETE_ON_CLOSE fails when file is a sym link
85
if (TestUtil.supportsLinks(dir)) {
86
file = dir.resolve("foo");
87
Files.createFile(file);
88
Path link = dir.resolve("link");
89
Files.createSymbolicLink(link, file);
90
try {
91
Files.newByteChannel(link, READ, WRITE, DELETE_ON_CLOSE);
92
throw new RuntimeException("IOException expected");
93
} catch (IOException ignore) { }
94
}
95
96
// check that DELETE_ON_CLOSE works with files created via open
97
// directories
98
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
99
if (stream instanceof SecureDirectoryStream) {
100
SecureDirectoryStream<Path> secure = (SecureDirectoryStream<Path>)stream;
101
file = Paths.get("foo");
102
103
Set<OpenOption> opts = new HashSet<>();
104
opts.add(WRITE);
105
opts.add(DELETE_ON_CLOSE);
106
secure.newByteChannel(file, opts).close();
107
108
if (Files.exists(dir.resolve(file)))
109
throw new RuntimeException("File not deleted");
110
}
111
}
112
} finally {
113
TestUtil.removeAll(dir);
114
}
115
}
116
}
117
118