test/jdk/tools/jpackage/macosx/BundleIdentifierTest.java
branchJDK-8200758-branch
changeset 57446 5a5b85f00a63
parent 57445 405ddd80496e
child 58114 42df7de58e39
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 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         "--input", "input",
       
    53         "--output", OUTPUT,
       
    54         "--name", APP_NAME,
       
    55         "--main-jar", "hello.jar",
       
    56         "--main-class", MAIN_CLASS
       
    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", MAIN_CLASS,
       
    65         "--mac-bundle-identifier", MAC_BUNDLE_IDENTIFIER
       
    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(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 validateBundleIdentifier(String bundleIdentifier)
       
   101                                                               throws Exception {
       
   102         System.out.println("Validating bundleIdentifier: " + bundleIdentifier);
       
   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 CFBundleIdentifier
       
   116         String v = (String)xPath.evaluate(
       
   117                        "//string[preceding-sibling::key = \"CFBundleIdentifier\"][1]",
       
   118                        doc, XPathConstants.STRING);
       
   119 
       
   120         if (!v.equals(bundleIdentifier)) {
       
   121             throw new AssertionError("Unexpected value of CFBundleIdentifier key: ["
       
   122                                   + v + "]. Expected value: [" + bundleIdentifier + "]");
       
   123         }
       
   124     }
       
   125 
       
   126     private static void testCreateAppImage(String [] cmd,
       
   127                                          String bundleIdentifier,
       
   128                                          boolean validateApp) throws Exception {
       
   129         JPackageHelper.executeCLI(true, cmd);
       
   130         if (validateApp) {
       
   131             validate();
       
   132         }
       
   133         validateBundleIdentifier(bundleIdentifier);
       
   134     }
       
   135 
       
   136     private static void testCreateAppImageToolProvider(String [] cmd,
       
   137                                          String bundleIdentifier,
       
   138                                          boolean validateApp) throws Exception {
       
   139         JPackageHelper.executeToolProvider(true, cmd);
       
   140         if (validateApp) {
       
   141             validate();
       
   142         }
       
   143         validateBundleIdentifier(bundleIdentifier);
       
   144     }
       
   145 
       
   146     public static void main(String[] args) throws Exception {
       
   147         JPackageHelper.createHelloImageJar();
       
   148         testCreateAppImage(CMD_1, MAIN_CLASS, false);
       
   149         JPackageHelper.deleteOutputFolder(OUTPUT);
       
   150         testCreateAppImageToolProvider(CMD_1, MAIN_CLASS, false);
       
   151         JPackageHelper.deleteOutputFolder(OUTPUT);
       
   152         testCreateAppImage(CMD_2, MAC_BUNDLE_IDENTIFIER, true);
       
   153         JPackageHelper.deleteOutputFolder(OUTPUT);
       
   154         testCreateAppImageToolProvider(CMD_2, MAC_BUNDLE_IDENTIFIER, true);
       
   155     }
       
   156 }