author | serb |
Wed, 21 Feb 2018 12:49:00 -0800 | |
changeset 49096 | eaef201ec301 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
16469
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
1 |
/* |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
4 |
* |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
8 |
* |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
13 |
* accompanied this code). |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
14 |
* |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
18 |
* |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
21 |
* questions. |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
22 |
*/ |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
23 |
|
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
24 |
/* @test |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
25 |
@bug 6550588 |
49096
eaef201ec301
8198333: ProblemList should be updated for headless mode
serb
parents:
47216
diff
changeset
|
26 |
@key headful |
eaef201ec301
8198333: ProblemList should be updated for headless mode
serb
parents:
47216
diff
changeset
|
27 |
@requires (os.family == "windows") |
16469
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
28 |
@summary java.awt.Desktop cannot open file with Windows UNC filename |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
29 |
@author Anton Litvinov |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
30 |
*/ |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
31 |
|
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
32 |
import java.awt.*; |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
33 |
import java.awt.event.*; |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
34 |
import java.io.*; |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
35 |
|
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
36 |
public class OpenByUNCPathNameTest { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
37 |
private static boolean validatePlatform() { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
38 |
String osName = System.getProperty("os.name"); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
39 |
if (osName == null) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
40 |
throw new RuntimeException("Name of the current OS could not be retrieved."); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
41 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
42 |
return osName.startsWith("Windows"); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
43 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
44 |
|
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
45 |
private static void openFile() throws IOException { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
46 |
if (!Desktop.isDesktopSupported()) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
47 |
System.out.println("java.awt.Desktop is not supported on this platform."); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
48 |
} else { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
49 |
Desktop desktop = Desktop.getDesktop(); |
23243
43e5bc27297f
8029263: user's default browser can not launch after we click the button, and there is an IOException shown in the log text (java.io.IOException)
azvegint
parents:
16469
diff
changeset
|
50 |
if (!desktop.isSupported(Desktop.Action.OPEN)) { |
43e5bc27297f
8029263: user's default browser can not launch after we click the button, and there is an IOException shown in the log text (java.io.IOException)
azvegint
parents:
16469
diff
changeset
|
51 |
System.out.println("Action.OPEN is not supported on this platform."); |
43e5bc27297f
8029263: user's default browser can not launch after we click the button, and there is an IOException shown in the log text (java.io.IOException)
azvegint
parents:
16469
diff
changeset
|
52 |
return; |
43e5bc27297f
8029263: user's default browser can not launch after we click the button, and there is an IOException shown in the log text (java.io.IOException)
azvegint
parents:
16469
diff
changeset
|
53 |
} |
16469
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
54 |
File file = File.createTempFile("Read Me File", ".txt"); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
55 |
try { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
56 |
// Test opening of the file with Windows local file path. |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
57 |
desktop.open(file); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
58 |
Robot robot = null; |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
59 |
try { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
60 |
Thread.sleep(5000); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
61 |
robot = new Robot(); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
62 |
} catch (Exception e) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
63 |
e.printStackTrace(); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
64 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
65 |
pressAltF4Keys(robot); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
66 |
|
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
67 |
// Test opening of the file with Windows UNC pathname. |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
68 |
String uncFilePath = "\\\\127.0.0.1\\" + file.getAbsolutePath().replace(':', '$'); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
69 |
File uncFile = new File(uncFilePath); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
70 |
if (!uncFile.exists()) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
71 |
throw new RuntimeException(String.format( |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
72 |
"File with UNC pathname '%s' does not exist.", uncFilePath)); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
73 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
74 |
desktop.open(uncFile); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
75 |
try { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
76 |
Thread.sleep(5000); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
77 |
} catch (InterruptedException ie) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
78 |
ie.printStackTrace(); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
79 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
80 |
pressAltF4Keys(robot); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
81 |
} finally { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
82 |
file.delete(); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
83 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
84 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
85 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
86 |
|
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
87 |
private static void pressAltF4Keys(Robot robot) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
88 |
if (robot != null) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
89 |
robot.keyPress(KeyEvent.VK_ALT); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
90 |
robot.keyPress(KeyEvent.VK_F4); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
91 |
robot.delay(50); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
92 |
robot.keyRelease(KeyEvent.VK_F4); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
93 |
robot.keyRelease(KeyEvent.VK_ALT); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
94 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
95 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
96 |
|
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
97 |
public static void main(String[] args) throws IOException { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
98 |
if (!validatePlatform()) { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
99 |
System.out.println("This test is only for MS Windows OS."); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
100 |
} else { |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
101 |
openFile(); |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
102 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
103 |
} |
169f9bf9b5d6
6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff
changeset
|
104 |
} |