1 
   2 import java.io.File;
   3 import java.nio.file.Files;
   4 
   5 /*
   6  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   7  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   8  *
   9  * This code is free software; you can redistribute it and/or modify it
  10  * under the terms of the GNU General Public License version 2 only, as
  11  * published by the Free Software Foundation.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 
  28 /*
  29  * @test
  30  * @summary jpackage create image --app-version test
  31  * @library ../helpers
  32  * @build JPackageHelper
  33  * @build JPackagePath
  34  * @modules jdk.jpackage
  35  * @run main/othervm -Xmx512m JPackageCreateImageVersionTest
  36  */
  37 public class JPackageCreateImageVersionTest {
  38     private static final String OUTPUT = "output";
  39     private static final String appCfg = JPackagePath.getAppCfg();
  40     private static final String VERSION = "2.3";
  41     private static final String VERSION_DEFAULT = "1.0";
  42 
  43     private static final String[] CMD = {
  44         "create-image",
  45         "--input", "input",
  46         "--output", OUTPUT,
  47         "--name", "test",
  48         "--main-jar", "hello.jar",
  49         "--main-class", "Hello",
  50         "--files", "hello.jar"};
  51 
  52     private static final String[] CMD_VERSION = {
  53         "create-image",
  54         "--input", "input",
  55         "--output", OUTPUT,
  56         "--name", "test",
  57         "--main-jar", "hello.jar",
  58         "--main-class", "Hello",
  59         "--files", "hello.jar",
  60         "--app-version", VERSION};
  61 
  62     private static void validate(String version)
  63             throws Exception {
  64         File outfile = new File(appCfg);
  65         if (!outfile.exists()) {
  66             throw new AssertionError(appCfg + " was not created");
  67         }
  68 
  69         String output = Files.readString(outfile.toPath());
  70         if (version == null) {
  71             version = VERSION_DEFAULT;
  72         }
  73 
  74         String expected = "app.version=" + version;
  75         if (!output.contains(expected)) {
  76             System.err.println("Expected: " + expected);
  77             throw new AssertionError("Cannot find expected entry in config file");
  78         }
  79     }
  80 
  81     private static void testVersion() throws Exception {
  82         JPackageHelper.executeCLI(true, CMD);
  83         validate(null);
  84         JPackageHelper.deleteOutputFolder(OUTPUT);
  85         JPackageHelper.executeCLI(true, CMD_VERSION);
  86         validate(VERSION);
  87     }
  88 
  89     private static void testVersionToolProvider() throws Exception {
  90         JPackageHelper.deleteOutputFolder(OUTPUT);
  91         JPackageHelper.executeToolProvider(true, CMD);
  92         validate(null);
  93         JPackageHelper.deleteOutputFolder(OUTPUT);
  94         JPackageHelper.executeToolProvider(true, CMD_VERSION);
  95         validate(VERSION);
  96     }
  97 
  98     public static void main(String[] args) throws Exception {
  99         JPackageHelper.createHelloImageJar();
 100         testVersion();
 101         testVersionToolProvider();
 102     }
 103 
 104 }