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.awt.Desktop; 25 import java.io.BufferedWriter; 26 import java.io.File; 27 import java.io.FileWriter; 28 import java.io.PrintWriter; 29 import java.nio.file.Files; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 public class JPackageCreateInstallerFileAssociationsBase { 34 35 private static String TEST_NAME; 36 private static String EXT; 37 private static String TEST_EXT; 38 private static String OUTPUT; 39 private static String[] CMD; 40 41 private static void copyResults() throws Exception { 42 List<String> files = new ArrayList<>(); 43 files.add(OUTPUT); 44 JPackageInstallerHelper.copyTestResults(files); 45 } 46 47 private static void testCreateInstaller() throws Exception { 48 JPackageHelper.executeCLI(true, CMD); 49 JPackageInstallerHelper.validateOutput(OUTPUT); 50 copyResults(); 51 } 52 53 private static void validateAppOutput() throws Exception { 54 File outFile = new File("appOutput.txt"); 55 int count = 10; 56 boolean bOutputCreated = false; 57 while (count > 0) { 58 if (!outFile.exists()) { 59 Thread.sleep(3000); 60 count--; 61 } else { 62 bOutputCreated = true; 63 break; 64 } 65 } 66 67 if (!bOutputCreated) { 68 throw new AssertionError(outFile.getAbsolutePath() + " was not created"); 69 } 70 71 String output = Files.readString(outFile.toPath()); 72 String[] result = output.split("\n"); 73 if (result.length != 3) { 74 System.err.println(output); 75 throw new AssertionError( 76 "Unexpected number of lines: " + result.length); 77 } 78 79 if (!result[0].trim().equals("jpackage test application")) { 80 throw new AssertionError("Unexpected result[0]: " + result[0]); 81 } 82 83 if (!result[1].trim().equals("args.length: 1")) { 84 throw new AssertionError("Unexpected result[1]: " + result[1]); 85 } 86 87 File faFile = new File(TEST_NAME + "." + TEST_EXT); 88 if (!result[2].trim().equals(faFile.getAbsolutePath())) { 89 throw new AssertionError("Unexpected result[2]: " + result[2]); 90 } 91 } 92 93 private static void verifyInstall() throws Exception { 94 createFileAssociationsTestFile(); 95 Desktop.getDesktop().open(new File(TEST_NAME + "." + TEST_EXT)); 96 validateAppOutput(); 97 98 // Validate start menu 99 JPackageInstallerHelper.validateStartMenu("Unknown", TEST_NAME, true); 100 101 // Validate registry 102 String[] values1 = {TEST_NAME}; 103 JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\." + TEST_EXT, values1, true); 104 String[] values2 = {TEST_EXT}; 105 JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\MIME\\Database\\Content Type\\application/" + TEST_EXT, values2, true); 106 } 107 108 private static void verifyUnInstall() throws Exception { 109 String folderPath = JPackagePath.getWinInstallFolder(TEST_NAME); 110 File folder = new File(folderPath); 111 if (folder.exists()) { 112 throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist"); 113 } 114 115 // Validate start menu 116 JPackageInstallerHelper.validateStartMenu("Unknown", TEST_NAME, false); 117 118 // Validate registry 119 JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\." + TEST_EXT, null, false); 120 JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\MIME\\Database\\Content Type\\application/" + TEST_EXT, null, false); 121 } 122 123 private static void createFileAssociationsTestFile() throws Exception { 124 try (PrintWriter out = new PrintWriter(new BufferedWriter( 125 new FileWriter(TEST_NAME + "." + TEST_EXT)))) { 126 out.println(TEST_NAME); 127 } 128 } 129 130 private static void createFileAssociationsProperties() throws Exception { 131 try (PrintWriter out = new PrintWriter(new BufferedWriter( 132 new FileWriter("fa.properties")))) { 133 out.println("extension=" + TEST_EXT); 134 out.println("mime-type=application/" + TEST_EXT); 135 out.println("description=jpackage test extention"); 136 } 137 } 138 139 private static void init(String name, String ext, String installDir, String testExt) { 140 TEST_NAME = name; 141 EXT = ext; 142 TEST_EXT = testExt; 143 OUTPUT = "output" + File.separator + TEST_NAME + "-1.0." + EXT; 144 if (installDir == null) { 145 CMD = new String[]{ 146 "create-installer", 147 "--installer-type", EXT, 148 "--input", "input", 149 "--output", "output", 150 "--name", TEST_NAME, 151 "--main-jar", "hello.jar", 152 "--main-class", "Hello", 153 "--file-associations", "fa.properties"}; 154 } else { 155 CMD = new String[]{ 156 "create-installer", 157 "--installer-type", EXT, 158 "--input", "input", 159 "--output", "output", 160 "--name", TEST_NAME, 161 "--main-jar", "hello.jar", 162 "--main-class", "Hello", 163 "--file-associations", "fa.properties", 164 "--install-dir", installDir}; 165 } 166 } 167 168 public static void run(String name, String ext, String installDir, String testExt) throws Exception { 169 init(name, ext, installDir, testExt); 170 171 if (JPackageInstallerHelper.isVerifyInstall()) { 172 verifyInstall(); 173 } else if (JPackageInstallerHelper.isVerifyUnInstall()) { 174 verifyUnInstall(); 175 } else { 176 JPackageHelper.createHelloInstallerJar(); 177 createFileAssociationsProperties(); 178 testCreateInstaller(); 179 } 180 } 181 }