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  */
  43 
  44 public final class ErrorTest {
  45 
  46     private final String expectedError;
  47     private final JPackageCommand cmd;
  48 
  49     @Parameters
  50     public static Collection input() {
  51         return List.of(new Object[][]{
  52             // non-existent arg
  53             {"Hello",
  54                     new String[]{"--no-such-argument"},
  55                     null,
  56                     "Invalid Option: [--no-such-argument]"},
  57             // no main jar
  58             {"Hello",
  59                     null, 
  60                     new String[]{"--main-jar"},
  61                     "--main-jar or --module"},
  62             // no main-class
  63             {"Hello",
  64                     null, 
  65                     new String[]{"--main-class"},
  66                     "main class was not specified"},
  67             // non-existent main jar
  68             {"Hello",
  69                     new String[]{"--main-jar", "non-existent.jar"},
  70                     null,
  71                     "main jar does not exist"},
  72             // non-existent runtime
  73             {"Hello",
  74                     new String[]{"--runtime-image", "non-existent.runtime"},
  75                     null,
  76                     "does not exist"},
  77             // non-existent resource-dir
  78             {"Hello",
  79                     new String[]{"--resource-dir", "non-existent.dir"},
  80                     null,
  81                     "does not exist"},
  82             // invalid type
  83             {"Hello",
  84                     new String[]{"--type", "invalid-type"},
  85                     null,
  86                     "Invalid or unsupported type:"},
  87         });
  88     }
  89 
  90     public ErrorTest(String javaAppDesc, String[] jpackageArgs,
  91                 String[] removeArgs,
  92                 String expectedError) {
  93         this.expectedError = expectedError;
  94 
  95         cmd = JPackageCommand.helloAppImage(javaAppDesc)
  96                 .saveConsoleOutput(true).dumpOutput(true);
  97         if (jpackageArgs != null) {
  98             cmd.addArguments(jpackageArgs);
  99         } if (removeArgs != null) {
 100             for (String arg : removeArgs) {
 101                 cmd.removeArgumentWithValue(arg);
 102             }
 103         }
 104     }
 105 
 106     @Test
 107     public void test() {
 108         List<String> output;
 109         output = cmd.useToolProvider(false).execute(1).getOutput();
 110         TKit.assertNotNull(output, "output is null");
 111         TKit.assertTextStream(expectedError).apply(output.stream());
 112 
 113         output = cmd.useToolProvider(true).execute(1).getOutput();
 114         TKit.assertNotNull(output, "output is null");
 115         TKit.assertTextStream(expectedError).apply(output.stream());
 116     }
 117 
 118     /* note
 119      * jdk.jpackage.tests.ErrorTest contains all functionality of
 120      * ErrorTest, InvalidArgTest, and MissingArgumentsTest except for 2 cases:
 121      * missing "--input" and missing "--module-path".  
 122      * These two cases cannot be handled by existing code that adds the
 123      * PrerequiseteAction in HelloApp.addTo().
 124      */
 125 }