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 --overwrite 30 * @library ../helpers 31 * @build JPackageHelper 32 * @build JPackagePath 33 * @modules jdk.jpackage 34 * @run main/othervm -Xmx512m JPackageCreateImageOverwriteTest 35 */ 36 public class JPackageCreateImageOverwriteTest { 37 private static final String app = JPackagePath.getApp(); 38 private static final String appOutput = JPackagePath.getAppOutputFile(); 39 private static final String appWorkingDir = JPackagePath.getAppWorkingDir(); 40 41 private static final String[] CMD = { 42 "create-image", 43 "--input", "input", 44 "--name", "test", 45 "--main-jar", "hello.jar", 46 "--main-class", "Hello", 47 "--files", "hello.jar", 48 "--output", "TBD"}; 49 50 private static final String[] CMD_OVERWRITE = { 51 "create-image", 52 "--input", "input", 53 "--name", "test", 54 "--main-jar", "hello.jar", 55 "--main-class", "Hello", 56 "--overwrite", 57 "--files", "hello.jar", 58 "--output", "TBD"}; 59 60 private static void validateResult(String[] result) throws Exception { 61 if (result.length != 2) { 62 throw new AssertionError( 63 "Unexpected number of lines: " + result.length); 64 } 65 66 if (!result[0].trim().equals("jpackage test application")) { 67 throw new AssertionError("Unexpected result[0]: " + result[0]); 68 } 69 70 if (!result[1].trim().equals("args.length: 0")) { 71 throw new AssertionError("Unexpected result[1]: " + result[1]); 72 } 73 } 74 75 private static void validate(String result) throws Exception { 76 if (!result.contains("java.io.IOException") && 77 !result.contains("already exists") && 78 !result.contains("--overwrite is not specified")) { 79 System.err.println(result); 80 throw new AssertionError("Unexpected error message"); 81 } 82 } 83 84 private static void validateOverwrite() throws Exception { 85 int retVal = JPackageHelper.execute(null, app); 86 if (retVal != 0) { 87 throw new AssertionError( 88 "Test application exited with error: " + retVal); 89 } 90 91 File outfile = new File(appWorkingDir + File.separator + appOutput); 92 if (!outfile.exists()) { 93 throw new AssertionError(appOutput + " was not created"); 94 } 95 96 String output = Files.readString(outfile.toPath()); 97 String[] result = output.split("\n"); 98 validateResult(result); 99 } 100 101 private static void testOverwrite() throws Exception { 102 CMD[CMD.length - 1] = "output"; 103 CMD_OVERWRITE[CMD_OVERWRITE.length - 1] = "output"; 104 105 String appFolder = "test"; 106 if (JPackageHelper.isOSX()) { 107 appFolder = "test.app"; 108 } 109 110 File output = new File("output" + File.separator + appFolder); 111 if (output.exists()) { 112 throw new AssertionError( 113 "Output folder already exist"); 114 } 115 output.mkdirs(); 116 117 String result = JPackageHelper.executeCLI(false, CMD); 118 validate(result); 119 120 JPackageHelper.executeCLI(true, CMD_OVERWRITE); 121 validateOverwrite(); 122 } 123 124 private static void testOverwriteToolProvider() throws Exception { 125 CMD[CMD.length - 1] = "outputToolProvider"; 126 CMD_OVERWRITE[CMD_OVERWRITE.length - 1] = "outputToolProvider"; 127 128 String appFolder = "test"; 129 if (JPackageHelper.isOSX()) { 130 appFolder = "test.app"; 131 } 132 133 File output = new File("outputToolProvider" + File.separator + appFolder); 134 if (output.exists()) { 135 throw new AssertionError( 136 "Output folder already exist"); 137 } 138 output.mkdirs(); 139 140 String result = JPackageHelper.executeToolProvider(false, CMD); 141 validate(result); 142 143 JPackageHelper.executeToolProvider(true, CMD_OVERWRITE); 144 validateOverwrite(); 145 } 146 147 public static void main(String[] args) throws Exception { 148 JPackageHelper.createHelloImageJar(); 149 testOverwrite(); 150 testOverwriteToolProvider(); 151 } 152 153 }