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