Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Desktop/OpenByUNCPathNameTest/OpenByUNCPathNameTest.java
48230 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 655058825@summary java.awt.Desktop cannot open file with Windows UNC filename26@author Anton Litvinov27*/2829import java.awt.*;30import java.awt.event.*;31import java.io.*;3233public class OpenByUNCPathNameTest {34private static boolean validatePlatform() {35String osName = System.getProperty("os.name");36if (osName == null) {37throw new RuntimeException("Name of the current OS could not be retrieved.");38}39return osName.startsWith("Windows");40}4142private static void openFile() throws IOException {43if (!Desktop.isDesktopSupported()) {44System.out.println("java.awt.Desktop is not supported on this platform.");45} else {46Desktop desktop = Desktop.getDesktop();47if (!desktop.isSupported(Desktop.Action.OPEN)) {48System.out.println("Action.OPEN is not supported on this platform.");49return;50}51File file = File.createTempFile("Read Me File", ".txt");52try {53// Test opening of the file with Windows local file path.54desktop.open(file);55Robot robot = null;56try {57Thread.sleep(5000);58robot = new Robot();59} catch (Exception e) {60e.printStackTrace();61}62pressAltF4Keys(robot);6364// Test opening of the file with Windows UNC pathname.65String uncFilePath = "\\\\127.0.0.1\\" + file.getAbsolutePath().replace(':', '$');66File uncFile = new File(uncFilePath);67if (!uncFile.exists()) {68throw new RuntimeException(String.format(69"File with UNC pathname '%s' does not exist.", uncFilePath));70}71desktop.open(uncFile);72try {73Thread.sleep(5000);74} catch (InterruptedException ie) {75ie.printStackTrace();76}77pressAltF4Keys(robot);78} finally {79file.delete();80}81}82}8384private static void pressAltF4Keys(Robot robot) {85if (robot != null) {86robot.keyPress(KeyEvent.VK_ALT);87robot.keyPress(KeyEvent.VK_F4);88robot.delay(50);89robot.keyRelease(KeyEvent.VK_F4);90robot.keyRelease(KeyEvent.VK_ALT);91}92}9394public static void main(String[] args) throws IOException {95if (!validatePlatform()) {96System.out.println("This test is only for MS Windows OS.");97} else {98openFile();99}100}101}102103104