< prev index next >

test/jdk/javax/swing/JFileChooser/8062561/bug8062561.java

Print this page
rev 51542 : 8210039: move OSInfo to top level testlibrary
Reviewed-by: duke


   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 jdk.testlibrary.OSInfo;
  25 import java.awt.Robot;
  26 import java.awt.event.KeyEvent;
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.PrintWriter;
  31 import java.util.concurrent.TimeUnit;
  32 import javax.swing.JFileChooser;
  33 import javax.swing.SwingUtilities;
  34 import javax.swing.filechooser.FileSystemView;
  35 
  36 /**
  37  * @test
  38  * @bug 8062561
  39  * @key headful
  40  * @requires (os.family == "windows")
  41  * @summary File system view returns null default directory
  42  * @library ../../../../lib/testlibrary
  43  * @modules java.desktop/sun.awt
  44  * @build jdk.testlibrary.OSInfo
  45  * @run main/othervm bug8062561 GENERATE_POLICY
  46  * @run main/othervm/policy=security.policy bug8062561 CHECK_DEFAULT_DIR run
  47  */
  48 public class bug8062561 {
  49 
  50     private static final String POLICY_FILE = "security2.policy";
  51     private static volatile boolean fileChooserIsShown = false;
  52 
  53     public static void main(String[] args) throws Exception {
  54 
  55         String test = args[0];
  56 
  57         switch (test) {
  58             case "GENERATE_POLICY":
  59                 generatePolicyFile();
  60                 break;
  61             case "CHECK_DEFAULT_DIR":
  62                 checkDefaultDirectory();
  63                 break;
  64             case "CHECK_FILE_CHOOSER":


 102 
 103         long time = System.currentTimeMillis();
 104         while (fileChooser == null) {
 105             if (System.currentTimeMillis() - time >= 10000) {
 106                 throw new RuntimeException("FileChoser is not shown!");
 107             }
 108             Thread.sleep(500);
 109         }
 110 
 111         Thread.sleep(500);
 112         robot.keyPress(KeyEvent.VK_ESCAPE);
 113         robot.keyRelease(KeyEvent.VK_ESCAPE);
 114         System.exit(0);
 115     }
 116 
 117     private static void generatePolicyFile() throws Exception {
 118         if (System.getSecurityManager() != null) {
 119             throw new RuntimeException("Security manager should be null!");
 120         }
 121 
 122         if (!OSInfo.getOSType().equals(OSInfo.OSType.WINDOWS)) {
 123             return;
 124         }
 125 
 126         File defaultDirectory = FileSystemView.getFileSystemView().
 127                 getDefaultDirectory();
 128 
 129         if (defaultDirectory == null) {
 130             throw new RuntimeException("Default directory is null!");
 131         }
 132 
 133         File policyFile = new File(POLICY_FILE);
 134         if (!policyFile.exists()) {
 135             policyFile.createNewFile();
 136         }
 137 
 138         try (PrintWriter writer = new PrintWriter(policyFile, "UTF-8")) {
 139             writer.println("grant {");
 140             String documents = defaultDirectory.getCanonicalPath();
 141             documents = documents.replace('\\', '/');
 142             // Documents permission




   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 jdk.test.lib.Platform;
  25 import java.awt.Robot;
  26 import java.awt.event.KeyEvent;
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.PrintWriter;
  31 import java.util.concurrent.TimeUnit;
  32 import javax.swing.JFileChooser;
  33 import javax.swing.SwingUtilities;
  34 import javax.swing.filechooser.FileSystemView;
  35 
  36 /**
  37  * @test
  38  * @bug 8062561
  39  * @key headful
  40  * @requires (os.family == "windows")
  41  * @summary File system view returns null default directory
  42  * @library /test/lib
  43  * @modules java.desktop/sun.awt
  44  * @build jdk.test.lib.Platform
  45  * @run main/othervm bug8062561 GENERATE_POLICY
  46  * @run main/othervm/policy=security.policy bug8062561 CHECK_DEFAULT_DIR run
  47  */
  48 public class bug8062561 {
  49 
  50     private static final String POLICY_FILE = "security2.policy";
  51     private static volatile boolean fileChooserIsShown = false;
  52 
  53     public static void main(String[] args) throws Exception {
  54 
  55         String test = args[0];
  56 
  57         switch (test) {
  58             case "GENERATE_POLICY":
  59                 generatePolicyFile();
  60                 break;
  61             case "CHECK_DEFAULT_DIR":
  62                 checkDefaultDirectory();
  63                 break;
  64             case "CHECK_FILE_CHOOSER":


 102 
 103         long time = System.currentTimeMillis();
 104         while (fileChooser == null) {
 105             if (System.currentTimeMillis() - time >= 10000) {
 106                 throw new RuntimeException("FileChoser is not shown!");
 107             }
 108             Thread.sleep(500);
 109         }
 110 
 111         Thread.sleep(500);
 112         robot.keyPress(KeyEvent.VK_ESCAPE);
 113         robot.keyRelease(KeyEvent.VK_ESCAPE);
 114         System.exit(0);
 115     }
 116 
 117     private static void generatePolicyFile() throws Exception {
 118         if (System.getSecurityManager() != null) {
 119             throw new RuntimeException("Security manager should be null!");
 120         }
 121 
 122         if (!Platform.isWindows()) {
 123             return;
 124         }
 125 
 126         File defaultDirectory = FileSystemView.getFileSystemView().
 127                 getDefaultDirectory();
 128 
 129         if (defaultDirectory == null) {
 130             throw new RuntimeException("Default directory is null!");
 131         }
 132 
 133         File policyFile = new File(POLICY_FILE);
 134         if (!policyFile.exists()) {
 135             policyFile.createNewFile();
 136         }
 137 
 138         try (PrintWriter writer = new PrintWriter(policyFile, "UTF-8")) {
 139             writer.println("grant {");
 140             String documents = defaultDirectory.getCanonicalPath();
 141             documents = documents.replace('\\', '/');
 142             // Documents permission


< prev index next >