8224597: create automated tests for platform create-app-image options JDK-8200758-branch
authorherrick
Thu, 13 Jun 2019 19:34:44 -0400
branchJDK-8200758-branch
changeset 57405 539d8b3f9e1e
parent 57404 a477b26bf888
child 57406 0748948fcf26
child 57407 2c14fbeff1dc
8224597: create automated tests for platform create-app-image options Submitted-by: almatvee Reviewed-by: asemenyuk
test/jdk/tools/jpackage/createappimage/JPackageCreateAppImageBase.java
test/jdk/tools/jpackage/createappimage/macosx/JPackageCreateAppImageBundleIdentifierTest.java
test/jdk/tools/jpackage/createappimage/macosx/JPackageCreateAppImageBundleNameTest.java
test/jdk/tools/jpackage/createappimage/windows/JPackageCreateAppImageWinConsoleTest.java
test/jdk/tools/jpackage/helpers/JPackageHelper.java
--- a/test/jdk/tools/jpackage/createappimage/JPackageCreateAppImageBase.java	Thu Jun 13 19:32:24 2019 -0400
+++ b/test/jdk/tools/jpackage/createappimage/JPackageCreateAppImageBase.java	Thu Jun 13 19:34:44 2019 -0400
@@ -25,7 +25,6 @@
 import java.nio.file.Files;
 
 public abstract class JPackageCreateAppImageBase {
-    private static final String app = JPackagePath.getApp();
     private static final String appOutput = JPackagePath.getAppOutputFile();
     private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
 
@@ -44,7 +43,7 @@
         }
     }
 
