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.Path;
  26 
  27 /**
  28  * Helper class which contains functions to get different system
  29  * dependent paths used by tests
  30  */
  31 public class JPackagePath {
  32 
  33     // Path to Windows "Program Files" folder
  34     // Probably better to figure this out programattically
  35     private static final String WIN_PROGRAM_FILES = "C:\\Program Files";
  36 
  37     // Path to Windows Start menu items
  38     private static final String WIN_START_MENU =
  39             "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs";
  40 
  41     // Path to Windows public desktop location
  42     private static final String WIN_PUBLIC_DESKTOP =
  43             "C:\\Users\\Public\\Desktop";
  44 
  45     // Return path to test src adjusted to location of caller
  46     public static String getTestSrcRoot() {
  47         return JPackageHelper.TEST_SRC_ROOT;
  48     }
  49 
  50     // Return path to calling test
  51     public static String getTestSrc() {
  52         return JPackageHelper.TEST_SRC;
  53     }
  54 
  55     // Returns path to generate test application
  56     public static String getApp() {
  57         return getApp("test");
  58     }
  59 
  60     public static String getApp(String name) {
  61         if (JPackageHelper.isWindows()) {
  62             return Path.of("output", name, "bin", name + ".exe").toString();
  63         } else if (JPackageHelper.isOSX()) {
  64             return Path.of("output", name + ".app",
  65                     "Contents", "MacOS", name).toString();
  66         } else if (JPackageHelper.isLinux()) {
  67             return Path.of("output", name, "bin", name).toString();
  68         } else {
  69             throw new AssertionError("Cannot detect platform");
  70         }
  71     }
  72 
  73     // Returns path to generate test application icon
  74     public static String getAppIcon() {
  75         return getAppIcon("test");
  76     }
  77 
  78     public static String getAppIcon(String name) {
  79         if (JPackageHelper.isWindows()) {
  80             return Path.of("output", name, "bin", name + ".ico").toString();
  81         } else if (JPackageHelper.isOSX()) {
  82             return Path.of("output", name + ".app",
  83                     "Contents", "Resources", name + ".icns").toString();
  84         } else if (JPackageHelper.isLinux()) {
  85             return Path.of("output", name, "bin", name + ".png").toString();
  86         } else {
  87             throw new AssertionError("Cannot detect platform");
  88         }
  89     }
  90 
  91     // Returns path to generate secondary launcher of given application
  92     public static String getAppSL(String sl) {
  93         return getAppSL("test", sl);
  94     }
  95 
  96     public static String getAppSL(String app, String sl) {
  97         if (JPackageHelper.isWindows()) {
  98             return Path.of("output", app, "bin", sl + ".exe").toString();
  99         } else if (JPackageHelper.isOSX()) {
 100             return Path.of("output", app + ".app",
 101                     "Contents", "MacOS", sl).toString();
 102         } else if (JPackageHelper.isLinux()) {
 103             return Path.of("output", app, "bin", sl).toString();
 104         } else {
 105             throw new AssertionError("Cannot detect platform");
 106         }
 107     }
 108 
 109     // Returns path to test application cfg file
 110     public static String getAppCfg() {
 111         return getAppCfg("test");
 112     }
 113 
 114     public static String getAppCfg(String name) {
 115          if (JPackageHelper.isWindows()) {
 116             return Path.of("output", name, "app", name + ".cfg").toString();
 117         } else if (JPackageHelper.isOSX()) {
 118             return Path.of("output", name + ".app",
 119                     "Contents", "Java", name + ".cfg").toString();
 120         } else if (JPackageHelper.isLinux()) {
 121             return Path.of("output", name, "app", name + ".cfg").toString();
 122         } else {
 123             throw new AssertionError("Cannot detect platform");
 124         }
 125     }
 126 
 127     // Returns path including executable to java in image runtime folder
 128     public static String getRuntimeJava() {
 129         return getRuntimeJava("test");
 130     }
 131 
 132     public static String getRuntimeJava(String name) {
 133         if (JPackageHelper.isWindows()) {
 134             return Path.of("output", name,
 135                     "runtime", "bin", "java.exe").toString();
 136         } else if (JPackageHelper.isOSX()) {
 137             return Path.of("output", name + ".app", "Contents",
 138                     "runtime", "Contents", "Home", "bin", "java").toString();
 139         } else if (JPackageHelper.isLinux()) {
 140             return Path.of("output", name,
 141                     "runtime", "bin", "java").toString();
 142         } else {
 143             throw new AssertionError("Cannot detect platform");
 144         }
 145     }
 146 
 147     // Returns output file name generate by test application
 148     public static String getAppOutputFile() {
 149         return "appOutput.txt";
 150     }
 151 
 152     // Returns path to bin folder in image runtime
 153     public static String getRuntimeBin() {
 154         return getRuntimeBin("test");
 155     }
 156 
 157     public static String getRuntimeBin(String name) {
 158         if (JPackageHelper.isWindows()) {
 159             return Path.of("output", name, "runtime", "bin").toString();
 160         } else if (JPackageHelper.isOSX()) {
 161             return Path.of("output", name + ".app",
 162                     "Contents", "runtime",
 163                     "Contents", "Home", "bin").toString();
 164         } else if (JPackageHelper.isLinux()) {
 165             return Path.of("output", name, "runtime", "bin").toString();
 166         } else {
 167             throw new AssertionError("Cannot detect platform");
 168         }
 169     }
 170 
 171     public static String getWinProgramFiles() {
 172         return WIN_PROGRAM_FILES;
 173     }
 174 
 175     public static String getWinUserLocal() {
 176         return Path.of(System.getProperty("user.home"),
 177                 "AppData", "Local").toString();
 178     }
 179 
 180     public static String getWinStartMenu() {
 181         return WIN_START_MENU;
 182     }
 183 
 184     public static String getWinPublicDesktop() {
 185         return WIN_PUBLIC_DESKTOP;
 186     }
 187 
 188     public static String getWinUserLocalStartMenu() {
 189         return Path.of(System.getProperty("user.home"), "AppData", "Roaming",
 190                 "Microsoft", "Windows", "Start Menu", "Programs").toString();
 191     }
 192 
 193     public static String getWinInstalledApp(String testName) {
 194         return Path.of(getWinProgramFiles(), testName,
 195                 testName + ".exe").toString();
 196     }
 197 
 198     public static String getWinInstalledApp(String installDir,
 199             String testName) {
 200         return Path.of(getWinProgramFiles(), installDir, "bin",
 201                 testName + ".exe").toString();
 202     }
 203 
 204     public static String getOSXInstalledApp(String testName) {
 205         return File.separator + "Applications"
 206                 + File.separator + testName + ".app"
 207                 + File.separator + "Contents"
 208                 + File.separator + "MacOS"
 209                 + File.separator + testName;
 210     }
 211 
 212     public static String getLinuxInstalledApp(String testName) {
 213         return File.separator + "opt"
 214                 + File.separator + testName
 215                 + File.separator + testName;
 216     }
 217 
 218     public static String getOSXInstalledApp(String subDir, String testName) {
 219         return File.separator + "Applications"
 220                 + File.separator + subDir
 221                 + File.separator + testName + ".app"
 222                 + File.separator + "Contents"
 223                 + File.separator + "MacOS"
 224                 + File.separator + testName;
 225     }
 226 
 227     public static String getLinuxInstalledApp(String subDir, String testName) {
 228         return File.separator + "opt"
 229                 + File.separator + subDir
 230                 + File.separator + testName
 231                 + File.separator + testName;
 232     }
 233 
 234     public static String getWinInstallFolder(String testName) {
 235         return getWinProgramFiles()
 236                 + File.separator + testName;
 237     }
 238 
 239     public static String getLinuxInstallFolder(String testName) {
 240         return File.separator + "opt"
 241                 + File.separator + testName;
 242     }
 243 
 244     public static String getLinuxInstallFolder(String subDir, String testName) {
 245         if (testName == null) {
 246             return File.separator + "opt"
 247                     + File.separator + subDir;
 248         } else {
 249             return File.separator + "opt"
 250                     + File.separator + subDir
 251                     + File.separator + testName;
 252         }
 253     }
 254 
 255     public static String getWinUserLocalInstalledApp(String testName) {
 256         return getWinUserLocal()
 257                 + File.separator + testName
 258                 + File.separator + "bin"
 259                 + File.separator + testName + ".exe";
 260     }
 261 
 262     public static String getWinUserLocalInstallFolder(String testName) {
 263         return getWinUserLocal() + File.separator + testName;
 264     }
 265 
 266     // Returs path to test license file
 267     public static String getLicenseFilePath() {
 268         String path = JPackagePath.getTestSrcRoot()
 269                 + File.separator + "resources"
 270                 + File.separator + "license.txt";
 271 
 272         return path;
 273     }
 274 
 275     // Returns path to app folder of installed application
 276     public static String getWinInstalledAppFolder(String testName) {
 277         return getWinProgramFiles()
 278                 + File.separator + testName
 279                 + File.separator + "app";
 280     }
 281 }