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.io.FileInputStream; 26 import java.nio.file.Files; 27 import javax.xml.parsers.DocumentBuilder; 28 import javax.xml.parsers.DocumentBuilderFactory; 29 import javax.xml.xpath.XPath; 30 import javax.xml.xpath.XPathConstants; 31 import javax.xml.xpath.XPathFactory; 32 33 /* 34 * @test 35 * @summary jpackage create image bundle name test 36 * @library ../../helpers 37 * @build JPackageHelper 38 * @build JPackagePath 39 * @modules jdk.jpackage 40 * @requires (os.family == "mac") 41 * @run main/othervm -Xmx512m JPackageCreateAppImageBundleNameTest 42 */ 43 public class JPackageCreateAppImageBundleNameTest { 44 private static final String OUTPUT = "output"; 45 private static final String app = JPackagePath.getApp(); 46 private static final String appOutput = JPackagePath.getAppOutputFile(); 47 private static final String appWorkingDir = JPackagePath.getAppWorkingDir(); 48 private static final String MAC_BUNDLE_NAME = "TestBundleName"; 49 private static final String APP_NAME = "test"; 50 51 private static final String [] CMD_1 = { 52 "--input", "input", 53 "--output", OUTPUT, 54 "--name", APP_NAME, 55 "--main-jar", "hello.jar", 56 "--main-class", "Hello" 57 }; 58 59 private static final String [] CMD_2 = { 60 "--input", "input", 61 "--output", OUTPUT, 62 "--name", APP_NAME, 63 "--main-jar", "hello.jar", 64 "--main-class", "Hello", 65 "--mac-bundle-name", MAC_BUNDLE_NAME 66 }; 67 68 private static void validateResult(String[] result) throws Exception { 69 if (result.length != 2) { 70 throw new AssertionError( 71 "Unexpected number of lines: " + result.length); 72 } 73 74 if (!result[0].trim().equals("jpackage test application")) { 75 throw new AssertionError("Unexpected result[0]: " + result[0]); 76 } 77 78 if (!result[1].trim().equals("args.length: 0")) { 79 throw new AssertionError("Unexpected result[1]: " + result[1]); 80 } 81 } 82 83 private static void validate() throws Exception { 84 int retVal = JPackageHelper.execute(null, app); 85 if (retVal != 0) { 86 throw new AssertionError( 87 "Test application exited with error: " + retVal); 88 } 89 90 File outfile = new File(appWorkingDir + File.separator + appOutput); 91 if (!outfile.exists()) { 92 throw new AssertionError(appOutput + " was not created"); 93 } 94 95 String output = Files.readString(outfile.toPath()); 96 String[] result = output.split("\n"); 97 validateResult(result); 98 } 99 100 private static void validateBundleName(String bundleName) throws Exception { 101 System.out.println("Validating bundleName: " + bundleName); 102 103 File infoPList = new File(OUTPUT + File.separator + APP_NAME + ".app" + 104 File.separator + "Contents" + File.separator + "Info.plist"); 105 106 DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance(); 107 dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 108 DocumentBuilder b = dbf.newDocumentBuilder(); 109 org.w3c.dom.Document doc = b.parse(new FileInputStream( 110 infoPList.getAbsolutePath())); 111 112 XPath xPath = XPathFactory.newInstance().newXPath(); 113 // Query for the value of <string> element preceding <key> element 114 // with value equal to CFBundleName 115 String v = (String)xPath.evaluate( 116 "//string[preceding-sibling::key = \"CFBundleName\"][1]", 117 doc, XPathConstants.STRING); 118 119 if (!v.equals(bundleName)) { 120 throw new AssertionError("Unexpected value of CFBundleName key: [" 121 + v + "]. Expected value: [" + bundleName + "]"); 122 } 123 } 124 125 private static void testCreateAppImage(String [] cmd, 126 String bundleName, 127 boolean validateApp) throws Exception { 128 JPackageHelper.executeCLI(true, cmd); 129 if (validateApp) { 130 validate(); 131 } 132 validateBundleName(bundleName); 133 } 134 135 private static void testCreateAppImageToolProvider(String [] cmd, 136 String bundleName, 137 boolean validateApp) throws Exception { 138 JPackageHelper.executeToolProvider(true, cmd); 139 if (validateApp) { 140 validate(); 141 } 142 validateBundleName(bundleName); 143 } 144 145 public static void main(String[] args) throws Exception { 146 JPackageHelper.createHelloImageJar(); 147 testCreateAppImage(CMD_1, APP_NAME, false); 148 JPackageHelper.deleteOutputFolder(OUTPUT); 149 testCreateAppImageToolProvider(CMD_1, APP_NAME, false); 150 JPackageHelper.deleteOutputFolder(OUTPUT); 151 testCreateAppImage(CMD_2, MAC_BUNDLE_NAME, true); 152 JPackageHelper.deleteOutputFolder(OUTPUT); 153 testCreateAppImageToolProvider(CMD_2, MAC_BUNDLE_NAME, true); 154 } 155 }