1 /*
   2  * Copyright (c) 2018, 2019, 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 import java.io.File;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.StandardCopyOption;
  28 import java.util.List;
  29 
  30 public class JPackageInstallerHelper {
  31     private static final String JPACKAGE_TEST_OUTPUT = "jpackage.test.output";
  32     private static final String JPACKAGE_VERIFY_INSTALL = "jpackage.verify.install";
  33     private static final String JPACKAGE_VERIFY_UNINSTALL = "jpackage.verify.uninstall";
  34     private static String testOutput;
  35     private static final boolean isTestOutputSet;
  36     private static final boolean isVerifyInstall;
  37     private static final boolean isVerifyUnInstall;
  38 
  39     static {
  40         String out = System.getProperty(JPACKAGE_TEST_OUTPUT);
  41         isTestOutputSet = (out != null);
  42         if (isTestOutputSet) {
  43             File file = new File(out);
  44             if (!file.exists()) {
  45                 throw new AssertionError(file.getAbsolutePath() + " does not exist");
  46             }
  47 
  48             if (!file.isDirectory()) {
  49                 throw new AssertionError(file.getAbsolutePath() + " is not a directory");
  50             }
  51 
  52             if (!file.canWrite()) {
  53                 throw new AssertionError(file.getAbsolutePath() + " is not writable");
  54             }
  55 
  56             if (out.endsWith(File.separator)) {
  57                 out = out.substring(0, out.length() - 2);
  58             }
  59 
  60             testOutput = out;
  61         }
  62 
  63         isVerifyInstall = (System.getProperty(JPACKAGE_VERIFY_INSTALL) != null);
  64         isVerifyUnInstall = (System.getProperty(JPACKAGE_VERIFY_UNINSTALL) != null);
  65     }
  66 
  67     public static boolean isTestOutputSet() {
  68         return isTestOutputSet;
  69     }
  70 
  71     public static boolean isVerifyInstall() {
  72         return isVerifyInstall;
  73     }
  74 
  75     public static boolean isVerifyUnInstall() {
  76         return isVerifyUnInstall;
  77     }
  78 
  79     public static void copyTestResults(List<String> files) throws Exception {
  80         if (!isTestOutputSet()) {
  81             return;
  82         }
  83 
  84         File dest = new File(testOutput);
  85         if (!dest.exists()) {
  86             dest.mkdirs();
  87         }
  88 
  89         if (JPackageHelper.isWindows()) {
  90             files.add(JPackagePath.getTestSrc() + File.separator + "install.bat");
  91             files.add(JPackagePath.getTestSrc() + File.separator + "uninstall.bat");
  92         } else {
  93             files.add(JPackagePath.getTestSrc() + File.separator + "install.sh");
  94             files.add(JPackagePath.getTestSrc() + File.separator + "uninstall.sh");
  95         }
  96 
  97         for (String file : files) {
  98             Path source = Path.of(file);
  99             Path target = Path.of(dest.toPath() + File.separator + source.getFileName());
 100             Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
 101         }
 102     }
 103 
 104     public static void validateApp(String app) throws Exception {
 105         File outFile = new File("appOutput.txt");
 106         if (outFile.exists()) {
 107             outFile.delete();
 108         }
 109 
 110         int retVal = JPackageHelper.execute(outFile, app);
 111         if (retVal != 0) {
 112             throw new AssertionError(
 113                    "Test application exited with error: " + retVal);
 114         }
 115 
 116         if (!outFile.exists()) {
 117             throw new AssertionError(outFile.getAbsolutePath() + " was not created");
 118         }
 119 
 120         String output = Files.readString(outFile.toPath());
 121         String[] result = output.split("\n");
 122         if (result.length != 2) {
 123             System.err.println(output);
 124             throw new AssertionError(
 125                    "Unexpected number of lines: " + result.length);
 126         }
 127 
 128         if (!result[0].trim().equals("jpackage test application")) {
 129             throw new AssertionError("Unexpected result[0]: " + result[0]);
 130         }
 131 
 132         if (!result[1].trim().equals("args.length: 0")) {
 133             throw new AssertionError("Unexpected result[1]: " + result[1]);
 134         }
 135     }
 136 
 137     public static void validateOutput(String output) throws Exception {
 138         File file = new File(output);
 139         if (!file.exists()) {
 140             // Try lower case in case of OS is case sensitive
 141             file = new File(output.toLowerCase());
 142             if (!file.exists()) {
 143                 throw new AssertionError("Cannot find " + file.getAbsolutePath());
 144             }
 145         }
 146     }
 147 
 148     public static void validateStartMenu(String menuGroup, String menu, boolean exist)
 149             throws Exception {
 150         String startMenuLink = JPackagePath.getWinStartMenu() +
 151                 File.separator + menuGroup + File.separator +
 152                 menu + ".lnk";
 153 
 154         File link = new File(startMenuLink);
 155         if (exist) {
 156             if (!link.exists()) {
 157                 throw new AssertionError("Cannot find " + link.getAbsolutePath());
 158             }
 159         } else {
 160             if (link.exists()) {
 161                 throw new AssertionError("Error: " + link.getAbsolutePath() + " exist");
 162             }
 163         }
 164     }
 165 
 166     public static void validateDesktopShortcut(String name, boolean exist)
 167             throws Exception {
 168         String shortcutLink = JPackagePath.getWinPublicDesktop() +
 169                 File.separator + name + ".lnk";
 170 
 171         File link = new File(shortcutLink);
 172         if (exist) {
 173             if (!link.exists()) {
 174                 throw new AssertionError("Cannot find " + link.getAbsolutePath());
 175             }
 176         } else {
 177             if (link.exists()) {
 178                 throw new AssertionError("Error: " + link.getAbsolutePath() + " exist");
 179             }
 180         }
 181     }
 182 
 183     public static void validateUserLocalStartMenu(String menuGroup, String menu, boolean exist)
 184             throws Exception {
 185         String startMenuLink = JPackagePath.getWinUserLocalStartMenu() +
 186                 File.separator + menuGroup + File.separator +
 187                 menu + ".lnk";
 188 
 189         File link = new File(startMenuLink);
 190         if (exist) {
 191             if (!link.exists()) {
 192                 throw new AssertionError("Cannot find " + link.getAbsolutePath());
 193             }
 194         } else {
 195             if (link.exists()) {
 196                 throw new AssertionError("Error: " + link.getAbsolutePath() + " exist");
 197             }
 198         }
 199     }
 200 
 201     public static void validateWinRegistry(String key, String [] values, boolean retValZero)
 202             throws Exception {
 203         File outFile = new File("regOutput.txt");
 204         if (outFile.exists()) {
 205             outFile.delete();
 206         }
 207 
 208         int retVal = JPackageHelper.execute(outFile, "reg.exe", "query", key);
 209         if (retValZero) {
 210             if (retVal != 0) {
 211                 System.out.println("validateWinRegistry() key=" + key);
 212                 if (outFile.exists()) {
 213                     System.err.println(Files.readString(outFile.toPath()));
 214                 }
 215                 throw new AssertionError(
 216                        "Reg.exe application exited with error: " + retVal);
 217             }
 218         } else {
 219             if (retVal == 0) {
 220                 System.out.println("validateWinRegistry() key=" + key);
 221                 if (outFile.exists()) {
 222                     System.err.println(Files.readString(outFile.toPath()));
 223                 }
 224                 throw new AssertionError(
 225                        "Reg.exe application exited without error: " + retVal);
 226             } else {
 227                 return; // Done
 228             }
 229         }
 230 
 231         if (!outFile.exists()) {
 232             throw new AssertionError(outFile.getAbsolutePath() + " was not created");
 233         }
 234 
 235         String output = Files.readString(outFile.toPath());
 236         for (String value : values) {
 237             if (!output.contains(value)) {
 238                 System.err.println(output);
 239                 throw new AssertionError("Cannot find in registry: " + value);
 240             }
 241         }
 242     }
 243 }