Path: blob/master/test/jdk/javax/swing/JFileChooser/ShellFolderQueries/ShellFolderQueriesTest.java
41154 views
/*1* Copyright (c) 2016, 2018, 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/*24* @test25* @bug 8081722 818204126* @summary Provide public API for file hierarchy provided by27* sun.awt.shell.ShellFolder28* @author Semyon Sadetsky29* @run main ShellFolderQueriesTest30*/313233import javax.swing.filechooser.FileSystemView;34import java.io.File;35import java.io.FileNotFoundException;36import java.io.FileOutputStream;37import java.io.IOException;3839public class ShellFolderQueriesTest {40static final String HOME = System.getProperty("user.home");41static final FileSystemView fsv = FileSystemView.getFileSystemView();424344static String scriptBeg =45"set WshShell = WScript.CreateObject(\"WScript.Shell\")\n" +46"set oShellLink = WshShell.CreateShortcut(\"shortcut.lnk\")\n" +47"oShellLink.TargetPath = \"";48static String scriptEnd = "\"\noShellLink.WindowStyle = 1\noShellLink.Save";4950public static void main(String[] args) throws Exception {51if(System.getProperty("os.name").toLowerCase().contains("windows")) {52System.out.println("Windows detected: will run shortcut test");53testGet();54testLink();55testShortcutPanelFiles();56} else {57testGet();58}59System.out.println("ok");60}6162private static void testLink() throws IOException, InterruptedException {63// Create and execute VBS script to create a link64File file = createVbsScript(scriptBeg + HOME + scriptEnd);65Runtime.getRuntime().exec("cscript " + file.getName(), null,66file.getParentFile()).waitFor();67file.delete();6869File link = new File(file.getParentFile(), "shortcut.lnk");70if (!fsv.isLink(link)) {71link.delete();72throw new RuntimeException("Link is not detected");73}7475File location = fsv.getLinkLocation(link);76if (!location.getAbsolutePath().equals(HOME)) {77link.delete();78throw new RuntimeException("Link location " + location +79" is wrong");80}81link.delete();828384link = File.createTempFile("test", ".tst");8586if (fsv.isLink(link)) {87link.delete();88throw new RuntimeException("File is not a link");89}9091try {92location = fsv.getLinkLocation(link);93if (location != null) {94link.delete();95throw new RuntimeException("Not a link, should return null");96}97}98catch (FileNotFoundException e) {99}100link.delete();101}102103private static File createVbsScript(String script) throws IOException {104File file = File.createTempFile("test", ".vbs");105file.deleteOnExit();106FileOutputStream fos = new FileOutputStream(file);107fos.write(script.getBytes());108fos.close();109return file;110}111112private static void testGet() {113File[] files = fsv.getChooserComboBoxFiles();114for (File file : files) {115if (fsv.isLink(file)) {116throw new RuntimeException(117"Link shouldn't be in FileChooser combobox, "118+ file.getPath());119}120}121}122123private static void testShortcutPanelFiles() {124File[] shortcuts = fsv.getChooserShortcutPanelFiles();125if (shortcuts.length == 0) {126throw new RuntimeException("No shortcut panel files found.");127}128}129}130131132