jdk/test/javax/swing/JFileChooser/GodMode/JFileChooserTest.java
changeset 45343 b0b8f2d6584e
equal deleted inserted replaced
45342:1164ed6b9b8e 45343:b0b8f2d6584e
       
     1 /*
       
     2  * Copyright (c) 2017, 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  * @bug 8179014
       
    27  * @requires (os.family == "Windows")
       
    28  * @summary Check if JFileChooser crashes with GodMode Directory.
       
    29  * @run main/manual JFileChooserTest
       
    30  */
       
    31 import java.awt.Color;
       
    32 import java.awt.GridBagConstraints;
       
    33 import java.awt.GridBagLayout;
       
    34 import java.util.concurrent.CountDownLatch;
       
    35 import javax.swing.JPanel;
       
    36 import javax.swing.JTextArea;
       
    37 import javax.swing.SwingUtilities;
       
    38 import javax.swing.JButton;
       
    39 import javax.swing.JFrame;
       
    40 import java.awt.event.ActionEvent;
       
    41 import java.awt.event.ActionListener;
       
    42 import java.util.concurrent.TimeUnit;
       
    43 import javax.swing.JFileChooser;
       
    44 import javax.swing.UIManager;
       
    45 
       
    46 public class JFileChooserTest {
       
    47 
       
    48     public static void main(String args[]) throws Exception {
       
    49         final CountDownLatch latch = new CountDownLatch(1);
       
    50         TestUI test = new TestUI(latch);
       
    51         SwingUtilities.invokeAndWait(() -> {
       
    52             try {
       
    53                 test.createUI();
       
    54             } catch (Exception ex) {
       
    55                 throw new RuntimeException("Exception while creating UI");
       
    56             }
       
    57         });
       
    58 
       
    59         boolean status = latch.await(5, TimeUnit.MINUTES);
       
    60 
       
    61         if (!status) {
       
    62             System.out.println("Test timed out.");
       
    63         }
       
    64 
       
    65         SwingUtilities.invokeAndWait(() -> {
       
    66             try {
       
    67                 test.disposeUI();
       
    68             } catch (Exception ex) {
       
    69                 throw new RuntimeException("Exception while disposing UI");
       
    70             }
       
    71         });
       
    72 
       
    73         if (test.testResult == false) {
       
    74             throw new RuntimeException("Test Failed.");
       
    75         }
       
    76     }
       
    77 }
       
    78 
       
    79 class TestUI {
       
    80 
       
    81     private static JFrame mainFrame;
       
    82     private static JPanel mainControlPanel;
       
    83 
       
    84     private static JTextArea instructionTextArea;
       
    85 
       
    86     private static JPanel resultButtonPanel;
       
    87     private static JButton passButton;
       
    88     private static JButton failButton;
       
    89 
       
    90     private static GridBagLayout layout;
       
    91     private final CountDownLatch latch;
       
    92     public boolean testResult = false;
       
    93 
       
    94     public TestUI(CountDownLatch latch) throws Exception {
       
    95         this.latch = latch;
       
    96     }
       
    97 
       
    98     public final void createUI() throws Exception {
       
    99         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       
   100         mainFrame = new JFrame("JFileChooserTest");
       
   101 
       
   102         layout = new GridBagLayout();
       
   103         mainControlPanel = new JPanel(layout);
       
   104         resultButtonPanel = new JPanel(layout);
       
   105 
       
   106         GridBagConstraints gbc = new GridBagConstraints();
       
   107 
       
   108         // Create Test instructions
       
   109         String instructions
       
   110                 = "INSTRUCTIONS:"
       
   111                 + "\n 1. Create a new folder on the desktop."
       
   112                 + "\n 2. Rename the folder exactly as given below: "
       
   113                 + "\n    GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} "
       
   114                 + "\n 3. Click on Launch Button. "
       
   115                 + "\n    Check if JFileChooser is launched successfully. "
       
   116                 + "\n    If yes, close the JFileChooser and click Pass, "
       
   117                 + "\n    else Fail. "
       
   118                 + "\n 4. Delete the GodMode folder.";
       
   119 
       
   120         instructionTextArea = new JTextArea();
       
   121         instructionTextArea.setText(instructions);
       
   122         instructionTextArea.setEnabled(false);
       
   123         instructionTextArea.setDisabledTextColor(Color.black);
       
   124         instructionTextArea.setBackground(Color.white);
       
   125 
       
   126         gbc.gridx = 0;
       
   127         gbc.gridy = 0;
       
   128         gbc.fill = GridBagConstraints.HORIZONTAL;
       
   129         mainControlPanel.add(instructionTextArea, gbc);
       
   130         JButton launchButton = new JButton("Launch");
       
   131         launchButton.setActionCommand("Launch");
       
   132         launchButton.addActionListener((ActionEvent e) -> {
       
   133             JFileChooser fileChooser = new JFileChooser();
       
   134             fileChooser.showOpenDialog(null);
       
   135         }
       
   136         );
       
   137 
       
   138         gbc.gridx = 0;
       
   139         gbc.gridy = 1;
       
   140         mainControlPanel.add(launchButton, gbc);
       
   141 
       
   142         passButton = new JButton("Pass");
       
   143         passButton.setActionCommand("Pass");
       
   144         passButton.addActionListener((ActionEvent e) -> {
       
   145             testResult = true;
       
   146             mainFrame.dispose();
       
   147             latch.countDown();
       
   148 
       
   149         });
       
   150         failButton = new JButton("Fail");
       
   151         failButton.setActionCommand("Fail");
       
   152         failButton.addActionListener(new ActionListener() {
       
   153             @Override
       
   154             public void actionPerformed(ActionEvent e) {
       
   155                 testResult = false;
       
   156                 mainFrame.dispose();
       
   157                 latch.countDown();
       
   158             }
       
   159         });
       
   160         gbc.gridx = 0;
       
   161         gbc.gridy = 0;
       
   162         resultButtonPanel.add(passButton, gbc);
       
   163         gbc.gridx = 1;
       
   164         gbc.gridy = 0;
       
   165         resultButtonPanel.add(failButton, gbc);
       
   166 
       
   167         gbc.gridx = 0;
       
   168         gbc.gridy = 2;
       
   169         mainControlPanel.add(resultButtonPanel, gbc);
       
   170 
       
   171         mainFrame.add(mainControlPanel);
       
   172         mainFrame.pack();
       
   173         mainFrame.setVisible(true);
       
   174     }
       
   175 
       
   176     public void disposeUI() {
       
   177         mainFrame.setVisible(false);
       
   178         mainFrame.dispose();
       
   179     }
       
   180 }