1 /*
   2  * Copyright (c) 2010, 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 /*
  25    bug 4150029 8006087
  26    summary BackSpace keyboard button does not lead to parent directory
  27    author Oleg Mokhovikov
  28 */
  29 
  30 import jdk.test.lib.Platform;
  31 
  32 import javax.swing.*;
  33 import java.io.File;
  34 import java.io.IOException;
  35 
  36 public class bug4150029 extends JApplet {
  37     private boolean res;
  38 
  39     public void init() {
  40         if (Platform.isOSX()) {
  41             try {
  42                 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  43             } catch (Exception e) {
  44                 throw new RuntimeException(e);
  45             }
  46         }
  47 
  48         String tmpDir = System.getProperty("java.io.tmpdir");
  49 
  50         if (tmpDir.length() == 0) {//'java.io.tmpdir' isn't guaranteed to be defined
  51             tmpDir = System.getProperty("user.home");
  52         }
  53 
  54         System.out.println("Temp directory: " + tmpDir);
  55 
  56         File testDir = new File(tmpDir, "testDir");
  57 
  58         testDir.mkdir();
  59 
  60         File subDir = new File(testDir, "subDir");
  61 
  62         subDir.mkdir();
  63 
  64         System.out.println("Created directory: " + testDir);
  65         System.out.println("Created sub-directory: " + subDir);
  66 
  67         JFileChooser fileChooser = new JFileChooser(testDir);
  68 
  69         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  70 
  71         try {
  72             res = fileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION ||
  73                     testDir.getCanonicalPath().equals(fileChooser.getSelectedFile().getCanonicalPath());
  74         } catch (IOException e) {
  75             res = false;
  76 
  77             e.printStackTrace();
  78         }
  79 
  80         try {
  81             subDir.delete();
  82             testDir.delete();
  83         } catch (SecurityException e) {
  84             e.printStackTrace();
  85         }
  86     }
  87 
  88     public void destroy() {
  89         if (!res) {
  90             throw new RuntimeException("BackSpace keyboard button does not lead to parent directory");
  91         }
  92     }
  93 }