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