Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/8062561/bug8062561.java
38854 views
/*1* Copyright (c) 2014, 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*/2223import java.awt.Robot;24import java.awt.event.KeyEvent;25import java.io.File;26import java.io.IOException;27import java.io.InputStream;28import java.io.PrintWriter;29import java.util.concurrent.TimeUnit;30import javax.swing.JFileChooser;31import javax.swing.SwingUtilities;32import javax.swing.filechooser.FileSystemView;33import sun.awt.OSInfo;3435/**36* @test37* @bug 806256138* @summary File system view returns null default directory39* @run main/othervm bug8062561 GENERATE_POLICY40* @run main/othervm/policy=security.policy bug8062561 CHECK_DEFAULT_DIR run41*/42public class bug8062561 {4344private static final String POLICY_FILE = "security2.policy";45private static volatile boolean fileChooserIsShown = false;4647public static void main(String[] args) throws Exception {4849String test = args[0];5051switch (test) {52case "GENERATE_POLICY":53generatePolicyFile();54break;55case "CHECK_DEFAULT_DIR":56checkDefaultDirectory();57break;58case "CHECK_FILE_CHOOSER":59checkFileChooser();60break;61default:62throw new RuntimeException("Wrong argument!");63}64}6566private static void checkDefaultDirectory() {67if (System.getSecurityManager() == null) {68throw new RuntimeException("Security manager is not set!");69}7071File defaultDirectory = FileSystemView.getFileSystemView().72getDefaultDirectory();73if (defaultDirectory != null) {74throw new RuntimeException("File system default directory is null!");75}76}77private static volatile JFileChooser fileChooser;7879private static void checkFileChooser() throws Exception {80if (System.getSecurityManager() == null) {81throw new RuntimeException("Security manager is not set!");82}8384Robot robot = new Robot();85robot.setAutoDelay(50);8687SwingUtilities.invokeLater(new Runnable() {8889public void run() {90fileChooser = new JFileChooser();91fileChooser.showOpenDialog(null);92fileChooserIsShown = true;93System.out.println("Start file chooser: " + fileChooserIsShown);94}95});9697long time = System.currentTimeMillis();98while (fileChooser == null) {99if (System.currentTimeMillis() - time >= 10000) {100throw new RuntimeException("FileChoser is not shown!");101}102Thread.sleep(500);103}104105Thread.sleep(500);106robot.keyPress(KeyEvent.VK_ESCAPE);107robot.keyRelease(KeyEvent.VK_ESCAPE);108System.exit(0);109}110111private static void generatePolicyFile() throws Exception {112if (System.getSecurityManager() != null) {113throw new RuntimeException("Security manager should be null!");114}115116if (!OSInfo.getOSType().equals(OSInfo.OSType.WINDOWS)) {117return;118}119120File defaultDirectory = FileSystemView.getFileSystemView().121getDefaultDirectory();122123if (defaultDirectory == null) {124throw new RuntimeException("Default directory is null!");125}126127File policyFile = new File(POLICY_FILE);128if (!policyFile.exists()) {129policyFile.createNewFile();130}131132try (PrintWriter writer = new PrintWriter(policyFile, "UTF-8")) {133writer.println("grant {");134String documents = defaultDirectory.getCanonicalPath();135documents = documents.replace('\\', '/');136// Documents permission137writer.print(" permission java.io.FilePermission");138writer.print(" \"" + documents + "\",");139writer.println(" \"read\";");140// Desktop permission141writer.print(" permission java.io.FilePermission");142writer.print(" \"" + documents.replace("Documents", "Desktop") + "\",");143writer.println(" \"read\";");144// robot permission // "java.awt.AWTPermission" "createRobot"145writer.print(" permission java.awt.AWTPermission");146writer.println(" \"createRobot\";");147writer.println("};");148}149150performTest();151}152153private static void performTest() throws Exception {154String javaPath = System.getProperty("java.home", "");155String command = javaPath + File.separator + "bin" + File.separator + "java"156+ " -Djava.security.manager -Djava.security.policy=" + POLICY_FILE157+ " bug8062561 CHECK_FILE_CHOOSER";158System.out.println(command);159boolean processExit = false;160161Process process = Runtime.getRuntime().exec(command);162163try {164processExit = process.waitFor(20, TimeUnit.SECONDS);165} catch (IllegalThreadStateException e) {166throw new RuntimeException(e);167}168System.out.println("[RESULT] : "169+ "The sub process has cleanly exited : PASS");170171InputStream errorStream = process.getErrorStream();172System.out.println("========= Child process stderr ========");173boolean exception = dumpStream(errorStream);174if (exception) {175throw new RuntimeException("[RESULT] :"176+ " Exception in child process : FAIL");177}178System.out.println("=======================================");179180InputStream processInputStream = process.getInputStream();181System.out.println("========= Child process output ========");182dumpStream(processInputStream);183System.out.println("=======================================");184185if (!processExit) {186process.destroy();187throw new RuntimeException("[RESULT] : "188+ "The sub process has not exited : FAIL");189}190}191192public static boolean dumpStream(InputStream in) throws IOException {193String tempString;194int count = in.available();195boolean exception = false;196while (count > 0) {197byte[] b = new byte[count];198in.read(b);199tempString = new String(b);200if (!exception) {201exception = tempString.indexOf("Exception") != -1;202}203System.out.println(tempString);204count = in.available();205}206207return exception;208}209}210211212