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 import java.io.File;
  25 import java.nio.file.Files;
  26 
  27 public abstract class JPackageCreateImageBase {
  28     private static final String app = JPackagePath.getApp();
  29     private static final String appOutput = JPackagePath.getAppOutputFile();
  30     private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
  31 
  32     private static void validateResult(String[] result) throws Exception {
  33         if (result.length != 2) {
  34             throw new AssertionError(
  35                    "Unexpected number of lines: " + result.length);
  36         }
  37 
  38         if (!result[0].trim().equals("jpackage test application")) {
  39             throw new AssertionError("Unexpected result[0]: " + result[0]);
  40         }
  41 
  42         if (!result[1].trim().equals("args.length: 0")) {
  43             throw new AssertionError("Unexpected result[1]: " + result[1]);
  44         }
  45     }
  46 
  47     private static void validate() throws Exception {
  48         int retVal = JPackageHelper.execute(null, app);
  49         if (retVal != 0) {
  50             throw new AssertionError(
  51                    "Test application exited with error: " + retVal);
  52         }
  53 
  54         File outfile = new File(appWorkingDir + File.separator + appOutput);
  55         if (!outfile.exists()) {
  56             throw new AssertionError(appOutput + " was not created");
  57         }
  58 
  59         String output = Files.readString(outfile.toPath());
  60         String[] result = output.split("\n");
  61         validateResult(result);
  62     }
  63 
  64     public static void testCreateImage(String [] cmd) throws Exception {
  65         JPackageHelper.executeCLI(true, cmd);
  66         validate();
  67     }
  68 
  69     public static void testCreateImageToolProvider(String [] cmd) throws Exception {
  70         JPackageHelper.executeToolProvider(true, cmd);
  71         validate();
  72     }
  73 }