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