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