1 /* 2 * Copyright (c) 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.BufferedWriter; 25 import java.io.File; 26 import java.io.FileWriter; 27 import java.io.PrintWriter; 28 import java.util.ArrayList; 29 import java.util.List; 30 31 public class JPackageCreateInstallerWinUpgradeUUIDBase { 32 33 private static String TEST_NAME; 34 private static String EXT; 35 private static String OUTPUT_1; 36 private static String[] CMD_1; 37 private static String OUTPUT_2; 38 private static String[] CMD_2; 39 private static final String FILE_1 = "file1.txt"; 40 private static final String FILE_2 = "file2.txt"; 41 42 private static void copyResults() throws Exception { 43 List<String> files = new ArrayList<>(); 44 files.add(OUTPUT_1); 45 files.add(OUTPUT_2); 46 JPackageInstallerHelper.copyTestResults(files); 47 } 48 49 private static void testCreateInstaller() throws Exception { 50 JPackageHelper.executeCLI(true, CMD_1); 51 JPackageInstallerHelper.validateOutput(OUTPUT_1); 52 JPackageHelper.executeCLI(true, CMD_2); 53 JPackageInstallerHelper.validateOutput(OUTPUT_2); 54 copyResults(); 55 } 56 57 private static void verifyInstall() throws Exception { 58 String app = JPackagePath.getWinInstalledApp(TEST_NAME); 59 JPackageInstallerHelper.validateApp(app); 60 61 String file1Path = JPackagePath.getWinInstalledAppFolder(TEST_NAME) + File.separator + FILE_1; 62 File file1 = new File(file1Path); 63 if (EXT.equals("msi")) { 64 if (file1.exists()) { 65 throw new AssertionError("Unexpected file does exist: " 66 + file1.getAbsolutePath()); 67 } 68 } else if (EXT.equals("exe")) { 69 if (!file1.exists()) { 70 throw new AssertionError("Unexpected file does not exist: " 71 + file1.getAbsolutePath()); 72 } 73 } else { 74 throw new AssertionError("Unknown installer type: " + EXT); 75 } 76 77 String file2Path = JPackagePath.getWinInstalledAppFolder(TEST_NAME) + File.separator + FILE_2; 78 File file2 = new File(file2Path); 79 if (!file2.exists()) { 80 throw new AssertionError("Unexpected file does not exist: " 81 + file2.getAbsolutePath()); 82 } 83 } 84 85 private static void verifyUnInstall() throws Exception { 86 String folderPath = JPackagePath.getWinInstallFolder(TEST_NAME); 87 File folder = new File(folderPath); 88 if (folder.exists()) { 89 throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist"); 90 } 91 } 92 93 private static void init(String name, String ext) { 94 TEST_NAME = name; 95 EXT = ext; 96 OUTPUT_1 = "output" + File.separator + TEST_NAME + "-1.0." + EXT; 97 CMD_1 = new String[]{ 98 "create-installer", 99 "--installer-type", EXT, 100 "--input", "input", 101 "--output", "output", 102 "--name", TEST_NAME, 103 "--main-jar", "hello.jar", 104 "--main-class", "Hello", 105 "--overwrite", 106 "--files", "hello.jar" + File.pathSeparator + FILE_1, 107 "--app-version", "1.0", 108 "--win-upgrade-uuid", "F0B18E75-52AD-41A2-BC86-6BE4FCD50BEB"}; 109 OUTPUT_2 = "output" + File.separator + TEST_NAME + "-2.0." + EXT; 110 CMD_2 = new String[]{ 111 "create-installer", 112 "--installer-type", EXT, 113 "--input", "input", 114 "--output", "output", 115 "--name", TEST_NAME, 116 "--main-jar", "hello.jar", 117 "--main-class", "Hello", 118 "--overwrite", 119 "--files", "hello.jar" + File.pathSeparator + FILE_2, 120 "--app-version", "2.0", 121 "--win-upgrade-uuid", "F0B18E75-52AD-41A2-BC86-6BE4FCD50BEB"}; 122 } 123 124 private static void createInputFile(String name, String context) throws Exception { 125 try (PrintWriter out = new PrintWriter( 126 new BufferedWriter(new FileWriter("input" + File.separator + name)))) { 127 out.println(context); 128 } 129 } 130 131 public static void run(String name, String ext) throws Exception { 132 init(name, ext); 133 134 if (JPackageInstallerHelper.isVerifyInstall()) { 135 verifyInstall(); 136 } else if (JPackageInstallerHelper.isVerifyUnInstall()) { 137 verifyUnInstall(); 138 } else { 139 JPackageHelper.createHelloInstallerJar(); 140 createInputFile(FILE_1, FILE_1); 141 createInputFile(FILE_2, FILE_2); 142 testCreateInstaller(); 143 } 144 } 145 }