Path: blob/master/test/jdk/java/awt/Desktop/OpenByUNCPathNameTest/OpenByUNCPathNameTest.java
41153 views
/*1* Copyright (c) 2013, 2020, 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@key headful26@requires (os.family == "windows")27@summary java.awt.Desktop cannot open file with Windows UNC filename28@author Anton Litvinov29@run main/manual OpenByUNCPathNameTest30*/3132import javax.swing.JButton;33import javax.swing.JFrame;34import javax.swing.JPanel;35import javax.swing.JTextArea;36import javax.swing.SwingUtilities;37import javax.swing.WindowConstants;38import java.awt.BorderLayout;39import java.awt.Desktop;40import java.awt.FlowLayout;41import java.io.IOException;42import java.io.File;43import java.util.concurrent.CountDownLatch;44import java.util.concurrent.TimeUnit;4546public class OpenByUNCPathNameTest {47private static volatile CountDownLatch countDownLatch;48private static JFrame instructionFrame;49private static JFrame testFrame;50private static volatile boolean testPassed = false;51private static File file;5253private static boolean validatePlatform() {54String osName = System.getProperty("os.name");55if (osName == null) {56throw new RuntimeException("Name of the current OS could not be" +57" retrieved.");58}59return osName.startsWith("Windows");60}6162private static void createInstructionUI() {63SwingUtilities.invokeLater(() -> {64String instructions =65"1. Make sure that disk C is shared \n"66+ "2. Click on openFileByLocalPath Button to test Test"67+ " opening of the file with Windows local file path\n"68+ "3. Check that the file is opened successfully\n"69+ "4. Close the file\n"70+ "5. Click on openFileByUNCPath Button to test Test"71+ " opening of the file with Windows UNC pathname\n"72+ "6. Check that the file is opened successfully\n"73+ "7. Close the file\n"74+ "8. If all the conditions are met then press PASS else "75+ "press FAIL";76instructionFrame = new JFrame("InstructionFrame");77JTextArea textArea = new JTextArea(instructions);78textArea.setEditable(false);79final JButton passButton = new JButton("PASS");80passButton.addActionListener((e) -> {81testPassed = true;82instructionFrame.dispose();83testFrame.dispose();84file.delete();85countDownLatch.countDown();86});87final JButton failButton = new JButton("FAIL");88failButton.addActionListener((e) -> {89instructionFrame.dispose();90testFrame.dispose();91file.delete();92countDownLatch.countDown();93});949596JPanel mainPanel = new JPanel(new BorderLayout());97mainPanel.add(textArea, BorderLayout.CENTER);9899JPanel buttonPanel = new JPanel(new FlowLayout());100buttonPanel.add(passButton);101buttonPanel.add(failButton);102mainPanel.add(buttonPanel, BorderLayout.SOUTH);103instructionFrame.setDefaultCloseOperation(104WindowConstants.DISPOSE_ON_CLOSE);105instructionFrame.setBounds(0,0,500,500);106instructionFrame.add(mainPanel);107instructionFrame.pack();108instructionFrame.setVisible(true);109});110}111112private static void openFile() throws IOException {113if (!Desktop.isDesktopSupported()) {114System.out.println("java.awt.Desktop is not supported on this"+115" platform.");116return;117}118119Desktop desktop = Desktop.getDesktop();120if (!desktop.isSupported(Desktop.Action.OPEN)) {121System.out.println("Action.OPEN is not supported on this" +122" platform.");123return;124}125file = File.createTempFile("Read Me File", ".txt");126testFrame = new JFrame("Test Frame");127JPanel testButtonPanel = new JPanel(new FlowLayout());128final JButton openFileByLocalPathButton = new129JButton("OpenFileByLocalPath");130final JButton openFileByUNCPathButton = new131JButton("OpenFileByUNCPath");132openFileByLocalPathButton.addActionListener((e) -> {133try {134desktop.open(file);135} catch (IOException ioException) {136}137});138139SwingUtilities.invokeLater(()->{140testButtonPanel.add(openFileByLocalPathButton);141testButtonPanel.add(openFileByUNCPathButton);142testFrame.setDefaultCloseOperation(143WindowConstants.DISPOSE_ON_CLOSE);144testFrame.setBounds(600,0,1000,200);145testFrame.add(testButtonPanel);146testFrame.pack();147testFrame.setVisible(true);148});149150// Test opening of the file with Windows UNC pathname.151String uncFilePath = "\\\\127.0.0.1\\" +152file.getAbsolutePath().replace(':', '$');153File uncFile = new File(uncFilePath);154if (!uncFile.exists()) {155throw new RuntimeException(String.format("File "+156"with UNC pathname '%s' does not exist.", uncFilePath));157}158openFileByUNCPathButton.addActionListener((e) -> {159try {160desktop.open(uncFile);161} catch (IOException ioException) {162}163});164}165166public static void main(String[] args) throws Exception {167if (!validatePlatform()) {168System.out.println("This test is only for MS Windows OS.");169return;170}171countDownLatch = new CountDownLatch(1);172OpenByUNCPathNameTest openByUNCPathNameTest =173new OpenByUNCPathNameTest();174175openByUNCPathNameTest.createInstructionUI();176openByUNCPathNameTest.openFile();177countDownLatch.await(15, TimeUnit.MINUTES);178179if(!testPassed) {180throw new RuntimeException("Test failed!");181}182}183}184185186