-    private static void validate() throws Exception {
+    public static void validate(String app) throws Exception {
         int retVal = JPackageHelper.execute(null, app);
         if (retVal != 0) {
             throw new AssertionError(
@@ -63,11 +62,11 @@
 
     public static void testCreateAppImage(String [] cmd) throws Exception {
         JPackageHelper.executeCLI(true, cmd);
-        validate();
+        validate(JPackagePath.getApp());
     }
 
     public static void testCreateAppImageToolProvider(String [] cmd) throws Exception {
         JPackageHelper.executeToolProvider(true, cmd);
-        validate();
+        validate(JPackagePath.getApp());
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackage/createappimage/macosx/JPackageCreateAppImageBundleIdentifierTest.java	Thu Jun 13 19:34:44 2019 -0400
@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.nio.file.Files;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+
+/*
+ * @test
+ * @summary jpackage create image bundle identifier test
+ * @library ../../helpers
+ * @build JPackageHelper
+ * @build JPackagePath
+ * @modules jdk.jpackage
+ * @requires (os.family == "mac")
+ * @run main/othervm -Xmx512m JPackageCreateAppImageBundleIdentifierTest
+ */
+public class JPackageCreateAppImageBundleIdentifierTest {
+    private static final String OUTPUT = "output";
+    private static final String app = JPackagePath.getApp();
+    private static final String appOutput = JPackagePath.getAppOutputFile();
+    private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
+    private static final String MAC_BUNDLE_IDENTIFIER = "TestBundleIdentifier";
+    private static final String APP_NAME = "test";
+    private static final String MAIN_CLASS = "Hello";
+
+    private static final String [] CMD_1 = {
+        "create-app-image",
+        "--input", "input",
+        "--output", OUTPUT,
+        "--name", APP_NAME,
+        "--main-jar", "hello.jar",
+        "--main-class", MAIN_CLASS
+    };
+
+    private static final String [] CMD_2 = {
+        "create-app-image",
+        "--input", "input",
+        "--output", OUTPUT,
+        "--name", APP_NAME,
+        "--main-jar", "hello.jar",
+        "--main-class", MAIN_CLASS,
+        "--mac-bundle-identifier", MAC_BUNDLE_IDENTIFIER
+    };
+
+    private static void validateResult(String[] result) throws Exception {
+        if (result.length != 2) {
+            throw new AssertionError(
+                   "Unexpected number of lines: " + result.length);
+        }
+
+        if (!result[0].trim().equals("jpackage test application")) {
+            throw new AssertionError("Unexpected result[0]: " + result[0]);
+        }
+
+        if (!result[1].trim().equals("args.length: 0")) {
+            throw new AssertionError("Unexpected result[1]: " + result[1]);
+        }
+    }
+
+    private static void validate() throws Exception {
+        int retVal = JPackageHelper.execute(null, app);
+        if (retVal != 0) {
+            throw new AssertionError(
+                   "Test application exited with error: " + retVal);
+        }
+
+        File outfile = new File(appWorkingDir + File.separator + appOutput);
+        if (!outfile.exists()) {
+            throw new AssertionError(appOutput + " was not created");
+        }
+
+        String output = Files.readString(outfile.toPath());
+        String[] result = output.split("\n");
+        validateResult(result);
+    }
+
+    private static void validateBundleIdentifier(String bundleIdentifier)
+                                                              throws Exception {
+        System.out.println("Validating bundleIdentifier: " + bundleIdentifier);
+
+        File infoPList = new File(OUTPUT + File.separator + APP_NAME + ".app" +
+                File.separator + "Contents" + File.separator + "Info.plist");
+
+        DocumentBuilder b = DocumentBuilderFactory.newDefaultInstance()
+                                                         .newDocumentBuilder();
+        org.w3c.dom.Document doc = b.parse(new FileInputStream(
+                                                  infoPList.getAbsolutePath()));
+
+        XPath xPath = XPathFactory.newInstance().newXPath();
+        // Query for the value of <string> element preceding <key> element
+        // with value equal to CFBundleIdentifier
+        String v = (String)xPath.evaluate(
+                       "//string[preceding-sibling::key = \"CFBundleIdentifier\"][1]",
+                       doc, XPathConstants.STRING);
+
+        if (!v.equals(bundleIdentifier)) {
+            throw new AssertionError("Unexpected value of CFBundleIdentifier key: ["
+                                  + v + "]. Expected value: [" + bundleIdentifier + "]");
+        }
+    }
+
+    private static void testCreateAppImage(String [] cmd,
+                                         String bundleIdentifier,
+                                         boolean validateApp) throws Exception {
+        JPackageHelper.executeCLI(true, cmd);
+        if (validateApp) {
+            validate();
+        }
+        validateBundleIdentifier(bundleIdentifier);
+    }
+
+    private static void testCreateAppImageToolProvider(String [] cmd,
+                                         String bundleIdentifier,
+                                         boolean validateApp) throws Exception {
+        JPackageHelper.executeToolProvider(true, cmd);
+        if (validateApp) {
+            validate();
+        }
+        validateBundleIdentifier(bundleIdentifier);
+    }
+
+    public static void main(String[] args) throws Exception {
+        JPackageHelper.createHelloImageJar();
+        testCreateAppImage(CMD_1, MAIN_CLASS, false);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        testCreateAppImageToolProvider(CMD_1, MAIN_CLASS, false);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        testCreateAppImage(CMD_2, MAC_BUNDLE_IDENTIFIER, true);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        testCreateAppImageToolProvider(CMD_2, MAC_BUNDLE_IDENTIFIER, true);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackage/createappimage/macosx/JPackageCreateAppImageBundleNameTest.java	Thu Jun 13 19:34:44 2019 -0400
@@ -0,0 +1,156 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.nio.file.Files;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+
+/*
+ * @test
+ * @summary jpackage create image bundle name test
+ * @library ../../helpers
+ * @build JPackageHelper
+ * @build JPackagePath
+ * @modules jdk.jpackage
+ * @requires (os.family == "mac")
+ * @run main/othervm -Xmx512m JPackageCreateAppImageBundleNameTest
+ */
+public class JPackageCreateAppImageBundleNameTest {
+    private static final String OUTPUT = "output";
+    private static final String app = JPackagePath.getApp();
+    private static final String appOutput = JPackagePath.getAppOutputFile();
+    private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
+    private static final String MAC_BUNDLE_NAME = "TestBundleName";
+    private static final String APP_NAME = "test";
+
+    private static final String [] CMD_1 = {
+        "create-app-image",
+        "--input", "input",
+        "--output", OUTPUT,
+        "--name", APP_NAME,
+        "--main-jar", "hello.jar",
+        "--main-class", "Hello"
+    };
+
+    private static final String [] CMD_2 = {
+        "create-app-image",
+        "--input", "input",
+        "--output", OUTPUT,
+        "--name", APP_NAME,
+        "--main-jar", "hello.jar",
+        "--main-class", "Hello",
+        "--mac-bundle-name", MAC_BUNDLE_NAME
+    };
+
+    private static void validateResult(String[] result) throws Exception {
+        if (result.length != 2) {
+            throw new AssertionError(
+                   "Unexpected number of lines: " + result.length);
+        }
+
+        if (!result[0].trim().equals("jpackage test application")) {
+            throw new AssertionError("Unexpected result[0]: " + result[0]);
+        }
+
+        if (!result[1].trim().equals("args.length: 0")) {
+            throw new AssertionError("Unexpected result[1]: " + result[1]);
+        }
+    }
+
+    private static void validate() throws Exception {
+        int retVal = JPackageHelper.execute(null, app);
+        if (retVal != 0) {
+            throw new AssertionError(
+                   "Test application exited with error: " + retVal);
+        }
+
+        File outfile = new File(appWorkingDir + File.separator + appOutput);
+        if (!outfile.exists()) {
+            throw new AssertionError(appOutput + " was not created");
+        }
+
+        String output = Files.readString(outfile.toPath());
+        String[] result = output.split("\n");
+        validateResult(result);
+    }
+
+    private static void validateBundleName(String bundleName) throws Exception {
+        System.out.println("Validating bundleName: " + bundleName);
+
+        File infoPList = new File(OUTPUT + File.separator + APP_NAME + ".app" +
+                File.separator + "Contents" + File.separator + "Info.plist");
+
+        DocumentBuilder b = DocumentBuilderFactory.newDefaultInstance()
+                                                         .newDocumentBuilder();
+        org.w3c.dom.Document doc = b.parse(new FileInputStream(
+                                                  infoPList.getAbsolutePath()));
+
+        XPath xPath = XPathFactory.newInstance().newXPath();
+        // Query for the value of <string> element preceding <key> element
+        // with value equal to CFBundleName
+        String v = (String)xPath.evaluate(
+                       "//string[preceding-sibling::key = \"CFBundleName\"][1]",
+                       doc, XPathConstants.STRING);
+
+        if (!v.equals(bundleName)) {
+            throw new AssertionError("Unexpected value of CFBundleName key: ["
+                                  + v + "]. Expected value: [" + bundleName + "]");
+        }
+    }
+
+    private static void testCreateAppImage(String [] cmd,
+                                         String bundleName,
+                                         boolean validateApp) throws Exception {
+        JPackageHelper.executeCLI(true, cmd);
+        if (validateApp) {
+            validate();
+        }
+        validateBundleName(bundleName);
+    }
+
+    private static void testCreateAppImageToolProvider(String [] cmd,
+                                         String bundleName,
+                                         boolean validateApp) throws Exception {
+        JPackageHelper.executeToolProvider(true, cmd);
+        if (validateApp) {
+            validate();
+        }
+        validateBundleName(bundleName);
+    }
+
+    public static void main(String[] args) throws Exception {
+        JPackageHelper.createHelloImageJar();
+        testCreateAppImage(CMD_1, APP_NAME, false);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        testCreateAppImageToolProvider(CMD_1, APP_NAME, false);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        testCreateAppImage(CMD_2, MAC_BUNDLE_NAME, true);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        testCreateAppImageToolProvider(CMD_2, MAC_BUNDLE_NAME, true);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackage/createappimage/windows/JPackageCreateAppImageWinConsoleTest.java	Thu Jun 13 19:34:44 2019 -0400
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.IOException;
+import java.io.File;
+import java.io.InputStream;
+import java.io.FileInputStream;
+
+/*
+ * @test
+ * @summary jpackage create image win console test
+ * @library ../../helpers
+ * @library ../
+ * @build JPackageHelper
+ * @build JPackagePath
+ * @build JPackageCreateAppImageBase
+ * @requires (os.family == "windows")
+ * @modules jdk.jpackage
+ * @run main/othervm -Xmx512m JPackageCreateAppImageWinConsoleTest
+ */
+
+public class JPackageCreateAppImageWinConsoleTest {
+    private static final String NAME = "test";
+    private static final String OUTPUT = "output";
+    private static final String OUTPUT_WIN_CONSOLE = "outputWinConsole";
+    private static final int BUFFER_SIZE = 512;
+    private static final int GUI_SUBSYSTEM = 2;
+    private static final int CONSOLE_SUBSYSTEM = 3;
+
+    private static final String [] CMD = {
+        "create-app-image",
+        "--input", "input",
+        "--output", OUTPUT,
+        "--name", NAME,
+        "--main-jar", "hello.jar",
+        "--main-class", "Hello",
+    };
+
+    private static final String [] CMD_WIN_CONSOLE = {
+        "create-app-image",
+        "--input", "input",
+        "--output", OUTPUT_WIN_CONSOLE,
+        "--name", NAME,
+        "--main-jar", "hello.jar",
+        "--main-class", "Hello",
+        "--win-console"
+    };
+
+    private static void checkSubsystem(boolean console) throws Exception {
+        String file = console ? OUTPUT_WIN_CONSOLE : OUTPUT;
+        file += File.separator;
+        file += NAME;
+        file += File.separator;
+        file += NAME + ".exe";
+
+        JPackageCreateAppImageBase.validate(file);
+
+        try (InputStream inputStream = new FileInputStream(file)) {
+            byte [] bytes = new byte[BUFFER_SIZE];
+            if (inputStream.read(bytes) != BUFFER_SIZE) {
+                throw new AssertionError("Wrong number of bytes read");
+            }
+
+            // Check PE header for console or Win GUI app.
+            // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_nt_headers
+            for (int i = 0;  i < (bytes.length - 4); i++) {
+                if (bytes[i] == 0x50 && bytes[i + 1] == 0x45 &&
+                        bytes[i + 2] == 0x0 && bytes[i + 3] == 0x0) {
+                    i = i + 4 + 20 + 68; // Signature, File Header and subsystem offset.
+                    byte subsystem = bytes[i];
+                    if (console) {
+                        if (subsystem != CONSOLE_SUBSYSTEM) {
+                            throw new AssertionError("Unexpected subsystem: " + subsystem);
+                        } else {
+                            return; // done
+                        }
+                    } else {
+                        if (subsystem != GUI_SUBSYSTEM) {
+                            throw new AssertionError("Unexpected subsystem: " + subsystem);
+                        } else {
+                            return; // done
+                        }
+                    }
+                }
+            }
+        }
+
+        throw new AssertionError("Unable to verify PE header");
+    }
+
+    private static void validate() throws Exception {
+        checkSubsystem(false);
+        checkSubsystem(true);
+    }
+
+    private static void testCreateAppImage(String [] cmd) throws Exception {
+        JPackageHelper.executeCLI(true, cmd);
+    }
+
+    private static void testCreateAppImageToolProvider(String [] cmd) throws Exception {
+        JPackageHelper.executeToolProvider(true, cmd);
+    }
+
+    public static void main(String[] args) throws Exception {
+        JPackageHelper.createHelloImageJar();
+        testCreateAppImage(CMD);
+        testCreateAppImage(CMD_WIN_CONSOLE);
+        validate();
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        JPackageHelper.deleteOutputFolder(OUTPUT_WIN_CONSOLE);
+        testCreateAppImageToolProvider(CMD);
+        testCreateAppImageToolProvider(CMD_WIN_CONSOLE);
+        validate();
+    }
+}
--- a/test/jdk/tools/jpackage/helpers/JPackageHelper.java	Thu Jun 13 19:32:24 2019 -0400
+++ b/test/jdk/tools/jpackage/helpers/JPackageHelper.java	Thu Jun 13 19:34:44 2019 -0400
@@ -25,7 +25,6 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.io.FileWriter;
 import java.io.BufferedWriter;
 import java.nio.file.FileVisitResult;
 
@@ -198,7 +197,7 @@
 
     public static void deleteOutputFolder(String output) throws IOException {
         File outputFolder = new File(output);
-        System.out.println("AMDEBUG output: " + outputFolder.getAbsolutePath());
+        System.out.println("deleteOutputFolder: " + outputFolder.getAbsolutePath());
         try {
             deleteRecursive(outputFolder);
         } catch (IOException ioe) {
@@ -312,7 +311,7 @@
         Path src = Path.of(TEST_SRC_ROOT + File.separator + "apps"
                 + File.separator + testType + File.separator + name + ".java");
         Path dst = Path.of(name + ".java");
-        
+
         if (dst.toFile().exists()) {
             Files.delete(dst);
         }
@@ -628,5 +627,4 @@
 
         return in;
     }
-
 }