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 MaintainerBase {
  30 
  31     private static String TEST_NAME;
  32     private static String EMAIL;
  33     private static String EXT;
  34     private static String OUTPUT;
  35     private static String[] CMD;
  36 
  37     private static void copyResults() throws Exception {
  38         List<String> files = new ArrayList<>();
  39         files.add(OUTPUT.toLowerCase());
  40         JPackageInstallerHelper.copyTestResults(files);
  41     }
  42 
  43     private static final String infoResult = "infoResult.txt";
  44     private static void validatePackage() throws Exception {
  45         int retVal = JPackageHelper.execute(new File(infoResult), "dpkg",
  46                 "--info", OUTPUT.toLowerCase());
  47         if (retVal != 0) {
  48             throw new AssertionError("dpkg exited with error: " + retVal);
  49         }
  50 
  51         File outfile = new File(infoResult);
  52         if (!outfile.exists()) {
  53             throw new AssertionError(infoResult + " was not created");
  54         }
  55 
  56         boolean maintainerFound = false;
  57         for (String line: Files.readAllLines(outfile.toPath())) {
  58             if (line.matches("^[ ]*Maintainer:.*$")) {
  59                 maintainerFound = true;
  60                 if (!line.contains(EMAIL)) {
  61                     throw new AssertionError("Unexpected result: " + line);
  62                 }
  63             }
  64         }
  65         
  66         if (!maintainerFound) {
  67             throw new AssertionError("Maintainer field not found");
  68         }
  69     }
  70 
  71     private static void testCreateInstaller() throws Exception {
  72         JPackageHelper.executeCLI(true, CMD);
  73         JPackageInstallerHelper.validateOutput(OUTPUT);
  74         validatePackage();
  75         copyResults();
  76     }
  77 
  78     private static void verifyInstall() throws Exception {
  79         String app = JPackagePath.getLinuxInstalledApp(TEST_NAME);
  80         JPackageInstallerHelper.validateApp(app);
  81     }
  82 
  83     private static void verifyUnInstall() throws Exception {
  84         String folderPath = JPackagePath.getLinuxInstallFolder(TEST_NAME);
  85         File folder = new File(folderPath);
  86         if (folder.exists()) {
  87             throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist");
  88         }
  89     }
  90 
  91     private static void init(String name, String ext) throws Exception {
  92         TEST_NAME = name;
  93         EMAIL = "jpackage-test@java.com";
  94         EXT = ext;
  95         OUTPUT = "output" + File.separator + TEST_NAME + "-1.0." + EXT;
  96         CMD = new String[]{
  97             "--package-type", EXT,
  98             "--input", "input",
  99             "--output", "output",
 100             "--name", TEST_NAME,
 101             "--main-jar", "hello.jar",
 102             "--main-class", "Hello",
 103             "--linux-deb-maintainer", EMAIL};
 104     }
 105 
 106     public static void run(String name, String ext) throws Exception {
 107         init(name, ext);
 108 
 109         if (JPackageInstallerHelper.isVerifyInstall()) {
 110             verifyInstall();
 111         } else if (JPackageInstallerHelper.isVerifyUnInstall()) {
 112             verifyUnInstall();
 113         } else {
 114             JPackageHelper.createHelloInstallerJar();
 115             testCreateInstaller();
 116         }
 117     }
 118 }