Path: blob/master/test/jdk/javax/swing/JFileChooser/8041694/bug8041694.java
41154 views
/*1* Copyright (c) 2016, 2019, 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*/2223/*24* @test25* @key headful26* @bug 804169427* @summary JFileChooser removes trailing spaces in the selected directory name28* @author Anton Litvinov29* @library /test/lib30* @build jdk.test.lib.Platform31* @run main bug804169432*/3334import java.awt.AWTException;35import java.awt.Robot;36import java.awt.event.KeyEvent;37import java.io.File;38import java.io.IOException;39import java.nio.file.Files;40import java.util.concurrent.CountDownLatch;41import javax.swing.JFileChooser;42import javax.swing.SwingUtilities;43import javax.swing.UIManager;44import javax.swing.UnsupportedLookAndFeelException;45import javax.swing.plaf.metal.MetalLookAndFeel;4647import jdk.test.lib.Platform;4849public class bug8041694 {50private static volatile File dir1;51private static File dir2;52private static volatile File selectedDir;5354private static void runTest() {55try {56// Set Metal L&F to make the test compatible with OS X.57UIManager.setLookAndFeel(new MetalLookAndFeel());58Robot robot = new Robot();59robot.setAutoDelay(100);6061dir1 = Files.createTempDirectory("bug8041694").toFile();62if (Platform.isWindows()) {63dir2 = new File(String.format(64"\\\\?\\%s\\d ", dir1.getAbsolutePath().replace('/', '\\')));65} else {66dir2 = new File(dir1.getAbsolutePath() + File.separator + "d ");67}68dir2.mkdir();6970final CountDownLatch fChooserClosedSignal = new CountDownLatch(1);71SwingUtilities.invokeLater(new Runnable() {72@Override73public void run() {74try {75JFileChooser fChooser = new JFileChooser(dir1);76fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);77if (fChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {78selectedDir = fChooser.getSelectedFile();79}80} finally {81fChooserClosedSignal.countDown();82}83}84});8586robot.delay(1000);87robot.waitForIdle();88robot.keyPress(KeyEvent.VK_D);89robot.keyRelease(KeyEvent.VK_D);90robot.waitForIdle();91robot.keyPress(KeyEvent.VK_SPACE);92robot.keyRelease(KeyEvent.VK_SPACE);93robot.waitForIdle();94robot.keyPress(KeyEvent.VK_ENTER);95robot.keyRelease(KeyEvent.VK_ENTER);96robot.waitForIdle();9798fChooserClosedSignal.await();99if (selectedDir == null) {100throw new RuntimeException("No directory was selected in JFileChooser.");101}102System.out.println(String.format(103"The selected directory is '%s'.", selectedDir.getAbsolutePath()));104if (selectedDir.getName().equals("d")) {105throw new RuntimeException(106"JFileChooser removed trailing spaces in the selected directory name. " +107"Expected 'd ' got '" + selectedDir.getName() + "'.");108} else if (!selectedDir.getName().equals("d ")) {109throw new RuntimeException("The selected directory name is not "110+ "the expected 'd ' but '" + selectedDir.getName() + "'.");111}112} catch (UnsupportedLookAndFeelException | AWTException | IOException | InterruptedException e) {113throw new RuntimeException(e);114} finally {115if (dir2 != null) {116dir2.delete();117}118if (dir1 != null) {119dir1.delete();120}121}122}123124public static void main(String[] args) {125runTest();126}127}128129130