1 /*
   2  * Copyright (c) 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.BufferedWriter;
  25 import java.io.File;
  26 import java.io.FileWriter;
  27 import java.io.PrintWriter;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 public class JPackageCreateInstallerWinUpgradeUUIDBase {
  32 
  33     private static String TEST_NAME;
  34     private static String EXT;
  35     private static String OUTPUT_1;
  36     private static String[] CMD_1;
  37     private static String OUTPUT_2;
  38     private static String[] CMD_2;
  39     private static final String FILE_1 = "file1.txt";
  40     private static final String FILE_2 = "file2.txt";
  41 
  42     private static void copyResults() throws Exception {
  43         List<String> files = new ArrayList<>();
  44         files.add(OUTPUT_1);
  45         files.add(OUTPUT_2);
  46         JPackageInstallerHelper.copyTestResults(files);
  47     }
  48 
  49     private static void testCreateInstaller() throws Exception {
  50         JPackageHelper.executeCLI(true, CMD_1);
  51         JPackageInstallerHelper.validateOutput(OUTPUT_1);
  52         JPackageHelper.executeCLI(true, CMD_2);
  53         JPackageInstallerHelper.validateOutput(OUTPUT_2);
  54         copyResults();
  55     }
  56 
  57     private static void verifyInstall() throws Exception {
  58         String app = JPackagePath.getWinInstalledApp(TEST_NAME);
  59         JPackageInstallerHelper.validateApp(app);
  60 
  61         String file1Path = JPackagePath.getWinInstalledAppFolder(TEST_NAME) + File.separator + FILE_1;
  62         File file1 = new File(file1Path);
  63         if (EXT.equals("msi")) {
  64             if (file1.exists()) {
  65                 throw new AssertionError("Unexpected file does exist: "
  66                         + file1.getAbsolutePath());
  67             }
  68         } else if (EXT.equals("exe")) {
  69             if (!file1.exists()) {
  70                 throw new AssertionError("Unexpected file does not exist: "
  71                     + file1.getAbsolutePath());
  72             }
  73         } else {
  74             throw new AssertionError("Unknown installer type: " + EXT);
  75         }
  76 
  77         String file2Path = JPackagePath.getWinInstalledAppFolder(TEST_NAME) + File.separator + FILE_2;
  78         File file2 = new File(file2Path);
  79         if (!file2.exists()) {
  80             throw new AssertionError("Unexpected file does not exist: "
  81                     + file2.getAbsolutePath());
  82         }
  83     }
  84 
  85     private static void verifyUnInstall() throws Exception {
  86         String folderPath = JPackagePath.getWinInstallFolder(TEST_NAME);
  87         File folder = new File(folderPath);
  88         if (folder.exists()) {
  89             throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist");
  90         }
  91     }
  92 
  93     private static void init(String name, String ext) {
  94         TEST_NAME = name;
  95         EXT = ext;
  96         OUTPUT_1 = "output" + File.separator + TEST_NAME + "-1.0." + EXT;
  97         CMD_1 = new String[]{
  98             "create-installer",
  99             "--installer-type", EXT,
 100             "--input", "input",
 101             "--output", "output",
 102             "--name", TEST_NAME,
 103             "--main-jar", "hello.jar",
 104             "--main-class", "Hello",
 105             "--app-version", "1.0",
 106             "--win-upgrade-uuid", "F0B18E75-52AD-41A2-BC86-6BE4FCD50BEB"};
 107         OUTPUT_2 = "output" + File.separator + TEST_NAME + "-2.0." + EXT;
 108         CMD_2 = new String[]{
 109             "create-installer",
 110             "--installer-type", EXT,
 111             "--input", "input",
 112             "--output", "output",
 113             "--name", TEST_NAME,
 114             "--main-jar", "hello.jar",
 115             "--main-class", "Hello",
 116             "--app-version", "2.0",
 117             "--win-upgrade-uuid", "F0B18E75-52AD-41A2-BC86-6BE4FCD50BEB"};
 118     }
 119 
 120     private static void createInputFile(String name, String context) throws Exception {
 121         try (PrintWriter out = new PrintWriter(
 122                 new BufferedWriter(new FileWriter("input" + File.separator + name)))) {
 123             out.println(context);
 124         }
 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             createInputFile(FILE_1, FILE_1);
 137             createInputFile(FILE_2, FILE_2);
 138             testCreateInstaller();
 139         }
 140     }
 141 }