|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
25 | 25 | @bug 6550588
|
26 | 26 | @summary java.awt.Desktop cannot open file with Windows UNC filename
|
27 | 27 | @author Anton Litvinov
|
| 28 | + @run main/manual OpenByUNCPathNameTest |
28 | 29 | */
|
29 | 30 |
|
30 |
| -import java.awt.*; |
31 |
| -import java.awt.event.*; |
32 |
| -import java.io.*; |
| 31 | +import javax.swing.JButton; |
| 32 | +import javax.swing.JFrame; |
| 33 | +import javax.swing.JPanel; |
| 34 | +import javax.swing.JTextArea; |
| 35 | +import javax.swing.SwingUtilities; |
| 36 | +import javax.swing.WindowConstants; |
| 37 | +import java.awt.BorderLayout; |
| 38 | +import java.awt.Desktop; |
| 39 | +import java.awt.FlowLayout; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.File; |
| 42 | +import java.util.concurrent.CountDownLatch; |
| 43 | +import java.util.concurrent.TimeUnit; |
33 | 44 |
|
34 | 45 | public class OpenByUNCPathNameTest {
|
| 46 | + private static volatile CountDownLatch countDownLatch; |
| 47 | + private static JFrame instructionFrame; |
| 48 | + private static JFrame testFrame; |
| 49 | + private static volatile boolean testPassed = false; |
| 50 | + private static File file; |
| 51 | + |
35 | 52 | private static boolean validatePlatform() {
|
36 | 53 | String osName = System.getProperty("os.name");
|
37 | 54 | if (osName == null) {
|
38 |
| - throw new RuntimeException("Name of the current OS could not be retrieved."); |
| 55 | + throw new RuntimeException("Name of the current OS could not be" + |
| 56 | + " retrieved."); |
39 | 57 | }
|
40 | 58 | return osName.startsWith("Windows");
|
41 | 59 | }
|
42 | 60 |
|
| 61 | + private static void createInstructionUI() { |
| 62 | + SwingUtilities.invokeLater(() -> { |
| 63 | + String instructions = |
| 64 | + "1. Make sure that disk C is shared \n" |
| 65 | + + "2. Click on openFileByLocalPath Button to test Test" |
| 66 | + + " opening of the file with Windows local file path\n" |
| 67 | + + "3. Check that the file is opened successfully\n" |
| 68 | + + "4. Close the file\n" |
| 69 | + + "5. Click on openFileByUNCPath Button to test Test" |
| 70 | + + " opening of the file with Windows UNC pathname\n" |
| 71 | + + "6. Check that the file is opened successfully\n" |
| 72 | + + "7. Close the file\n" |
| 73 | + + "8. If all the conditions are met then press PASS else " |
| 74 | + + "press FAIL"; |
| 75 | + instructionFrame = new JFrame("InstructionFrame"); |
| 76 | + JTextArea textArea = new JTextArea(instructions); |
| 77 | + textArea.setEditable(false); |
| 78 | + final JButton passButton = new JButton("PASS"); |
| 79 | + passButton.addActionListener((e) -> { |
| 80 | + testPassed = true; |
| 81 | + instructionFrame.dispose(); |
| 82 | + testFrame.dispose(); |
| 83 | + file.delete(); |
| 84 | + countDownLatch.countDown(); |
| 85 | + }); |
| 86 | + final JButton failButton = new JButton("FAIL"); |
| 87 | + failButton.addActionListener((e) -> { |
| 88 | + instructionFrame.dispose(); |
| 89 | + testFrame.dispose(); |
| 90 | + file.delete(); |
| 91 | + countDownLatch.countDown(); |
| 92 | + }); |
| 93 | + |
| 94 | + |
| 95 | + JPanel mainPanel = new JPanel(new BorderLayout()); |
| 96 | + mainPanel.add(textArea, BorderLayout.CENTER); |
| 97 | + |
| 98 | + JPanel buttonPanel = new JPanel(new FlowLayout()); |
| 99 | + buttonPanel.add(passButton); |
| 100 | + buttonPanel.add(failButton); |
| 101 | + mainPanel.add(buttonPanel, BorderLayout.SOUTH); |
| 102 | + instructionFrame.setDefaultCloseOperation( |
| 103 | + WindowConstants.DISPOSE_ON_CLOSE); |
| 104 | + instructionFrame.setBounds(0,0,500,500); |
| 105 | + instructionFrame.add(mainPanel); |
| 106 | + instructionFrame.pack(); |
| 107 | + instructionFrame.setVisible(true); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
43 | 111 | private static void openFile() throws IOException {
|
44 | 112 | if (!Desktop.isDesktopSupported()) {
|
45 |
| - System.out.println("java.awt.Desktop is not supported on this platform."); |
46 |
| - } else { |
47 |
| - Desktop desktop = Desktop.getDesktop(); |
48 |
| - if (!desktop.isSupported(Desktop.Action.OPEN)) { |
49 |
| - System.out.println("Action.OPEN is not supported on this platform."); |
50 |
| - return; |
51 |
| - } |
52 |
| - File file = File.createTempFile("Read Me File", ".txt"); |
| 113 | + System.out.println("java.awt.Desktop is not supported on this"+ |
| 114 | + " platform."); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + Desktop desktop = Desktop.getDesktop(); |
| 119 | + if (!desktop.isSupported(Desktop.Action.OPEN)) { |
| 120 | + System.out.println("Action.OPEN is not supported on this" + |
| 121 | + " platform."); |
| 122 | + return; |
| 123 | + } |
| 124 | + file = File.createTempFile("Read Me File", ".txt"); |
| 125 | + testFrame = new JFrame("Test Frame"); |
| 126 | + JPanel testButtonPanel = new JPanel(new FlowLayout()); |
| 127 | + final JButton openFileByLocalPathButton = new |
| 128 | + JButton("OpenFileByLocalPath"); |
| 129 | + final JButton openFileByUNCPathButton = new |
| 130 | + JButton("OpenFileByUNCPath"); |
| 131 | + openFileByLocalPathButton.addActionListener((e) -> { |
53 | 132 | try {
|
54 |
| - // Test opening of the file with Windows local file path. |
55 | 133 | desktop.open(file);
|
56 |
| - Robot robot = null; |
57 |
| - try { |
58 |
| - Thread.sleep(5000); |
59 |
| - robot = new Robot(); |
60 |
| - } catch (Exception e) { |
61 |
| - e.printStackTrace(); |
62 |
| - } |
63 |
| - pressAltF4Keys(robot); |
64 |
| - |
65 |
| - // Test opening of the file with Windows UNC pathname. |
66 |
| - String uncFilePath = "\\\\127.0.0.1\\" + file.getAbsolutePath().replace(':', '$'); |
67 |
| - File uncFile = new File(uncFilePath); |
68 |
| - if (!uncFile.exists()) { |
69 |
| - throw new RuntimeException(String.format( |
70 |
| - "File with UNC pathname '%s' does not exist.", uncFilePath)); |
71 |
| - } |
72 |
| - desktop.open(uncFile); |
73 |
| - try { |
74 |
| - Thread.sleep(5000); |
75 |
| - } catch (InterruptedException ie) { |
76 |
| - ie.printStackTrace(); |
77 |
| - } |
78 |
| - pressAltF4Keys(robot); |
79 |
| - } finally { |
80 |
| - file.delete(); |
| 134 | + } catch (IOException ioException) { |
81 | 135 | }
|
82 |
| - } |
83 |
| - } |
| 136 | + }); |
| 137 | + |
| 138 | + SwingUtilities.invokeLater(()->{ |
| 139 | + testButtonPanel.add(openFileByLocalPathButton); |
| 140 | + testButtonPanel.add(openFileByUNCPathButton); |
| 141 | + testFrame.setDefaultCloseOperation( |
| 142 | + WindowConstants.DISPOSE_ON_CLOSE); |
| 143 | + testFrame.setBounds(600,0,1000,200); |
| 144 | + testFrame.add(testButtonPanel); |
| 145 | + testFrame.pack(); |
| 146 | + testFrame.setVisible(true); |
| 147 | + }); |
84 | 148 |
|
85 |
| - private static void pressAltF4Keys(Robot robot) { |
86 |
| - if (robot != null) { |
87 |
| - robot.keyPress(KeyEvent.VK_ALT); |
88 |
| - robot.keyPress(KeyEvent.VK_F4); |
89 |
| - robot.delay(50); |
90 |
| - robot.keyRelease(KeyEvent.VK_F4); |
91 |
| - robot.keyRelease(KeyEvent.VK_ALT); |
| 149 | + // Test opening of the file with Windows UNC pathname. |
| 150 | + String uncFilePath = "\\\\127.0.0.1\\" + |
| 151 | + file.getAbsolutePath().replace(':', '$'); |
| 152 | + File uncFile = new File(uncFilePath); |
| 153 | + if (!uncFile.exists()) { |
| 154 | + throw new RuntimeException(String.format("File "+ |
| 155 | + "with UNC pathname '%s' does not exist.", uncFilePath)); |
92 | 156 | }
|
| 157 | + openFileByUNCPathButton.addActionListener((e) -> { |
| 158 | + try { |
| 159 | + desktop.open(uncFile); |
| 160 | + } catch (IOException ioException) { |
| 161 | + } |
| 162 | + }); |
93 | 163 | }
|
94 | 164 |
|
95 |
| - public static void main(String[] args) throws IOException { |
| 165 | + public static void main(String[] args) throws Exception { |
96 | 166 | if (!validatePlatform()) {
|
97 | 167 | System.out.println("This test is only for MS Windows OS.");
|
98 |
| - } else { |
99 |
| - openFile(); |
| 168 | + return; |
| 169 | + } |
| 170 | + countDownLatch = new CountDownLatch(1); |
| 171 | + OpenByUNCPathNameTest openByUNCPathNameTest = |
| 172 | + new OpenByUNCPathNameTest(); |
| 173 | + |
| 174 | + openByUNCPathNameTest.createInstructionUI(); |
| 175 | + openByUNCPathNameTest.openFile(); |
| 176 | + countDownLatch.await(15, TimeUnit.MINUTES); |
| 177 | + |
| 178 | + if(!testPassed) { |
| 179 | + throw new RuntimeException("Test failed!"); |
100 | 180 | }
|
101 | 181 | }
|
102 | 182 | }
|
0 commit comments