1 /*
   2  * Copyright (c) 2018, 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  /*
  28  * @test
  29  * @summary jpackager create image test
  30  * @build JPackagerHelper
  31  * @modules jdk.jpackager
  32  * @run main/othervm -Xmx512m JPackagerCreateImageTest
  33  */
  34 public class JPackagerCreateImageTest {
  35     private static final String app;
  36     private static final String appOutput = "appOutput.txt";
  37     private static final String appOutputPath;
  38     private static final String MSG = "jpackager test application";
  39     private static final String [] CMD = {
  40         "create-image",
  41         "--input", "input",
  42         "--output", "output",
  43         "--name", "test",
  44         "--main-jar", "hello.jar",
  45         "--class", "Hello",
  46         "--force",
  47         "--files", "hello.jar"};
  48 
  49     static {
  50         if (JPackagerHelper.isWindows()) {
  51             app = "output" + File.separator + "test"
  52                     + File.separator + "test.exe";
  53             appOutputPath = "output" + File.separator + "test"
  54                     + File.separator + "app";
  55         } else if (JPackagerHelper.isOSX()) {
  56             app = "output" + File.separator + "test.app" + File.separator
  57                     + "Contents" + File.separator
  58                     + "MacOS" + File.separator + "test";
  59             appOutputPath = "output" + File.separator + "test.app"
  60                     + File.separator + "Contents" + File.separator + "Java";
  61         } else {
  62             app = "output" + File.separator + "test" + File.separator + "test";
  63             appOutputPath = "output" + File.separator + "test"
  64                     + File.separator + "app";
  65         }
  66     }
  67 
  68     private static void validateResult(String[] result) throws Exception {
  69         if (result.length != 2) {
  70             throw new AssertionError(
  71                    "Unexpected number of lines: " + result.length);
  72         }
  73 
  74         if (!result[0].trim().equals("jpackager test application")) {
  75             throw new AssertionError("Unexpected result[0]: " + result[0]);
  76         }
  77 
  78         if (!result[1].trim().equals("args.length: 0")) {
  79             throw new AssertionError("Unexpected result[1]: " + result[1]);
  80         }
  81     }
  82 
  83     private static void validate() throws Exception {
  84         int retVal = JPackagerHelper.execute(null, app);
  85         if (retVal != 0) {
  86             throw new AssertionError(
  87                    "Test application exited with error: " + retVal);
  88         }
  89 
  90         File outfile = new File(appOutputPath + File.separator + appOutput);
  91         if (!outfile.exists()) {
  92             throw new AssertionError(appOutput + " was not created");
  93         }
  94 
  95         String output = Files.readString(outfile.toPath());
  96         String[] result = output.split("\n");
  97         validateResult(result);
  98     }
  99 
 100     private static void testCreateImage() throws Exception {
 101         JPackagerHelper.executeCLI(true, CMD);
 102         validate();
 103     }
 104 
 105     private static void testCreateImageToolProvider() throws Exception {
 106         JPackagerHelper.executeToolProvider(true, CMD);
 107         validate();
 108     }
 109 
 110     public static void main(String[] args) throws Exception {
 111         JPackagerHelper.createHelloJar();
 112         testCreateImage();
 113         testCreateImageToolProvider();
 114     }
 115 
 116 }