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 }