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