jdk/test/java/awt/Desktop/OpenByUNCPathNameTest/OpenByUNCPathNameTest.java
author alitvinov
Wed, 20 Mar 2013 20:41:03 +0400
changeset 16469 169f9bf9b5d6
child 23243 43e5bc27297f
permissions -rw-r--r--
6550588: java.awt.Desktop cannot open file with Windows UNC filename Reviewed-by: art, uta
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();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    48
            File file = File.createTempFile("Read Me File", ".txt");
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    49
            try {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    50
                // 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
    51
                desktop.open(file);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    52
                Robot robot = null;
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
                    Thread.sleep(5000);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    55
                    robot = new Robot();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    56
                } catch (Exception e) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    57
                    e.printStackTrace();
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    58
                }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    59
                pressAltF4Keys(robot);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    60
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    61
                // 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
    62
                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
    63
                File uncFile = new File(uncFilePath);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    64
                if (!uncFile.exists()) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    65
                    throw new RuntimeException(String.format(
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    66
                        "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
    67
                }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    68
                desktop.open(uncFile);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    69
                try {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    70
                    Thread.sleep(5000);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    71
                } catch (InterruptedException ie) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    72
                    ie.printStackTrace();
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
                pressAltF4Keys(robot);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    75
            } finally {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    76
                file.delete();
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
        }
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
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    81
    private static void pressAltF4Keys(Robot robot) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    82
        if (robot != null) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    83
            robot.keyPress(KeyEvent.VK_ALT);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    84
            robot.keyPress(KeyEvent.VK_F4);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    85
            robot.delay(50);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    86
            robot.keyRelease(KeyEvent.VK_F4);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    87
            robot.keyRelease(KeyEvent.VK_ALT);
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    88
        }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    89
    }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    90
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    91
    public static void main(String[] args) throws IOException {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    92
        if (!validatePlatform()) {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    93
            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
    94
        } else {
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    95
            openFile();
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
    }
169f9bf9b5d6 6550588: java.awt.Desktop cannot open file with Windows UNC filename
alitvinov
parents:
diff changeset
    98
}