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