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