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    @key headful
  27    @requires (os.family == "windows")
  28    @summary java.awt.Desktop cannot open file with Windows UNC filename
  29    @author Anton Litvinov
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.io.*;
  35 
  36 public class OpenByUNCPathNameTest {
  37     private static boolean validatePlatform() {
  38         String osName = System.getProperty("os.name");
  39         if (osName == null) {
  40             throw new RuntimeException("Name of the current OS could not be retrieved.");
  41         }
  42         return osName.startsWith("Windows");
  43     }
  44 
  45     private static void openFile() throws IOException {
  46         if (!Desktop.isDesktopSupported()) {
  47             System.out.println("java.awt.Desktop is not supported on this platform.");
  48         } else {
  49             Desktop desktop = Desktop.getDesktop();
  50             if (!desktop.isSupported(Desktop.Action.OPEN)) {
  51                 System.out.println("Action.OPEN is not supported on this platform.");
  52                 return;
  53             }
  54             File file = File.createTempFile("Read Me File", ".txt");
  55             try {
  56                 // Test opening of the file with Windows local file path.
  57                 desktop.open(file);
  58                 Robot robot = null;
  59                 try {
  60                     Thread.sleep(5000);
  61                     robot = new Robot();
  62                 } catch (Exception e) {
  63                     e.printStackTrace();
  64                 }
  65                 pressAltF4Keys(robot);
  66 
  67                 // Test opening of the file with Windows UNC pathname.
  68                 String uncFilePath = "\\\\127.0.0.1\\" + file.getAbsolutePath().replace(':', '$');
  69                 File uncFile = new File(uncFilePath);
  70                 if (!uncFile.exists()) {
  71                     throw new RuntimeException(String.format(
  72                         "File with UNC pathname '%s' does not exist.", uncFilePath));
  73                 }
  74                 desktop.open(uncFile);
  75                 try {
  76                     Thread.sleep(5000);
  77                 } catch (InterruptedException ie) {
  78                     ie.printStackTrace();
  79                 }
  80                 pressAltF4Keys(robot);
  81             } finally {
  82                 file.delete();
  83             }
  84         }
  85     }
  86 
  87     private static void pressAltF4Keys(Robot robot) {
  88         if (robot != null) {
  89             robot.keyPress(KeyEvent.VK_ALT);
  90             robot.keyPress(KeyEvent.VK_F4);
  91             robot.delay(50);
  92             robot.keyRelease(KeyEvent.VK_F4);
  93             robot.keyRelease(KeyEvent.VK_ALT);
  94         }
  95     }
  96 
  97     public static void main(String[] args) throws IOException {
  98         if (!validatePlatform()) {
  99             System.out.println("This test is only for MS Windows OS.");
 100         } else {
 101             openFile();
 102         }
 103     }
 104 }