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 app image error test
  27  * @library ../helpers
  28  * @build JPackageHelper
  29  * @build JPackagePath
  30  * @build JPackageCreateAppImageBase
  31  * @modules jdk.jpackage
  32  * @run main/othervm -Xmx512m JPackageCreateAppImageErrorTest
  33  */
  34 import java.util.*;
  35 import java.io.*;
  36 import java.nio.*;
  37 import java.nio.file.*;
  38 import java.nio.file.attribute.*;
  39 
  40 public class JPackageCreateAppImageErrorTest {
  41 
  42     private static final String OUTPUT = "output";
  43 
  44     private static final String ARG1 = "--no-such-argument";
  45     private static final String EXPECTED1 =
  46             "Invalid Option: [--no-such-argument]";
  47     private static final String ARG2 = "--output";
  48     private static final String EXPECTED2 = "Missing argument:";
  49 
  50     private static final String [] CMD1 = {
  51         "--input", "input",
  52         "--output", OUTPUT,
  53         "--name", "test",
  54         "--main-jar", "non-existant.jar",
  55     };
  56     private static final String EXP1 = "main jar does not exist";
  57 
  58     private static final String [] CMD2 = {
  59         "--input", "input",
  60         "--output", OUTPUT,
  61         "--name", "test",
  62         "--main-jar", "hello.jar",
  63     };
  64     private static final String EXP2 = "class was not specified nor was";
  65 
  66     private static void validate(String output, String expected, boolean single)
  67             throws Exception {
  68         String[] result = output.split("\n");
  69         if (single && result.length != 1) {
  70             System.err.println(output);
  71             throw new AssertionError("Unexpected multiple lines of output: "
  72                     + output);
  73         }
  74 
  75         if (!result[0].trim().contains(expected)) {
  76             throw new AssertionError("Unexpected output: " + result[0]
  77                     + " - expected output to contain: " + expected);
  78         }
  79     }
  80 
  81 
  82     public static void main(String[] args) throws Exception {
  83         JPackageHelper.createHelloImageJar();
  84 
  85         validate(JPackageHelper.executeToolProvider(false, ARG1), EXPECTED1, true);
  86         validate(JPackageHelper.executeToolProvider(false, ARG2), EXPECTED2, true);
  87 
  88         JPackageHelper.deleteOutputFolder(OUTPUT);
  89         validate(JPackageHelper.executeToolProvider(false, CMD1), EXP1, false);
  90 
  91         JPackageHelper.deleteOutputFolder(OUTPUT);
  92         validate(JPackageHelper.executeToolProvider(false, CMD2), EXP2, false);
  93 
  94     }
  95 
  96 }