Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/8062561/bug8062561.java
38854 views
1
/*
2
* Copyright (c) 2014, 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
import java.awt.Robot;
25
import java.awt.event.KeyEvent;
26
import java.io.File;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.PrintWriter;
30
import java.util.concurrent.TimeUnit;
31
import javax.swing.JFileChooser;
32
import javax.swing.SwingUtilities;
33
import javax.swing.filechooser.FileSystemView;
34
import sun.awt.OSInfo;
35
36
/**
37
* @test
38
* @bug 8062561
39
* @summary File system view returns null default directory
40
* @run main/othervm bug8062561 GENERATE_POLICY
41
* @run main/othervm/policy=security.policy bug8062561 CHECK_DEFAULT_DIR run
42
*/
43
public class bug8062561 {
44
45
private static final String POLICY_FILE = "security2.policy";
46
private static volatile boolean fileChooserIsShown = false;
47
48
public static void main(String[] args) throws Exception {
49
50
String test = args[0];
51
52
switch (test) {
53
case "GENERATE_POLICY":
54
generatePolicyFile();
55
break;
56
case "CHECK_DEFAULT_DIR":
57
checkDefaultDirectory();
58
break;
59
case "CHECK_FILE_CHOOSER":
60
checkFileChooser();
61
break;
62
default:
63
throw new RuntimeException("Wrong argument!");
64
}
65
}
66
67
private static void checkDefaultDirectory() {
68
if (System.getSecurityManager() == null) {
69
throw new RuntimeException("Security manager is not set!");
70
}
71
72
File defaultDirectory = FileSystemView.getFileSystemView().
73
getDefaultDirectory();
74
if (defaultDirectory != null) {
75
throw new RuntimeException("File system default directory is null!");
76
}
77
}
78
private static volatile JFileChooser fileChooser;
79
80
private static void checkFileChooser() throws Exception {
81
if (System.getSecurityManager() == null) {
82
throw new RuntimeException("Security manager is not set!");
83
}
84
85
Robot robot = new Robot();
86
robot.setAutoDelay(50);
87
88
SwingUtilities.invokeLater(new Runnable() {
89
90
public void run() {
91
fileChooser = new JFileChooser();
92
fileChooser.showOpenDialog(null);
93
fileChooserIsShown = true;
94
System.out.println("Start file chooser: " + fileChooserIsShown);
95
}
96
});
97
98
long time = System.currentTimeMillis();
99
while (fileChooser == null) {
100
if (System.currentTimeMillis() - time >= 10000) {
101
throw new RuntimeException("FileChoser is not shown!");
102
}
103
Thread.sleep(500);
104
}
105
106
Thread.sleep(500);
107
robot.keyPress(KeyEvent.VK_ESCAPE);
108
robot.keyRelease(KeyEvent.VK_ESCAPE);
109
System.exit(0);
110
}
111
112
private static void generatePolicyFile() throws Exception {
113
if (System.getSecurityManager() != null) {
114
throw new RuntimeException("Security manager should be null!");
115
}
116
117
if (!OSInfo.getOSType().equals(OSInfo.OSType.WINDOWS)) {
118
return;
119
}
120
121
File defaultDirectory = FileSystemView.getFileSystemView().
122
getDefaultDirectory();
123
124
if (defaultDirectory == null) {
125
throw new RuntimeException("Default directory is null!");
126
}
127
128
File policyFile = new File(POLICY_FILE);
129
if (!policyFile.exists()) {
130
policyFile.createNewFile();
131
}
132
133
try (PrintWriter writer = new PrintWriter(policyFile, "UTF-8")) {
134
writer.println("grant {");
135
String documents = defaultDirectory.getCanonicalPath();
136
documents = documents.replace('\\', '/');
137
// Documents permission
138
writer.print(" permission java.io.FilePermission");
139
writer.print(" \"" + documents + "\",");
140
writer.println(" \"read\";");
141
// Desktop permission
142
writer.print(" permission java.io.FilePermission");
143
writer.print(" \"" + documents.replace("Documents", "Desktop") + "\",");
144
writer.println(" \"read\";");
145
// robot permission // "java.awt.AWTPermission" "createRobot"
146
writer.print(" permission java.awt.AWTPermission");
147
writer.println(" \"createRobot\";");
148
writer.println("};");
149
}
150
151
performTest();
152
}
153
154
private static void performTest() throws Exception {
155
String javaPath = System.getProperty("java.home", "");
156
String command = javaPath + File.separator + "bin" + File.separator + "java"
157
+ " -Djava.security.manager -Djava.security.policy=" + POLICY_FILE
158
+ " bug8062561 CHECK_FILE_CHOOSER";
159
System.out.println(command);
160
boolean processExit = false;
161
162
Process process = Runtime.getRuntime().exec(command);
163
164
try {
165
processExit = process.waitFor(20, TimeUnit.SECONDS);
166
} catch (IllegalThreadStateException e) {
167
throw new RuntimeException(e);
168
}
169
System.out.println("[RESULT] : "
170
+ "The sub process has cleanly exited : PASS");
171
172
InputStream errorStream = process.getErrorStream();
173
System.out.println("========= Child process stderr ========");
174
boolean exception = dumpStream(errorStream);
175
if (exception) {
176
throw new RuntimeException("[RESULT] :"
177
+ " Exception in child process : FAIL");
178
}
179
System.out.println("=======================================");
180
181
InputStream processInputStream = process.getInputStream();
182
System.out.println("========= Child process output ========");
183
dumpStream(processInputStream);
184
System.out.println("=======================================");
185
186
if (!processExit) {
187
process.destroy();
188
throw new RuntimeException("[RESULT] : "
189
+ "The sub process has not exited : FAIL");
190
}
191
}
192
193
public static boolean dumpStream(InputStream in) throws IOException {
194
String tempString;
195
int count = in.available();
196
boolean exception = false;
197
while (count > 0) {
198
byte[] b = new byte[count];
199
in.read(b);
200
tempString = new String(b);
201
if (!exception) {
202
exception = tempString.indexOf("Exception") != -1;
203
}
204
System.out.println(tempString);
205
count = in.available();
206
}
207
208
return exception;
209
}
210
}
211
212