5838N/A/*
5838N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
5838N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5838N/A *
5838N/A * This code is free software; you can redistribute it and/or modify it
5838N/A * under the terms of the GNU General Public License version 2 only, as
5838N/A * published by the Free Software Foundation.
5838N/A *
5838N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5838N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5838N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5838N/A * version 2 for more details (a copy is included in the LICENSE file that
5838N/A * accompanied this code).
5838N/A *
5838N/A * You should have received a copy of the GNU General Public License version
5838N/A * 2 along with this work; if not, write to the Free Software Foundation,
5838N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5838N/A *
5838N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5838N/A * or visit www.oracle.com if you need additional information or have any
5838N/A * questions.
5838N/A */
5838N/A
5838N/A/* @test
5838N/A @bug 6550588
5838N/A @summary java.awt.Desktop cannot open file with Windows UNC filename
5838N/A @author Anton Litvinov
5838N/A*/
5838N/A
5838N/Aimport java.awt.*;
5838N/Aimport java.awt.event.*;
5838N/Aimport java.io.*;
5838N/A
5838N/Apublic class OpenByUNCPathNameTest {
5838N/A private static boolean validatePlatform() {
5838N/A String osName = System.getProperty("os.name");
5838N/A if (osName == null) {
5838N/A throw new RuntimeException("Name of the current OS could not be retrieved.");
5838N/A }
5838N/A return osName.startsWith("Windows");
5838N/A }
5838N/A
5838N/A private static void openFile() throws IOException {
5838N/A if (!Desktop.isDesktopSupported()) {
5838N/A System.out.println("java.awt.Desktop is not supported on this platform.");
5838N/A } else {
5838N/A Desktop desktop = Desktop.getDesktop();
5838N/A File file = File.createTempFile("Read Me File", ".txt");
5838N/A try {
5838N/A // Test opening of the file with Windows local file path.
5838N/A desktop.open(file);
5838N/A Robot robot = null;
5838N/A try {
5838N/A Thread.sleep(5000);
5838N/A robot = new Robot();
5838N/A } catch (Exception e) {
5838N/A e.printStackTrace();
5838N/A }
5838N/A pressAltF4Keys(robot);
5838N/A
5838N/A // Test opening of the file with Windows UNC pathname.
5838N/A String uncFilePath = "\\\\127.0.0.1\\" + file.getAbsolutePath().replace(':', '$');
5838N/A File uncFile = new File(uncFilePath);
5838N/A if (!uncFile.exists()) {
5838N/A throw new RuntimeException(String.format(
5838N/A "File with UNC pathname '%s' does not exist.", uncFilePath));
5838N/A }
5838N/A desktop.open(uncFile);
5838N/A try {
5838N/A Thread.sleep(5000);
5838N/A } catch (InterruptedException ie) {
5838N/A ie.printStackTrace();
5838N/A }
5838N/A pressAltF4Keys(robot);
5838N/A } finally {
5838N/A file.delete();
5838N/A }
5838N/A }
5838N/A }
5838N/A
5838N/A private static void pressAltF4Keys(Robot robot) {
5838N/A if (robot != null) {
5838N/A robot.keyPress(KeyEvent.VK_ALT);
5838N/A robot.keyPress(KeyEvent.VK_F4);
5838N/A robot.delay(50);
5838N/A robot.keyRelease(KeyEvent.VK_F4);
5838N/A robot.keyRelease(KeyEvent.VK_ALT);
5838N/A }
5838N/A }
5838N/A
5838N/A public static void main(String[] args) throws IOException {
5838N/A if (!validatePlatform()) {
5838N/A System.out.println("This test is only for MS Windows OS.");
5838N/A } else {
5838N/A openFile();
5838N/A }
5838N/A }
5838N/A}