test/jdk/tools/jpackage/macosx/BundleIdentifierTest.java
author herrick
Wed, 11 Sep 2019 13:26:36 -0400
branchJDK-8200758-branch
changeset 58115 4a27283b542d
parent 58114 42df7de58e39
child 58147 45a9084fe981
permissions -rw-r--r--
/home/aherrick/comment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     1
/*
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     4
 *
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     8
 *
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    13
 * accompanied this code).
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    14
 *
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    18
 *
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    21
 * questions.
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    22
 */
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    23
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    24
import java.io.File;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    25
import java.io.FileInputStream;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    26
import java.nio.file.Files;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    27
import javax.xml.parsers.DocumentBuilder;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    28
import javax.xml.parsers.DocumentBuilderFactory;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    29
import javax.xml.xpath.XPath;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    30
import javax.xml.xpath.XPathConstants;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    31
import javax.xml.xpath.XPathFactory;
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    32
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    33
/*
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    34
 * @test
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    35
 * @summary jpackage create image bundle identifier test
57446
5a5b85f00a63 8226891: rename and re-layout jpackage tests.
herrick
parents: 57445
diff changeset
    36
 * @library ../helpers
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    37
 * @build JPackageHelper
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    38
 * @build JPackagePath
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    39
 * @modules jdk.jpackage
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    40
 * @requires (os.family == "mac")
57446
5a5b85f00a63 8226891: rename and re-layout jpackage tests.
herrick
parents: 57445
diff changeset
    41
 * @run main/othervm -Xmx512m BundleIdentifierTest
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    42
 */
57446
5a5b85f00a63 8226891: rename and re-layout jpackage tests.
herrick
parents: 57445
diff changeset
    43
