Path: blob/master/test/jdk/javax/swing/JFileChooser/GodMode/JFileChooserTest.java
41154 views
/*1* Copyright (c) 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 817901426* @requires (os.family == "Windows")27* @summary Check if JFileChooser crashes with GodMode Directory.28* @run main/manual JFileChooserTest29*/30import java.awt.Color;31import java.awt.GridBagConstraints;32import java.awt.GridBagLayout;33import java.util.concurrent.CountDownLatch;34import javax.swing.JPanel;35import javax.swing.JTextArea;36import javax.swing.SwingUtilities;37import javax.swing.JButton;38import javax.swing.JFrame;39import java.awt.event.ActionEvent;40import java.awt.event.ActionListener;41import java.util.concurrent.TimeUnit;42import javax.swing.JFileChooser;43import javax.swing.UIManager;4445public class JFileChooserTest {4647public static void main(String args[]) throws Exception {48final CountDownLatch latch = new CountDownLatch(1);49TestUI test = new TestUI(latch);50SwingUtilities.invokeAndWait(() -> {51try {52test.createUI();53} catch (Exception ex) {54throw new RuntimeException("Exception while creating UI");55}56});5758boolean status = latch.await(5, TimeUnit.MINUTES);5960if (!status) {61System.out.println("Test timed out.");62}6364SwingUtilities.invokeAndWait(() -> {65try {66test.disposeUI();67} catch (Exception ex) {68throw new RuntimeException("Exception while disposing UI");69}70});7172if (test.testResult == false) {73throw new RuntimeException("Test Failed.");74}75}76}7778class TestUI {7980private static JFrame mainFrame;81private static JPanel mainControlPanel;8283private static JTextArea instructionTextArea;8485private static JPanel resultButtonPanel;86private static JButton passButton;87private static JButton failButton;8889private static GridBagLayout layout;90private final CountDownLatch latch;91public boolean testResult = false;9293public TestUI(CountDownLatch latch) throws Exception {94this.latch = latch;95}9697public final void createUI() throws Exception {98UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());99mainFrame = new JFrame("JFileChooserTest");100101layout = new GridBagLayout();102mainControlPanel = new JPanel(layout);103resultButtonPanel = new JPanel(layout);104105GridBagConstraints gbc = new GridBagConstraints();106107// Create Test instructions108String instructions109= "INSTRUCTIONS:"110+ "\n 1. Create a new folder on the desktop."111+ "\n 2. Rename the folder exactly as given below: "112+ "\n GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} "113+ "\n 3. Click on Launch Button. "114+ "\n Check if JFileChooser is launched successfully. "115+ "\n If yes, close the JFileChooser and click Pass, "116+ "\n else Fail. "117+ "\n 4. Delete the GodMode folder.";118119instructionTextArea = new JTextArea();120instructionTextArea.setText(instructions);121instructionTextArea.setEnabled(false);122instructionTextArea.setDisabledTextColor(Color.black);123instructionTextArea.setBackground(Color.white);124125gbc.gridx = 0;126gbc.gridy = 0;127gbc.fill = GridBagConstraints.HORIZONTAL;128mainControlPanel.add(instructionTextArea, gbc);129JButton launchButton = new JButton("Launch");130launchButton.setActionCommand("Launch");131launchButton.addActionListener((ActionEvent e) -> {132JFileChooser fileChooser = new JFileChooser();133fileChooser.showOpenDialog(null);134}135);136137gbc.gridx = 0;138gbc.gridy = 1;139mainControlPanel.add(launchButton, gbc);140141passButton = new JButton("Pass");142passButton.setActionCommand("Pass");143passButton.addActionListener((ActionEvent e) -> {144testResult = true;145mainFrame.dispose();146latch.countDown();147148});149failButton = new JButton("Fail");150failButton.setActionCommand("Fail");151failButton.addActionListener(new ActionListener() {152@Override153public void actionPerformed(ActionEvent e) {154testResult = false;155mainFrame.dispose();156latch.countDown();157}158});159gbc.gridx = 0;160gbc.gridy = 0;161resultButtonPanel.add(passButton, gbc);162gbc.gridx = 1;163gbc.gridy = 0;164resultButtonPanel.add(failButton, gbc);165166gbc.gridx = 0;167gbc.gridy = 2;168mainControlPanel.add(resultButtonPanel, gbc);169170mainFrame.add(mainControlPanel);171mainFrame.pack();172mainFrame.setVisible(true);173}174175public void disposeUI() {176mainFrame.setVisible(false);177mainFrame.dispose();178}179}180181182