1 /* 2 * Copyright (c) 2016, 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 * @key headful 27 * @bug 8041694 28 * @summary JFileChooser removes trailing spaces in the selected directory name 29 * @author Anton Litvinov 30 * @library ../../../../lib/testlibrary 31 * @build jdk.testlibrary.OSInfo 32 * @run main bug8041694 33 */ 34 35 import java.awt.AWTException; 36 import java.awt.Robot; 37 import java.awt.event.KeyEvent; 38 import java.io.File; 39 import java.io.IOException; 40 import java.nio.file.Files; 41 import java.util.concurrent.CountDownLatch; 42 import javax.swing.JFileChooser; 43 import javax.swing.SwingUtilities; 44 import javax.swing.UIManager; 45 import javax.swing.UnsupportedLookAndFeelException; 46 import javax.swing.plaf.metal.MetalLookAndFeel; 47 48 import jdk.testlibrary.OSInfo; 49 50 public class bug8041694 { 51 private static volatile File dir1; 52 private static File dir2; 53 private static volatile File selectedDir; 54 55 private static void runTest() { 56 try { 57 // Set Metal L&F to make the test compatible with OS X. 58 UIManager.setLookAndFeel(new MetalLookAndFeel()); 59 Robot robot = new Robot(); 60 61 dir1 = Files.createTempDirectory("bug8041694").toFile(); 62 if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) { 63 dir2 = new File(String.format( 64 "\\\\?\\%s\\d ", dir1.getAbsolutePath().replace('/', '\\'))); 65 } else { 66 dir2 = new File(dir1.getAbsolutePath() + File.separator + "d "); 67 } 68 dir2.mkdir(); 69 70 final CountDownLatch fChooserClosedSignal = new CountDownLatch(1); 71 SwingUtilities.invokeLater(new Runnable() { 72 @Override 73 public void run() { 74 try { 75 JFileChooser fChooser = new JFileChooser(dir1); 76 fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 77 if (fChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 78 selectedDir = fChooser.getSelectedFile(); 79 } 80 } finally { 81 fChooserClosedSignal.countDown(); 82 } 83 } 84 }); 85 86 robot.setAutoDelay(50); 87 robot.delay(1000); 88 robot.waitForIdle(); 89 robot.keyPress(KeyEvent.VK_D); 90 robot.keyRelease(KeyEvent.VK_D); 91 robot.keyPress(KeyEvent.VK_SPACE); 92 robot.keyRelease(KeyEvent.VK_SPACE); 93 robot.keyPress(KeyEvent.VK_ENTER); 94 robot.keyRelease(KeyEvent.VK_ENTER); 95 96 fChooserClosedSignal.await(); 97 if (selectedDir == null) { 98 throw new RuntimeException("No directory was selected in JFileChooser."); 99 } 100 System.out.println(String.format( 101 "The selected directory is '%s'.", selectedDir.getAbsolutePath())); 102 if (selectedDir.getName().equals("d")) { 103 throw new RuntimeException( 104 "JFileChooser removed trailing spaces in the selected directory name."); 105 } else if (!selectedDir.getName().equals("d ")) { 106 throw new RuntimeException("The selected directory name is not the expected 'd '."); 107 } 108 } catch (UnsupportedLookAndFeelException | AWTException | IOException | InterruptedException e) { 109 throw new RuntimeException(e); 110 } finally { 111 if (dir2 != null) { 112 dir2.delete(); 113 } 114 if (dir1 != null) { 115 dir1.delete(); 116 } 117 } 118 } 119 120 public static void main(String[] args) { 121 runTest(); 122 } 123 }