1 /*
   2  * Copyright (c) 2020, 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 package jdk.jpackage.tests;
  25 
  26 import java.util.Collection;
  27 import java.util.List;
  28 import jdk.jpackage.test.Annotations.Parameters;
  29 import jdk.jpackage.test.Annotations.Test;
  30 import jdk.jpackage.test.JPackageCommand;
  31 import jdk.jpackage.test.TKit;
  32 
  33 /*
  34  * @test
  35  * @summary jpackage application version testing
  36  * @library ../../../../helpers
  37  * @build jdk.jpackage.test.*
  38  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  39  * @compile ErrorTest.java
  40  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  41  *  --jpt-run=jdk.jpackage.tests.ErrorTest
  42  *  --jpt-before-run=jdk.jpackage.test.JPackageCommand.useExecutableByDefault
  43  */
  44 
  45 /*
  46  * @test
  47  * @summary jpackage application version testing
  48  * @library ../../../../helpers
  49  * @build jdk.jpackage.test.*
  50  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  51  * @compile ErrorTest.java
  52  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  53  *  --jpt-run=jdk.jpackage.tests.ErrorTest
  54  *  --jpt-before-run=jdk.jpackage.test.JPackageCommand.useToolProviderByDefault
  55  */
  56 
  57 public final class ErrorTest {
  58 
  59     private final String expectedError;
  60     private final JPackageCommand cmd;
  61 
  62     @Parameters
  63     public static Collection input() {
  64         return List.of(new Object[][]{
  65             // non-existent arg
  66             {"Hello",
  67                     new String[]{"--no-such-argument"},
  68                     null,
  69                     "Invalid Option: [--no-such-argument]"},
  70             // no main jar
  71             {"Hello",
  72                     null, 
  73                     new String[]{"--main-jar"},
  74                     "--main-jar or --module"},
  75             // no main-class
  76             {"Hello",
  77                     null, 
  78                     new String[]{"--main-class"},
  79                     "main class was not specified"},
  80             // non-existent main jar
  81             {"Hello",
  82                     new String[]{"--main-jar", "non-existent.jar"},
  83                     null,
  84                     "main jar does not exist"},
  85             // non-existent runtime
  86             {"Hello",
  87                     new String[]{"--runtime-image", "non-existent.runtime"},
  88                     null,
  89                     "does not exist"},
  90             // non-existent resource-dir
  91             {"Hello",
  92                     new String[]{"--resource-dir", "non-existent.dir"},
  93                     null,
  94                     "does not exist"},
  95             // invalid type
  96             {"Hello",
  97                     new String[]{"--type", "invalid-type"},
  98                     null,
  99                     "Invalid or unsupported type:"},
 100             // no --input 
 101             {"Hello",
 102                     null, 
 103                     new String[]{"--input"},
 104                     "Missing argument: --input"},
 105             // no --module-path
 106             {"com.other/com.other.Hello",
 107                     null, 
 108                     new String[]{"--module-path"},
 109                     "Missing argument: --runtime-image or --module-path"},
 110         });
 111     }
 112 
 113     public ErrorTest(String javaAppDesc, String[] jpackageArgs,
 114                 String[] removeArgs,
 115                 String expectedError) {
 116         this.expectedError = expectedError;
 117 
 118         cmd = JPackageCommand.helloAppImage(javaAppDesc)
 119                 .saveConsoleOutput(true).dumpOutput(true);
 120         if (jpackageArgs != null) {
 121             cmd.addArguments(jpackageArgs);
 122         } if (removeArgs != null) {
 123             for (String arg : removeArgs) {
 124                 cmd.removeArgumentWithValue(arg);
 125             }
 126         }
 127     }
 128 
 129     @Test
 130     public void test() {
 131         List<String> output = cmd.execute(1).getOutput();
 132         TKit.assertNotNull(output, "output is null");
 133         TKit.assertTextStream(expectedError).apply(output.stream());
 134     }
 135 
 136 }