1 2 import java.io.File; 3 import java.nio.file.Files; 4 5 /* 6 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8 * 9 * This code is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License version 2 only, as 11 * published by the Free Software Foundation. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 28 /* 29 * @test 30 * @summary jpackage create image --app-version test 31 * @library ../helpers 32 * @build JPackageHelper 33 * @build JPackagePath 34 * @modules jdk.jpackage 35 * @run main/othervm -Xmx512m JPackageCreateImageVersionTest 36 */ 37 public class JPackageCreateImageVersionTest { 38 private static final String appCfg = JPackagePath.getAppCfg(); 39 private static final String VERSION = "2.3"; 40 private static final String VERSION_DEFAULT = "1.0"; 41 42 private static final String[] CMD = { 43 "create-image", 44 "--input", "input", 45 "--output", "output", 46 "--name", "test", 47 "--main-jar", "hello.jar", 48 "--main-class", "Hello", 49 "--overwrite", 50 "--files", "hello.jar"}; 51 52 private static final String[] CMD_VERSION = { 53 "create-image", 54 "--input", "input", 55 "--output", "output", 56 "--name", "test", 57 "--main-jar", "hello.jar", 58 "--main-class", "Hello", 59 "--files", "hello.jar", 60 "--overwrite", 61 "--app-version", VERSION}; 62 63 private static void validate(String version) 64 throws Exception { 65 File outfile = new File(appCfg); 66 if (!outfile.exists()) { 67 throw new AssertionError(appCfg + " was not created"); 68 } 69 70 String output = Files.readString(outfile.toPath()); 71 if (version == null) { 72 version = VERSION_DEFAULT; 73 } 74 75 String expected = "app.version=" + version; 76 if (!output.contains(expected)) { 77 System.err.println("Expected: " + expected); 78 throw new AssertionError("Cannot find expected entry in config file"); 79 } 80 } 81 82 private static void testVersion() throws Exception { 83 JPackageHelper.executeCLI(true, CMD); 84 validate(null); 85 JPackageHelper.executeCLI(true, CMD_VERSION); 86 validate(VERSION); 87 } 88 89 private static void testVersionToolProvider() throws Exception { 90 JPackageHelper.executeToolProvider(true, CMD); 91 validate(null); 92 JPackageHelper.executeToolProvider(true, CMD_VERSION); 93 validate(VERSION); 94 } 95 96 public static void main(String[] args) throws Exception { 97 JPackageHelper.createHelloImageJar(); 98 testVersion(); 99 testVersionToolProvider(); 100 } 101 102 }