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