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  /*
  25  * @test
  26  * @summary jpackage create image missing arguments test
  27  * @library helpers
  28  * @build JPackageHelper
  29  * @build JPackagePath
  30  * @modules jdk.jpackage
  31  * @run main/othervm -Xmx512m JPackageMissingArgumentsTest
  32  */
  33 
  34 public class JPackageMissingArgumentsTest {
  35     private static final String [] RESULT_1 = {"--output"};
  36     private static final String [] CMD_1 = {
  37         "create-image",
  38         "--input", "input",
  39         "--name", "test",
  40         "--main-jar", "hello.jar",
  41         "--class", "Hello",
  42         "--force",
  43         "--files", "hello.jar"};
  44 
  45     private static final String [] RESULT_2 = {"--input"};
  46     private static final String [] CMD_2 = {
  47         "create-image",
  48         "--output", "output",
  49         "--name", "test",
  50         "--main-jar", "hello.jar",
  51         "--class", "Hello",
  52         "--force",
  53         "--files", "hello.jar"};
  54 
  55     private static final String [] RESULT_3 = {"--input", "--app-image"};
  56     private static final String [] CMD_3 = {
  57         "create-installer",
  58         "--output", "output",
  59         "--name", "test",
  60         "--main-jar", "hello.jar",
  61         "--class", "Hello",
  62         "--force",
  63         "--files", "hello.jar"};
  64 
  65     private static final String [] RESULT_4 = {"--class"};
  66     private static final String [] CMD_4 = {
  67         "create-image",
  68         "--input", "input",
  69         "--output", "output",
  70         "--name", "test",
  71         "--main-jar", "hello.jar",
  72         "--force",
  73         "--files", "hello.jar"};
  74 
  75     private static final String [] RESULT_5 = {"--main-jar"};
  76     private static final String [] CMD_5 = {
  77         "create-image",
  78         "--input", "input",
  79         "--output", "output",
  80         "--name", "test",
  81         "--class", "Hello",
  82         "--force",
  83         "--files", "hello.jar"};
  84 
  85     private static final String [] RESULT_6 = {"--module-path", "--runtime-image"};
  86     private static final String [] CMD_6 = {
  87         "create-image",
  88         "--output", "output",
  89         "--name", "test",
  90         "--module", "com.hello/com.hello.Hello",
  91         "--force",
  92         "--files", "hello.jar"};
  93 
  94     private static final String [] RESULT_7 = {"--module-path", "--runtime-image",
  95                                                "--app-image"};
  96     private static final String [] CMD_7 = {
  97         "create-installer",
  98         "--output", "output",
  99         "--name", "test",
 100         "--module", "com.hello/com.hello.Hello",
 101         "--force",
 102         "--files", "hello.jar"};
 103 
 104     private static void validate(String output, String [] expected)
 105            throws Exception {
 106         String[] result = output.split("\n");
 107         if (result.length != 1) {
 108             System.err.println(output);
 109             throw new AssertionError("Invalid number of lines in output: "
 110                     + result.length);
 111         }
 112 
 113         for (String s : expected) {
 114             if (!result[0].contains(s)) {
 115                 System.err.println("Expected to contain: " + s);
 116                 System.err.println("Actual: " + result[0]);
 117                 throw new AssertionError("Unexpected error message");
 118             }
 119         }
 120     }
 121 
 122     private static void testMissingArg() throws Exception {
 123         String output = JPackageHelper.executeCLI(false, CMD_1);
 124         validate(output, RESULT_1);
 125 
 126         output = JPackageHelper.executeCLI(false, CMD_2);
 127         validate(output, RESULT_2);
 128 
 129         output = JPackageHelper.executeCLI(false, CMD_3);
 130         validate(output, RESULT_3);
 131 
 132         output = JPackageHelper.executeCLI(false, CMD_4);
 133         validate(output, RESULT_4);
 134 
 135         output = JPackageHelper.executeCLI(false, CMD_5);
 136         validate(output, RESULT_5);
 137 
 138         output = JPackageHelper.executeCLI(false, CMD_6);
 139         validate(output, RESULT_6);
 140 
 141         output = JPackageHelper.executeCLI(false, CMD_7);
 142         validate(output, RESULT_7);
 143     }
 144 
 145     private static void testMissingArgToolProvider() throws Exception {
 146         String output = JPackageHelper.executeToolProvider(false, CMD_1);
 147         validate(output, RESULT_1);
 148 
 149         output = JPackageHelper.executeToolProvider(false, CMD_2);
 150         validate(output, RESULT_2);
 151 
 152         output = JPackageHelper.executeToolProvider(false, CMD_3);
 153         validate(output, RESULT_3);
 154 
 155         output = JPackageHelper.executeToolProvider(false, CMD_4);
 156         validate(output, RESULT_4);
 157 
 158         output = JPackageHelper.executeToolProvider(false, CMD_5);
 159         validate(output, RESULT_5);
 160 
 161         output = JPackageHelper.executeToolProvider(false, CMD_6);
 162         validate(output, RESULT_6);
 163 
 164         output = JPackageHelper.executeToolProvider(false, CMD_7);
 165         validate(output, RESULT_7);
 166     }
 167 
 168     public static void main(String[] args) throws Exception {
 169         JPackageHelper.createHelloImageJar();
 170         testMissingArg();
 171         testMissingArgToolProvider();
 172     }
 173 
 174 }