test/jdk/java/awt/Desktop/OpenByUNCPathNameTest/OpenByUNCPathNameTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 23243 jdk/test/java/awt/Desktop/OpenByUNCPathNameTest/OpenByUNCPathNameTest.java@43e5bc27297f
child 49096 eaef201ec301
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    26
   @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
    27
   @author Anton Litvinov
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    28
*/
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    29
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    30
import java.awt.*;
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    31
import java.awt.event.*;
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    32
import java.io.*;
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    33
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    34
public class OpenByUNCPathNameTest {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    35
    private static boolean validatePlatform() {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    36
        String osName = System.getProperty("os.name");
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    37
        if (osName == null) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    38
            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
    39
        }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    40
        return osName.startsWith("Windows");
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
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    43
    private static void openFile() throws IOException {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    44
        if (!Desktop.isDesktopSupported()) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    45
            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
    46
        } else {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    47
            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
    48
            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
    49
                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
    50
                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
    51
            }
16469
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    52
            File file = File.createTempFile("Read Me File", ".txt");
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    53
            try {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    54
                // 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
    55
                desktop.open(file);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    56
                Robot robot = null;
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    57
                try {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    58
                    Thread.sleep(5000);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    59
                    robot = new Robot();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    60
                } catch (Exception e) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    61
                    e.printStackTrace();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    62
                }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    63
                pressAltF4Keys(robot);
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
                // 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
    66
                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
    67
                File uncFile = new File(uncFilePath);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    68
                if (!uncFile.exists()) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    69
                    throw new RuntimeException(String.format(
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    70
                        "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
    71
                }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    72
                desktop.open(uncFile);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    73
                try {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    74
                    Thread.sleep(5000);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    75
                } catch (InterruptedException ie) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    76
                    ie.printStackTrace();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    77
                }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    78
                pressAltF4Keys(robot);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    79
            } finally {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    80
                file.delete();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    81
            }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    82
        }
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
    private static void pressAltF4Keys(Robot robot) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    86
        if (robot != null) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    87
            robot.keyPress(KeyEvent.VK_ALT);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    88
            robot.keyPress(KeyEvent.VK_F4);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    89
            robot.delay(50);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    90
            robot.keyRelease(KeyEvent.VK_F4);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    91
            robot.keyRelease(KeyEvent.VK_ALT);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    92
        }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    93
    }
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
    public static void main(String[] args) throws IOException {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    96
        if (!validatePlatform()) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    97
            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
    98
        } else {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    99
            openFile();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
   100
        }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
   101
    }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
   102
}