Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/File/createTempFile/SpecialTempFile.java
38821 views
1
/*
2
* Copyright (c) 2013, 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 8013827 8011950 8017212 8025128
27
* @summary Check whether File.createTempFile can handle special parameters
28
* @author Dan Xu
29
*/
30
31
import java.io.File;
32
import java.io.IOException;
33
34
public class SpecialTempFile {
35
36
private static void test(String name, String[] prefix, String[] suffix,
37
boolean exceptionExpected) throws IOException
38
{
39
if (prefix == null || suffix == null
40
|| prefix.length != suffix.length)
41
{
42
return;
43
}
44
45
final String exceptionMsg = "Unable to create temporary file";
46
String[] dirs = { null, "." };
47
48
for (int i = 0; i < prefix.length; i++) {
49
boolean exceptionThrown = false;
50
File f = null;
51
52
for (String dir: dirs) {
53
System.out.println("In test " + name +
54
", creating temp file with prefix, " +
55
prefix[i] + ", suffix, " + suffix[i] +
56
", in dir, " + dir);
57
58
try {
59
if (dir == null || dir.isEmpty())
60
f = File.createTempFile(prefix[i], suffix[i]);
61
else
62
f = File.createTempFile(prefix[i], suffix[i], new File(dir));
63
} catch (IOException e) {
64
if (exceptionExpected) {
65
if (e.getMessage().startsWith(exceptionMsg))
66
exceptionThrown = true;
67
else
68
System.out.println("Wrong error message:" +
69
e.getMessage());
70
} else {
71
throw e;
72
}
73
}
74
75
if (exceptionExpected && (!exceptionThrown || f != null))
76
throw new RuntimeException("IOException is expected");
77
}
78
}
79
}
80
81
public static void main(String[] args) throws Exception {
82
// Common test
83
final String name = "SpecialTempFile";
84
File f = new File(System.getProperty("java.io.tmpdir"), name);
85
if (!f.exists()) {
86
f.createNewFile();
87
}
88
String[] nulPre = { name + "\u0000" };
89
String[] nulSuf = { ".test" };
90
test("NulName", nulPre, nulSuf, true);
91
92
// Test JDK-8025128
93
String[] goodPre = { "///..///", "/foo" };
94
String[] goodSuf = { ".temp", ".tmp" };
95
test("goodName", goodPre, goodSuf, false);
96
97
// Test JDK-8011950
98
String[] slashPre = { "temp", "///..///", "/foo" };
99
String[] slashSuf = { "///..///..", "///..///..", "///..///.." };
100
test("SlashedName", slashPre, slashSuf, true);
101
102
// Windows tests
103
if (!System.getProperty("os.name").startsWith("Windows"))
104
return;
105
106
// Test JDK-8013827
107
String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
108
String[] resvSuf = { ".temp", ".temp" };
109
test("ReservedName", resvPre, resvSuf, true);
110
}
111
}
112
113