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.awt.Desktop;
  25 import java.io.BufferedWriter;
  26 import java.io.File;
  27 import java.io.FileWriter;
  28 import java.io.PrintWriter;
  29 import java.nio.file.Files;
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 
  33 public class JPackageCreateInstallerWinRegistryNameBase {
  34 
  35     private static String TEST_NAME;
  36     private static String EXT;
  37     private static String TEST_EXT;
  38     private static String WIN_REGISTRY_NAME;
  39     private static String OUTPUT;
  40     private static String[] CMD;
  41 
  42     private static void copyResults() throws Exception {
  43         List<String> files = new ArrayList<>();
  44         files.add(OUTPUT);
  45         JPackageInstallerHelper.copyTestResults(files);
  46     }
  47 
  48     private static void testCreateInstaller() throws Exception {
  49         JPackageHelper.executeCLI(true, CMD);
  50         JPackageInstallerHelper.validateOutput(OUTPUT);
  51         copyResults();
  52     }
  53 
  54     private static void validateAppOutput() throws Exception {
  55         File outFile = new File("appOutput.txt");
  56         int count = 10;
  57         boolean bOutputCreated = false;
  58         while (count > 0) {
  59             if (!outFile.exists()) {
  60                 Thread.sleep(3000);
  61                 count--;
  62             } else {
  63                 bOutputCreated = true;
  64                 break;
  65             }
  66         }
  67 
  68         if (!bOutputCreated) {
  69             throw new AssertionError(outFile.getAbsolutePath() + " was not created");
  70         }
  71 
  72         String output = Files.readString(outFile.toPath());
  73         String[] result = output.split("\n");
  74         if (result.length != 3) {
  75             System.err.println(output);
  76             throw new AssertionError(
  77                     "Unexpected number of lines: " + result.length);
  78         }
  79 
  80         if (!result[0].trim().equals("jpackage test application")) {
  81             throw new AssertionError("Unexpected result[0]: " + result[0]);
  82         }
  83 
  84         if (!result[1].trim().equals("args.length: 1")) {
  85             throw new AssertionError("Unexpected result[1]: " + result[1]);
  86         }
  87 
  88         File faFile = new File(TEST_NAME + "." + TEST_EXT);
  89         if (!result[2].trim().equals(faFile.getAbsolutePath())) {
  90             throw new AssertionError("Unexpected result[2]: " + result[2]);
  91         }
  92     }
  93 
  94     private static void verifyInstall() throws Exception {
  95         createFileAssociationsTestFile();
  96         Desktop.getDesktop().open(new File(TEST_NAME + "." + TEST_EXT));
  97         validateAppOutput();
  98 
  99         // Validate start menu
 100         JPackageInstallerHelper.validateStartMenu("Unknown", TEST_NAME, true);
 101 
 102         // Validate registry
 103         String[] values1 = {WIN_REGISTRY_NAME};
 104         JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\." + TEST_EXT, values1, true);
 105         String[] values2 = {TEST_EXT};
 106         JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\MIME\\Database\\Content Type\\application/" + TEST_EXT, values2, true);
 107     }
 108 
 109     private static void verifyUnInstall() throws Exception {
 110         String folderPath = JPackagePath.getWinInstallFolder(TEST_NAME);
 111         File folder = new File(folderPath);
 112         if (folder.exists()) {
 113             throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist");
 114         }
 115 
 116         // Validate start menu
 117         JPackageInstallerHelper.validateStartMenu("Unknown", TEST_NAME, false);
 118 
 119         // Validate registry
 120         JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\." + TEST_EXT, null, false);
 121         JPackageInstallerHelper.validateWinRegistry("HKLM\\Software\\Classes\\MIME\\Database\\Content Type\\application/" + TEST_EXT, null, false);
 122     }
 123 
 124     private static void createFileAssociationsTestFile() throws Exception {
 125         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 126                 new FileWriter(TEST_NAME + "." + TEST_EXT)))) {
 127             out.println(TEST_NAME);
 128         }
 129     }
 130 
 131     private static void createFileAssociationsProperties() throws Exception {
 132         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 133                 new FileWriter("fa.properties")))) {
 134             out.println("extension=" + TEST_EXT);
 135             out.println("mime-type=application/" + TEST_EXT);
 136             out.println("description=jpackage test extention");
 137         }
 138     }
 139 
 140     private static void init(String name, String ext) {
 141         TEST_NAME = name;
 142         EXT = ext;
 143         TEST_EXT = "jptest2";
 144         WIN_REGISTRY_NAME = "JPWINTESTREGISTRYNAME";
 145         OUTPUT = "output" + File.separator + TEST_NAME + "-1.0." + EXT;
 146         CMD = new String[]{
 147             "create-installer",
 148             "--installer-type", EXT,
 149             "--input", "input",
 150             "--output", "output",
 151             "--name", TEST_NAME,
 152             "--main-jar", "hello.jar",
 153             "--main-class", "Hello",
 154             "--file-associations", "fa.properties",
 155             "--win-registry-name", WIN_REGISTRY_NAME};
 156     }
 157 
 158     public static void run(String name, String ext) throws Exception {
 159         init(name, ext);
 160 
 161         if (JPackageInstallerHelper.isVerifyInstall()) {
 162             verifyInstall();
 163         } else if (JPackageInstallerHelper.isVerifyUnInstall()) {
 164             verifyUnInstall();
 165         } else {
 166             JPackageHelper.createHelloInstallerJar();
 167             createFileAssociationsProperties();
 168             testCreateInstaller();
 169         }
 170     }
 171 }