1 /*
   2  * Copyright (c) 2016, 2018, 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 8182041
  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             testShortcutPanelFiles();
  57         } else {
  58             testGet();
  59         }
  60         System.out.println("ok");
  61     }
  62 
  63     private static void testLink() throws IOException, InterruptedException {
  64         // Create and execute VBS script to create a link
  65         File file = createVbsScript(scriptBeg + HOME + scriptEnd);
  66         Runtime.getRuntime().exec("cscript " + file.getName(), null,
  67                 file.getParentFile()).waitFor();
  68         file.delete();
  69 
  70         File link = new File(file.getParentFile(), "shortcut.lnk");
  71         if (!fsv.isLink(link)) {
  72             link.delete();
  73             throw new RuntimeException("Link is not detected");
  74         }
  75 
  76         File location = fsv.getLinkLocation(link);
  77         if (!location.getAbsolutePath().equals(HOME)) {
  78             link.delete();
  79             throw new RuntimeException("Link location " + location +
  80                     " is wrong");
  81         }
  82         link.delete();
  83 
  84 
  85         link = File.createTempFile("test", ".tst");
  86 
  87         if (fsv.isLink(link)) {
  88             link.delete();
  89             throw new RuntimeException("File is not a link");
  90         }
  91 
  92         try {
  93             location = fsv.getLinkLocation(link);
  94             if (location != null) {
  95                 link.delete();
  96                 throw new RuntimeException("Not a link, should return null");
  97             }
  98         }
  99         catch (FileNotFoundException e) {
 100         }
 101         link.delete();
 102     }
 103 
 104     private static File createVbsScript(String script) throws IOException {
 105         File file = File.createTempFile("test", ".vbs");
 106         file.deleteOnExit();
 107         FileOutputStream fos = new FileOutputStream(file);
 108         fos.write(script.getBytes());
 109         fos.close();
 110         return file;
 111     }
 112 
 113     private static void testGet() {
 114         File[] files = fsv.getChooserComboBoxFiles();
 115         for (File file : files) {
 116             if (fsv.isLink(file)) {
 117                 throw new RuntimeException(
 118                         "Link shouldn't be in FileChooser combobox, "
 119                                 + file.getPath());
 120             }
 121         }
 122     }
 123 
 124     private static void testShortcutPanelFiles() {
 125         File[] shortcuts = fsv.getChooserShortcutPanelFiles();
 126         if (shortcuts.length == 0) {
 127             throw new RuntimeException("No shortcut panel files found.");
 128         }
 129     }
 130 }