test/jdk/tools/jpackage/linux/base/PackageDepsBase.java
branchJDK-8200758-branch
changeset 58113 885b0543f6e4
parent 58037 c127c766fe8e
child 58114 42df7de58e39
equal deleted inserted replaced
58037:c127c766fe8e 58113:885b0543f6e4
     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 import java.util.ArrayList;
       
    27 import java.util.List;
       
    28 
       
    29 public class PackageDepsBase {
       
    30 
       
    31     private static String TEST_NAME;
       
    32     private static String DEP_NAME;
       
    33     private static String EXT;
       
    34     private static String OUTPUT;
       
    35     private static String OUTPUT_DEP;
       
    36     private static String[] CMD;
       
    37     private static String[] CMD_DEP;
       
    38 
       
    39     private static void copyResults() throws Exception {
       
    40         List<String> files = new ArrayList<>();
       
    41         files.add(OUTPUT.toLowerCase());
       
    42         files.add(OUTPUT_DEP.toLowerCase());
       
    43         JPackageInstallerHelper.copyTestResults(files);
       
    44     }
       
    45 
       
    46     private static final String infoResult = "infoResult.txt";
       
    47     private static void validatePackage() throws Exception {
       
    48         if (EXT.equals("rpm")) {
       
    49             int retVal = JPackageHelper.execute(new File(infoResult),"rpm",
       
    50                     "--query", "--package", "--requires", OUTPUT.toLowerCase());
       
    51             if (retVal != 0) {
       
    52                 throw new AssertionError("rpm exited with error: " + retVal);
       
    53             }
       
    54         } else {
       
    55             int retVal = JPackageHelper.execute(new File(infoResult), "dpkg",
       
    56                     "--info", OUTPUT.toLowerCase());
       
    57             if (retVal != 0) {
       
    58                 throw new AssertionError("dpkg exited with error: " + retVal);
       
    59             }
       
    60         }
       
    61 
       
    62         File outfile = new File(infoResult);
       
    63         if (!outfile.exists()) {
       
    64             throw new AssertionError(infoResult + " was not created");
       
    65         }
       
    66 
       
    67         String output = Files.readString(outfile.toPath());
       
    68         if (!output.contains(DEP_NAME.toLowerCase())) {
       
    69             throw new AssertionError("Unexpected result: " + output);
       
    70         }
       
    71     }
       
    72 
       
    73     private static void testCreateInstaller() throws Exception {
       
    74         JPackageHelper.executeCLI(true, CMD);
       
    75         JPackageHelper.executeCLI(true, CMD_DEP);
       
    76         JPackageInstallerHelper.validateOutput(OUTPUT);
       
    77         JPackageInstallerHelper.validateOutput(OUTPUT_DEP);
       
    78         validatePackage();
       
    79         copyResults();
       
    80     }
       
    81 
       
    82     private static void verifyInstall() throws Exception {
       
    83         String app = JPackagePath.getLinuxInstalledApp(TEST_NAME);
       
    84         JPackageInstallerHelper.validateApp(app);
       
    85 
       
    86         app = JPackagePath.getLinuxInstalledApp(DEP_NAME);
       
    87         JPackageInstallerHelper.validateApp(app);
       
    88     }
       
    89 
       
    90     private static void verifyUnInstall() throws Exception {
       
    91         String folderPath = JPackagePath.getLinuxInstallFolder(TEST_NAME);
       
    92         File folder = new File(folderPath);
       
    93         if (folder.exists()) {
       
    94             throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist");
       
    95         }
       
    96 
       
    97         folderPath = JPackagePath.getLinuxInstallFolder(DEP_NAME);
       
    98         folder = new File(folderPath);
       
    99         if (folder.exists()) {
       
   100             throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist");
       
   101         }
       
   102     }
       
   103 
       
   104     private static void init(String name, String ext) throws Exception {
       
   105         TEST_NAME = name;
       
   106         DEP_NAME = name + "Dep";
       
   107         EXT = ext;
       
   108         OUTPUT = Base.getBundlePath(TEST_NAME, EXT);
       
   109         OUTPUT_DEP = Base.getBundlePath(DEP_NAME, EXT);
       
   110         CMD = new String[]{
       
   111             "--package-type", EXT,
       
   112             "--input", "input",
       
   113             "--output", "output",
       
   114             "--name", TEST_NAME,
       
   115             "--main-jar", "hello.jar",
       
   116             "--main-class", "Hello",
       
   117             "--linux-package-deps", DEP_NAME.toLowerCase()};
       
   118         CMD_DEP = new String[]{
       
   119             "--package-type", EXT,
       
   120             "--input", "input",
       
   121             "--output", "output",
       
   122             "--name", DEP_NAME,
       
   123             "--main-jar", "hello.jar",
       
   124             "--main-class", "Hello"};
       
   125     }
       
   126 
       
   127     public static void run(String name, String ext) throws Exception {
       
   128         init(name, ext);
       
   129 
       
   130         if (JPackageInstallerHelper.isVerifyInstall()) {
       
   131             verifyInstall();
       
   132         } else if (JPackageInstallerHelper.isVerifyUnInstall()) {
       
   133             verifyUnInstall();
       
   134         } else {
       
   135             JPackageHelper.createHelloInstallerJar();
       
   136             testCreateInstaller();
       
   137         }
       
   138     }
       
   139 }