public class BundleIdentifierTest {
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    44
    private static final String OUTPUT = "output";
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    45
    private static final String app = JPackagePath.getApp();
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    46
    private static final String appOutput = JPackagePath.getAppOutputFile();
58115
4a27283b542d /home/aherrick/comment
herrick
parents: 58114
diff changeset
    47
    private static final String MAC_PACKAGE_IDENTIFIER = "TestBundleIdentifier";
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    48
    private static final String APP_NAME = "test";
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    49
    private static final String MAIN_CLASS = "Hello";
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    50
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    51
    private static final String [] CMD_1 = {
58114
42df7de58e39 8230519: jpackage "--package-type" values and default
herrick
parents: 57446
diff changeset
    52
        "--package-type", "app-image",
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    53
        "--input", "input",
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    54
        "--output", OUTPUT,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    55
        "--name", APP_NAME,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    56
        "--main-jar", "hello.jar",
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    57
        "--main-class", MAIN_CLASS
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    58
    };
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    59
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    60
    private static final String [] CMD_2 = {
58114
42df7de58e39 8230519: jpackage "--package-type" values and default
herrick
parents: 57446
diff changeset
    61
        "--package-type", "app-image",
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    62
        "--input", "input",
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    63
        "--output", OUTPUT,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    64
        "--name", APP_NAME,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    65
        "--main-jar", "hello.jar",
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    66
        "--main-class", MAIN_CLASS,
58115
4a27283b542d /home/aherrick/comment
herrick
parents: 58114
diff changeset
    67
        "--mac-package-identifier", MAC_PACKAGE_IDENTIFIER
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    68
    };
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    69
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    70
    private static void validateResult(String[] result) throws Exception {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    71
        if (result.length != 2) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    72
            throw new AssertionError(
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    73
                   "Unexpected number of lines: " + result.length);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    74
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    75
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    76
        if (!result[0].trim().equals("jpackage test application")) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    77
            throw new AssertionError("Unexpected result[0]: " + result[0]);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    78
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    79
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    80
        if (!result[1].trim().equals("args.length: 0")) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    81
            throw new AssertionError("Unexpected result[1]: " + result[1]);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    82
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    83
    }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    84
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    85
    private static void validate() throws Exception {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    86
        int retVal = JPackageHelper.execute(null, app);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    87
        if (retVal != 0) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    88
            throw new AssertionError(
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    89
                   "Test application exited with error: " + retVal);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    90
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    91
57445
405ddd80496e 8226904: current working directory wrong running jpackage app
herrick
parents: 57414
diff changeset
    92
        File outfile = new File(appOutput);
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    93
        if (!outfile.exists()) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    94
            throw new AssertionError(appOutput + " was not created");
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    95
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    96
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    97
        String output = Files.readString(outfile.toPath());
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    98
        String[] result = output.split("\n");
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
    99
        validateResult(result);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   100
    }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   101
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   102
    private static void validateBundleIdentifier(String bundleIdentifier)
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   103
                                                              throws Exception {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   104
        System.out.println("Validating bundleIdentifier: " + bundleIdentifier);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   105
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   106
        File infoPList = new File(OUTPUT + File.separator + APP_NAME + ".app" +
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   107
                File.separator + "Contents" + File.separator + "Info.plist");
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   108
57408
f4a3f1dd2c02 8226193: BundleNameTest and BundleIdentifierTest fails if run without network connection
herrick
parents: 57405
diff changeset
   109
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance();
f4a3f1dd2c02 8226193: BundleNameTest and BundleIdentifierTest fails if run without network connection
herrick
parents: 57405
diff changeset
   110
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
f4a3f1dd2c02 8226193: BundleNameTest and BundleIdentifierTest fails if run without network connection
herrick
parents: 57405
diff changeset
   111
        DocumentBuilder b = dbf.newDocumentBuilder();
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   112
        org.w3c.dom.Document doc = b.parse(new FileInputStream(
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   113
                                                  infoPList.getAbsolutePath()));
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   114
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   115
        XPath xPath = XPathFactory.newInstance().newXPath();
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   116
        // Query for the value of <string> element preceding <key> element
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   117
        // with value equal to CFBundleIdentifier
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   118
        String v = (String)xPath.evaluate(
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   119
                       "//string[preceding-sibling::key = \"CFBundleIdentifier\"][1]",
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   120
                       doc, XPathConstants.STRING);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   121
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   122
        if (!v.equals(bundleIdentifier)) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   123
            throw new AssertionError("Unexpected value of CFBundleIdentifier key: ["
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   124
                                  + v + "]. Expected value: [" + bundleIdentifier + "]");
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   125
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   126
    }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   127
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   128
    private static void testCreateAppImage(String [] cmd,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   129
                                         String bundleIdentifier,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   130
                                         boolean validateApp) throws Exception {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   131
        JPackageHelper.executeCLI(true, cmd);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   132
        if (validateApp) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   133
            validate();
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   134
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   135
        validateBundleIdentifier(bundleIdentifier);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   136
    }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   137
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   138
    private static void testCreateAppImageToolProvider(String [] cmd,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   139
                                         String bundleIdentifier,
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   140
                                         boolean validateApp) throws Exception {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   141
        JPackageHelper.executeToolProvider(true, cmd);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   142
        if (validateApp) {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   143
            validate();
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   144
        }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   145
        validateBundleIdentifier(bundleIdentifier);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   146
    }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   147
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   148
    public static void main(String[] args) throws Exception {
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   149
        JPackageHelper.createHelloImageJar();
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   150
        testCreateAppImage(CMD_1, MAIN_CLASS, false);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   151
        JPackageHelper.deleteOutputFolder(OUTPUT);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   152
        testCreateAppImageToolProvider(CMD_1, MAIN_CLASS, false);
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   153
        JPackageHelper.deleteOutputFolder(OUTPUT);
58115
4a27283b542d /home/aherrick/comment
herrick
parents: 58114
diff changeset
   154
        testCreateAppImage(CMD_2, MAC_PACKAGE_IDENTIFIER, true);
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   155
        JPackageHelper.deleteOutputFolder(OUTPUT);
58115
4a27283b542d /home/aherrick/comment
herrick
parents: 58114
diff changeset
   156
        testCreateAppImageToolProvider(CMD_2, MAC_PACKAGE_IDENTIFIER, true);
57405
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   157
    }
539d8b3f9e1e 8224597: create automated tests for platform create-app-image options
herrick
parents:
diff changeset
   158
}