test/jdk/tools/jpackage/createappimage/JPackageCreateAppImageVersionTest.java
branchJDK-8200758-branch
changeset 57307 4948a1944cf9
child 57395 521c02b9eed0
equal deleted inserted replaced
57306:b3b59183df78 57307:4948a1944cf9
       
     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 JPackageCreateAppImageVersionTest
       
    36  */
       
    37 public class JPackageCreateAppImageVersionTest {
       
    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-app-image",
       
    45         "--input", "input",
       
    46         "--output", OUTPUT,
       
    47         "--name", "test",
       
    48         "--main-jar", "hello.jar",
       
    49         "--main-class", "Hello",
       
    50    };
       
    51 
       
    52     private static final String[] CMD_VERSION = {
       
    53         "create-app-image",
       
    54         "--input", "input",
       
    55         "--output", OUTPUT,
       
    56         "--name", "test",
       
    57         "--main-jar", "hello.jar",
       
    58         "--main-class", "Hello",
       
    59         "--app-version", VERSION};
       
    60 
       
    61     private static void validate(String version)
       
    62             throws Exception {
       
    63         File outfile = new File(appCfg);
       
    64         if (!outfile.exists()) {
       
    65             throw new AssertionError(appCfg + " was not created");
       
    66         }
       
    67 
       
    68         String output = Files.readString(outfile.toPath());
       
    69         if (version == null) {
       
    70             version = VERSION_DEFAULT;
       
    71         }
       
    72 
       
    73         String expected = "app.version=" + version;
       
    74         if (!output.contains(expected)) {
       
    75             System.err.println("Expected: " + expected);
       
    76             throw new AssertionError("Cannot find expected entry in config file");
       
    77         }
       
    78     }
       
    79 
       
    80     private static void testVersion() throws Exception {
       
    81         JPackageHelper.executeCLI(true, CMD);
       
    82         validate(null);
       
    83         JPackageHelper.deleteOutputFolder(OUTPUT);
       
    84         JPackageHelper.executeCLI(true, CMD_VERSION);
       
    85         validate(VERSION);
       
    86     }
       
    87 
       
    88     private static void testVersionToolProvider() throws Exception {
       
    89         JPackageHelper.deleteOutputFolder(OUTPUT);
       
    90         JPackageHelper.executeToolProvider(true, CMD);
       
    91         validate(null);
       
    92         JPackageHelper.deleteOutputFolder(OUTPUT);
       
    93         JPackageHelper.executeToolProvider(true, CMD_VERSION);
       
    94         validate(VERSION);
       
    95     }
       
    96 
       
    97     public static void main(String[] args) throws Exception {
       
    98         JPackageHelper.createHelloImageJar();
       
    99         testVersion();
       
   100         testVersionToolProvider();
       
   101     }
       
   102 
       
   103 }