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 Base
  31  * @modules jdk.incubator.jpackage
  32  * @run main/othervm -Xmx512m ErrorTest
  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 ErrorTest {
  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 = "--dest";
  48     private static final String EXPECTED2 = "--main-jar or --module";
  49 
  50     private static final String [] CMD1 = {
  51         "--type", "app-image",
  52         "--input", "input",
  53         "--dest", OUTPUT,
  54         "--name", "test",
  55         "--main-jar", "non-existant.jar",
  56     };
  57     private static final String EXP1 = "main jar does not exist";
  58 
  59     private static final String [] CMD2 = {
  60         "--type", "app-image",
  61         "--input", "input",
  62         "--dest", OUTPUT,
  63         "--name", "test",
  64         "--main-jar", "hello.jar",
  65     };
  66     private static final String EXP2 = "class was not specified nor was";
  67 
  68     private static void validate(String output, String expected, boolean single)
  69             throws Exception {
  70         String[] result = JPackageHelper.splitAndFilter(output);
  71         if (single && result.length != 1) {
  72             System.err.println(output);
  73             throw new AssertionError("Unexpected multiple lines of output: "
  74                     + output);
  75         }
  76 
  77         if (!result[0].trim().contains(expected)) {
  78             throw new AssertionError("Unexpected output: " + result[0]
  79                     + " - expected output to contain: " + expected);
  80         }
  81     }
  82 
  83 
  84     public static void main(String[] args) throws Exception {
  85         JPackageHelper.createHelloImageJar();
  86 
  87         validate(JPackageHelper.executeToolProvider(false,
  88             "--type", "app-image", ARG1), EXPECTED1, true);
  89         validate(JPackageHelper.executeToolProvider(false,
  90             "--type", "app-image", ARG2), EXPECTED2, true);
  91 
  92         JPackageHelper.deleteOutputFolder(OUTPUT);
  93         validate(JPackageHelper.executeToolProvider(false, CMD1), EXP1, false);
  94 
  95         JPackageHelper.deleteOutputFolder(OUTPUT);
  96         validate(JPackageHelper.executeToolProvider(false, CMD2), EXP2, false);
  97 
  98     }
  99 
 100 }