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             if (!desktop.isSupported(Desktop.Action.OPEN)) {
  49                 System.out.println("Action.OPEN is not supported on this platform.");
  50                 return;
  51             }
  52             File file = File.createTempFile("Read Me File", ".txt");
  53             try {
  54                 // Test opening of the file with Windows local file path.
  55                 desktop.open(file);
  56                 Robot robot = null;
  57                 try {
  58                     Thread.sleep(5000);
  59                     robot = new Robot();
  60                 } catch (Exception e) {
  61                     e.printStackTrace();
  62                 }
  63                 pressAltF4Keys(robot);
  64 
  65                 // Test opening of the file with Windows UNC pathname.
  66                 String uncFilePath = "\\\\127.0.0.1\\" + file.getAbsolutePath().replace(':', '$');
  67                 File uncFile = new File(uncFilePath);
  68                 if (!uncFile.exists()) {
  69                     throw new RuntimeException(String.format(
  70                         "File with UNC pathname '%s' does not exist.", uncFilePath));
  71                 }
  72                 desktop.open(uncFile);
  73                 try {
  74                     Thread.sleep(5000);
  75                 } catch (InterruptedException ie) {
  76                     ie.printStackTrace();
  77                 }
  78                 pressAltF4Keys(robot);
  79             } finally {
  80                 file.delete();
  81             }
  82         }
  83     }
  84 
  85     private static void pressAltF4Keys(Robot robot) {
  86         if (robot != null) {
  87             robot.keyPress(KeyEvent.VK_ALT);
  88             robot.keyPress(KeyEvent.VK_F4);
  89             robot.delay(50);
  90             robot.keyRelease(KeyEvent.VK_F4);
  91             robot.keyRelease(KeyEvent.VK_ALT);
  92         }
  93     }
  94 
  95     public static void main(String[] args) throws IOException {
  96         if (!validatePlatform()) {
  97             System.out.println("This test is only for MS Windows OS.");
  98         } else {
  99             openFile();
 100         }
 101     }
 102 }