Path: blob/master/test/jdk/javax/swing/JFileChooser/8067660/FileChooserTest.java
41154 views
/*1* Copyright (c) 2015, 2017, 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* @bug 8067660 817810626* @summary JFileChooser create new folder fails silently27* @requires (os.family == "windows")28* @run main/manual FileChooserTest29*/30import java.awt.Panel;31import java.awt.TextArea;32import java.awt.event.ActionEvent;33import java.awt.event.ActionListener;34import javax.swing.JButton;35import javax.swing.JDialog;36import javax.swing.JFileChooser;37import javax.swing.JFrame;38import javax.swing.SwingUtilities;3940public class FileChooserTest {4142private static boolean theTestPassed;43private static boolean testGeneratedInterrupt;44private static Thread mainThread;45private static int sleepTime = 30000;46public static JFileChooser fileChooser;4748private static void init() throws Exception {4950SwingUtilities.invokeAndWait(new Runnable() {51@Override52public void run() {53String[] instructions54= {55"1) Create a folder with read only permissions by "56+ "changing security permission through Security tab"57+ "under Folder->Properties menu to deny write permission"58+ " to the newly created folder",59"2) Click on run test button.It will open a open dialog"60+ " Navigate to the newly created read only folder",61"3) Click on the create new folder button in open dialog",62"4) If an error message does not pops up"63+ "test failed otherwise passed.",64"5) Pressing Pass/Fail button will mark test as "65+ "pass/fail and will shutdown JVM",66"6) Newly created folder permissions can now be restored"67+ " back to default",68};6970Sysout.createDialogWithInstructions(instructions);71Sysout.printInstructions(instructions);72}73});74}7576/**77* ***************************************************78* Standard Test Machinery Section DO NOT modify anything in this section --79* it's a standard chunk of code which has all of the synchronisation80* necessary for the test harness. By keeping it the same in all tests, it81* is easier to read and understand someone else's test, as well as insuring82* that all tests behave correctly with the test harness. There is a section83* following this for test-defined classes84*/85public static void main(String args[]) throws Exception {8687mainThread = Thread.currentThread();88try {89init();90} catch (Exception ex) {91return;92}93try {94mainThread.sleep(sleepTime);95} catch (InterruptedException ex) {96Sysout.dispose();97if (!theTestPassed && testGeneratedInterrupt) {98throw new RuntimeException("Test Failed");99}100}101if (!testGeneratedInterrupt) {102Sysout.dispose();103throw new RuntimeException("Test Failed");104}105}106107public static synchronized void pass() {108theTestPassed = true;109testGeneratedInterrupt = true;110mainThread.interrupt();111}112113public static synchronized void fail() {114theTestPassed = false;115testGeneratedInterrupt = true;116mainThread.interrupt();117}118}119120/**121* This is part of the standard test machinery. It creates a dialog (with the122* instructions), and is the interface for sending text messages to the user. To123* print the instructions, send an array of strings to Sysout.createDialog124* WithInstructions method. Put one line of instructions per array entry. To125* display a message for the tester to see, simply call Sysout.println with the126* string to be displayed. This mimics System.out.println but works within the127* test harness as well as standalone.128*/129class Sysout {130131private static TestDialog dialog;132private static JFrame frame;133134public static void createDialogWithInstructions(String[] instructions) {135frame = new JFrame();136dialog = new TestDialog(frame, "Instructions");137dialog.printInstructions(instructions);138dialog.setVisible(true);139println("Any messages for the tester will display here.");140}141142public static void printInstructions(String[] instructions) {143dialog.printInstructions(instructions);144}145146public static void println(String messageIn) {147dialog.displayMessage(messageIn);148}149150public static void dispose() {151Sysout.println("Shutting down the Java process..");152if(FileChooserTest.fileChooser != null) {153FileChooserTest.fileChooser.cancelSelection();154}155frame.dispose();156dialog.dispose();157}158}159160/**161* This is part of the standard test machinery. It provides a place for the test162* instructions to be displayed, and a place for interactive messages to the163* user to be displayed. To have the test instructions displayed, see Sysout. To164* have a message to the user be displayed, see Sysout. Do not call anything in165* this dialog directly.166*/167class TestDialog extends JDialog {168169private TextArea instructionsText;170private TextArea messageText;171private int maxStringLength = 80;172private Panel buttonP = new Panel();173private JButton run = new JButton("Run");174private JButton passB = new JButton("Pass");175private JButton failB = new JButton("Fail");176177public TestDialog(JFrame frame, String name) {178super(frame, name);179frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);180int scrollBoth = TextArea.SCROLLBARS_BOTH;181instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);182add("North", instructionsText);183184messageText = new TextArea("", 5, maxStringLength, scrollBoth);185add("Center", messageText);186187buttonP.add("East", run);188buttonP.add("East", passB);189buttonP.add("West", failB);190passB.setEnabled(false);191failB.setEnabled(false);192add("South", buttonP);193194run.addActionListener(new ActionListener() {195196@Override197public void actionPerformed(ActionEvent ae) {198FileChooserTest.fileChooser = new JFileChooser();199FileChooserTest.fileChooser.showOpenDialog(null);200passB.setEnabled(true);201failB.setEnabled(true);202}203});204205passB.addActionListener(new ActionListener() {206207@Override208public void actionPerformed(ActionEvent ae) {209FileChooserTest.pass();210}211});212213failB.addActionListener(new ActionListener() {214215@Override216public void actionPerformed(ActionEvent ae) {217FileChooserTest.fail();218}219});220pack();221222setVisible(true);223}224225public void printInstructions(String[] instructions) {226instructionsText.setText("");227228String printStr, remainingStr;229for (String instruction : instructions) {230remainingStr = instruction;231while (remainingStr.length() > 0) {232if (remainingStr.length() >= maxStringLength) {233int posOfSpace = remainingStr.234lastIndexOf(' ', maxStringLength - 1);235236if (posOfSpace <= 0) {237posOfSpace = maxStringLength - 1;238}239240printStr = remainingStr.substring(0, posOfSpace + 1);241remainingStr = remainingStr.substring(posOfSpace + 1);242} else {243printStr = remainingStr;244remainingStr = "";245}246instructionsText.append(printStr + "\n");247}248}249250}251252public void displayMessage(String messageIn) {253messageText.append(messageIn + "\n");254}255}256257258