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.incubator.jpackage
  31  * @run main/othervm -Xmx512m MissingArgumentsTest
  32  */
  33 
  34 public class MissingArgumentsTest {
  35     private static final String [] RESULT_1 = {"--input"};
  36     private static final String [] CMD_1 = {
  37         "--type", "app-image",
  38         "--dest", "output",
  39         "--name", "test",
  40         "--main-jar", "hello.jar",
  41         "--main-class", "Hello",
  42     };
  43 
  44     private static final String [] RESULT_2 = {"--input", "--app-image"};
  45     private static final String [] CMD_2 = {
  46         "--type", "app-image",
  47         "--type", "invalid-type",
  48         "--dest", "output",
  49         "--name", "test",
  50         "--main-jar", "hello.jar",
  51         "--main-class", "Hello",
  52     };
  53 
  54     private static final String [] RESULT_3 = {"main class was not specified"};
  55     private static final String [] CMD_3 = {
  56         "--type", "app-image",
  57         "--input", "input",
  58         "--dest", "output",
  59         "--name", "test",
  60         "--main-jar", "hello.jar",
  61     };
  62 
  63     private static final String [] RESULT_4 = {"--main-jar"};
  64     private static final String [] CMD_4 = {
  65         "--type", "app-image",
  66         "--input", "input",
  67         "--dest", "output",
  68         "--name", "test",
  69         "--main-class", "Hello",
  70     };
  71 
  72     private static final String [] RESULT_5 = {"--module-path", "--runtime-image"};
  73     private static final String [] CMD_5 = {
  74         "--type", "app-image",
  75         "--dest", "output",
  76         "--name", "test",
  77         "--module", "com.hello/com.hello.Hello",
  78     };
  79 
  80     private static final String [] RESULT_6 = {"--module-path", "--runtime-image",
  81                                                "--app-image"};
  82     private static final String [] CMD_6 = {
  83         "--type", "invalid-type",
  84         "--dest", "output",
  85         "--name", "test",
  86         "--module", "com.hello/com.hello.Hello",
  87     };
  88 
  89     private static void validate(String output, String [] expected,
  90            boolean single) throws Exception {
  91         String[] result = JPackageHelper.splitAndFilter(output);
  92         if (single && result.length != 1) {
  93             System.err.println(output);
  94             throw new AssertionError("Invalid number of lines in output: "
  95                     + result.length);
  96         }
  97 
  98         for (String s : expected) {
  99             if (!result[0].contains(s)) {
 100                 System.err.println("Expected to contain: " + s);
 101                 System.err.println("Actual: " + result[0]);
 102                 throw new AssertionError("Unexpected error message");
 103             }
 104         }
 105     }
 106 
 107     private static void testMissingArg() throws Exception {
 108         String output = JPackageHelper.executeCLI(false, CMD_1);
 109         validate(output, RESULT_1, true);
 110 
 111         output = JPackageHelper.executeCLI(false, CMD_2);
 112         validate(output, RESULT_2, true);
 113 
 114         output = JPackageHelper.executeCLI(false, CMD_3);
 115         validate(output, RESULT_3, false);
 116 
 117         output = JPackageHelper.executeCLI(false, CMD_4);
 118         validate(output, RESULT_4, true);
 119 
 120         output = JPackageHelper.executeCLI(false, CMD_5);
 121         validate(output, RESULT_5, true);
 122 
 123         output = JPackageHelper.executeCLI(false, CMD_6);
 124         validate(output, RESULT_6, true);
 125 
 126     }
 127 
 128     private static void testMissingArgToolProvider() throws Exception {
 129         String output = JPackageHelper.executeToolProvider(false, CMD_1);
 130         validate(output, RESULT_1, true);
 131 
 132         output = JPackageHelper.executeToolProvider(false, CMD_2);
 133         validate(output, RESULT_2, true);
 134 
 135         output = JPackageHelper.executeToolProvider(false, CMD_3);
 136         validate(output, RESULT_3, false);
 137 
 138         output = JPackageHelper.executeToolProvider(false, CMD_4);
 139         validate(output, RESULT_4, true);
 140 
 141         output = JPackageHelper.executeToolProvider(false, CMD_5);
 142         validate(output, RESULT_5, true);
 143 
 144         output = JPackageHelper.executeToolProvider(false, CMD_6);
 145         validate(output, RESULT_6, true);
 146     }
 147 
 148     public static void main(String[] args) throws Exception {
 149         JPackageHelper.createHelloImageJar();
 150         testMissingArg();
 151         testMissingArgToolProvider();
 152     }
 153 
 154 }