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 FileAssociationsBase {
  34 
  35     private static String TEST_NAME;
  36     private static String EXT;
  37     private static String TEST_EXT;
  38     private static String OUTPUT;
  39     private static String[] CMD;
  40 
  41     private static void copyResults() throws Exception {
  42         List<String> files = new ArrayList<>();
  43         files.add(OUTPUT.toLowerCase());
  44         JPackageInstallerHelper.copyTestResults(files);
  45     }
  46 
  47     private static void testCreateInstaller() throws Exception {
  48         JPackageHelper.executeCLI(true, CMD);
  49         JPackageInstallerHelper.validateOutput(OUTPUT);
  50         copyResults();
  51     }
  52 
  53     private static void validateAppOutput() throws Exception {
  54         File outFile = new File("appOutput.txt");
  55         int count = 10;
  56         boolean bOutputCreated = false;
  57         while (count > 0) {
  58             if (!outFile.exists()) {
  59                 Thread.sleep(3000);
  60                 count--;
  61             } else {
  62                 bOutputCreated = true;
  63                 break;
  64             }
  65         }
  66 
  67         if (!bOutputCreated) {
  68             throw new AssertionError(outFile.getAbsolutePath() + " was not created");
  69         }
  70 
  71         String output = Files.readString(outFile.toPath());
  72         String[] result = output.split("\n");
  73         if (result.length != 3) {
  74             System.err.println(output);
  75             throw new AssertionError(
  76                     "Unexpected number of lines: " + result.length);
  77         }
  78 
  79         if (!result[0].trim().equals("jpackage test application")) {
  80             throw new AssertionError("Unexpected result[0]: " + result[0]);
  81         }
  82 
  83         if (!result[1].trim().equals("args.length: 1")) {
  84             throw new AssertionError("Unexpected result[1]: " + result[1]);
  85         }
  86 
  87         File faFile = new File(TEST_NAME + "." + TEST_EXT);
  88         if (!result[2].trim().equals(faFile.getAbsolutePath())) {
  89             throw new AssertionError("Unexpected result[2]: " + result[2]);
  90         }
  91     }
  92 
  93     private static void verifyInstall() throws Exception {
  94         createAssociationsTestFile();
  95         Desktop.getDesktop().open(new File(TEST_NAME + "." + TEST_EXT));
  96         validateAppOutput();
  97     }
  98 
  99     private static void verifyUnInstall() throws Exception {
 100         String folderPath = JPackagePath.getLinuxInstallFolder(TEST_NAME);
 101         File folder = new File(folderPath);
 102         if (folder.exists()) {
 103             throw new AssertionError("Error: " + folder.getAbsolutePath() + " exist");
 104         }
 105     }
 106 
 107     private static void createAssociationsTestFile() throws Exception {
 108         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 109                 new FileWriter(TEST_NAME + "." + TEST_EXT)))) {
 110             out.println(TEST_NAME);
 111         }
 112     }
 113 
 114     private static void createAssociationsProperties() throws Exception {
 115         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 116                 new FileWriter("fa.properties")))) {
 117             out.println("extension=" + TEST_EXT);
 118             out.println("mime-type=application/" + TEST_EXT);
 119             out.println("description=jpackage test extention");
 120         }
 121     }
 122 
 123     private static void init(String name, String ext) {
 124         TEST_NAME = name;
 125         EXT = ext;
 126         TEST_EXT = "jptest1";
 127         if (EXT.equals("rpm")) {
 128             OUTPUT = "output" + File.separator + TEST_NAME + "-1.0-1.x86_64." + EXT;
 129         } else {
 130             OUTPUT = "output" + File.separator + TEST_NAME + "-1.0." + EXT;
 131         }
 132         CMD = new String[]{
 133             "--package-type", EXT,
 134             "--input", "input",
 135             "--output", "output",
 136             "--name", TEST_NAME,
 137             "--main-jar", "hello.jar",
 138             "--main-class", "Hello",
 139             "--file-associations", "fa.properties"};
 140     }
 141 
 142     public static void run(String name, String ext) throws Exception {
 143         init(name, ext);
 144 
 145         if (JPackageInstallerHelper.isVerifyInstall()) {
 146             verifyInstall();
 147         } else if (JPackageInstallerHelper.isVerifyUnInstall()) {
 148             verifyUnInstall();
 149         } else {
 150             JPackageHelper.createHelloInstallerJar();
 151             createAssociationsProperties();
 152             testCreateInstaller();
 153         }
 154     }
 155 }