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.nio.file.Files;
  26 
  27 /*
  28  * @test
  29  * @summary jpackage create image to verify --icon
  30  * @library ../helpers
  31  * @build JPackageHelper
  32  * @build JPackagePath
  33  * @modules jdk.jpackage
  34  * @run main/othervm -Xmx512m JPackageCreateAppImageIconTest
  35  */
  36 public class JPackageCreateAppImageIconTest {
  37     private static final String OUTPUT = "output";
  38     private static final String app = JPackagePath.getApp();
  39     private static final String appOutput = JPackagePath.getAppOutputFile();
  40     private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
  41 
  42     private static final String[] CMD = {
  43         "create-app-image",
  44         "--input", "input",
  45         "--name", "test",
  46         "--main-jar", "hello.jar",
  47         "--main-class", "Hello",
  48         "--icon", getIconPath(),
  49         "--output", OUTPUT};
  50 
  51     private static void validateResult(String[] result) throws Exception {
  52         if (result.length != 2) {
  53             throw new AssertionError(
  54                    "Unexpected number of lines: " + result.length);
  55         }
  56 
  57         if (!result[0].trim().equals("jpackage test application")) {
  58             throw new AssertionError("Unexpected result[0]: " + result[0]);
  59         }
  60 
  61         if (!result[1].trim().equals("args.length: 0")) {
  62             throw new AssertionError("Unexpected result[1]: " + result[1]);
  63         }
  64     }
  65 
  66     private static void validate() throws Exception {
  67         int retVal = JPackageHelper.execute(null, app);
  68         if (retVal != 0) {
  69             throw new AssertionError(
  70                    "Test application exited with error: " + retVal);
  71         }
  72 
  73         File outfile = new File(appWorkingDir + File.separator + appOutput);
  74         if (!outfile.exists()) {
  75             throw new AssertionError(appOutput + " was not created");
  76         }
  77 
  78         String output = Files.readString(outfile.toPath());
  79         String[] result = output.split("\n");
  80         validateResult(result);
  81     }
  82 
  83     private static void validateIcon() throws Exception {
  84         File origIcon = new File(getIconPath());
  85         File icon = new File(JPackagePath.getAppIcon());
  86         if (origIcon.length() != icon.length()) {
  87             System.err.println("origIcon.length(): " + origIcon.length());
  88             System.err.println("icon.length(): " + icon.length());
  89             throw new AssertionError("Icons size does not match");
  90         }
  91     }
  92 
  93     private static void testIcon() throws Exception {
  94         JPackageHelper.executeCLI(true, CMD);
  95         validate();
  96         validateIcon();
  97     }
  98 
  99     private static void testIconToolProvider() throws Exception {
 100         JPackageHelper.deleteOutputFolder(OUTPUT);
 101         JPackageHelper.executeToolProvider(true, CMD);
 102         validate();
 103         validateIcon();
 104     }
 105 
 106     private static String getIconPath() {
 107         String ext = ".ico";
 108         if (JPackageHelper.isOSX()) {
 109             ext = ".icns";
 110         } else if (JPackageHelper.isLinux()) {
 111             ext = ".png";
 112         }
 113 
 114         String path = JPackagePath.getTestSrcRoot() + File.separator + "resources"
 115                 + File.separator + "icon" + ext;
 116 
 117         return path;
 118     }
 119 
 120     public static void main(String[] args) throws Exception {
 121         JPackageHelper.createHelloImageJar();
 122         testIcon();
 123         testIconToolProvider();
 124     }
 125 
 126 }