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