1 /* 2 * Copyright (c) 2016, 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 /* 25 * @test 26 * @bug 8081722 27 * @summary Provide public API for file hierarchy provided by 28 * sun.awt.shell.ShellFolder 29 * @author Semyon Sadetsky 30 * @run main ShellFolderQueriesTest 31 */ 32 33 34 import javax.swing.filechooser.FileSystemView; 35 import java.io.File; 36 import java.io.FileNotFoundException; 37 import java.io.FileOutputStream; 38 import java.io.IOException; 39 40 public class ShellFolderQueriesTest { 41 static final String HOME = System.getProperty("user.home"); 42 static final FileSystemView fsv = FileSystemView.getFileSystemView(); 43 44 45 static String scriptBeg = 46 "set WshShell = WScript.CreateObject(\"WScript.Shell\")\n" + 47 "set oShellLink = WshShell.CreateShortcut(\"shortcut.lnk\")\n" + 48 "oShellLink.TargetPath = \""; 49 static String scriptEnd = "\"\noShellLink.WindowStyle = 1\noShellLink.Save"; 50 51 public static void main(String[] args) throws Exception { 52 if(System.getProperty("os.name").toLowerCase().contains("windows")) { 53 System.out.println("Windows detected: will run shortcut test"); 54 testGet(); 55 testLink(); 56 } else { 57 testGet(); 58 } 59 System.out.println("ok"); 60 } 61 62 private static void testLink() throws IOException, InterruptedException { 63 // Create and execute VBS script to create a link 64 File file = createVbsScript(scriptBeg + HOME + scriptEnd); 65 Runtime.getRuntime().exec("cscript " + file.getName(), null, 66 file.getParentFile()).waitFor(); 67 file.delete(); 68 69 File link = new File(file.getParentFile(), "shortcut.lnk"); 70 if (!fsv.isLink(link)) { 71 link.delete(); 72 throw new RuntimeException("Link is not detected"); 73 } 74 75 File location = fsv.getLinkLocation(link); 76 if (!location.getAbsolutePath().equals(HOME)) { 77 link.delete(); 78 throw new RuntimeException("Link location " + location + 79 " is wrong"); 80 } 81 link.delete(); 82 83 84 link = File.createTempFile("test", ".tst"); 85 86 if (fsv.isLink(link)) { 87 link.delete(); 88 throw new RuntimeException("File is not a link"); 89 } 90 91 try { 92 location = fsv.getLinkLocation(link); 93 if (location != null) { 94 link.delete(); 95 throw new RuntimeException("Not a link, should return null"); 96 } 97 } 98 catch (FileNotFoundException e) { 99 } 100 link.delete(); 101 } 102 103 private static File createVbsScript(String script) throws IOException { 104 File file = File.createTempFile("test", ".vbs"); 105 file.deleteOnExit(); 106 FileOutputStream fos = new FileOutputStream(file); 107 fos.write(script.getBytes()); 108 fos.close(); 109 return file; 110 } 111 112 private static void testGet() { 113 File[] files = fsv.getChooserComboBoxFiles(); 114 for (File file : files) { 115 if (fsv.isLink(file)) { 116 throw new RuntimeException( 117 "Link shouldn't be in FileChooser combobox, " 118 + file.getPath()); 119 } 120 } 121 } 122 }