1 /*
   2  * Copyright (c) 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.tools.packager.windows;
  27 
  28 import com.oracle.tools.packager.Bundler;
  29 import com.oracle.tools.packager.Log;
  30 import com.oracle.tools.packager.ConfigException;
  31 import com.oracle.tools.packager.RelativeFileSet;
  32 import com.oracle.tools.packager.UnsupportedPlatformException;
  33 import org.junit.After;
  34 import org.junit.Assume;
  35 import org.junit.Before;
  36 import org.junit.BeforeClass;
  37 import org.junit.Test;
  38 
  39 import java.io.File;
  40 import java.io.IOException;
  41 import java.nio.file.Files;
  42 import java.util.*;
  43 
  44 import static com.oracle.tools.packager.StandardBundlerParam.*;
  45 import static org.junit.Assert.assertNotNull;
  46 import static org.junit.Assert.assertNull;
  47 import static org.junit.Assert.assertTrue;
  48 
  49 public class WinExeBundlerTest {
  50 
  51     static File tmpBase;
  52     static File workDir;
  53     static File appResourcesDir;
  54     static File fakeMainJar;
  55     static Set<File> appResources;
  56     static boolean retain = false;
  57 
  58     @BeforeClass
  59     public static void prepareApp() {
  60         // only run on windows
  61         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().startsWith("win"));
  62 
  63         // only run if we have InnoSetup installed
  64         Assume.assumeNotNull(WinExeBundler.TOOL_INNO_SETUP_COMPILER_EXECUTABLE.fetchFrom(new HashMap<>()));
  65 
  66         Log.setLogger(new Log.Logger(true));
  67 
  68         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  69 
  70         workDir = new File("build/tmp/tests", "winexe");
  71         appResourcesDir = new File("build/tmp/tests", "appResources");
  72         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  73 
  74         appResources = new HashSet<>(Arrays.asList(fakeMainJar,
  75                 new File(appResourcesDir, "LICENSE"),
  76                 new File(appResourcesDir, "LICENSE2")
  77         ));
  78     }
  79 
  80     @Before
  81     public void createTmpDir() throws IOException {
  82         if (retain) {
  83             tmpBase = new File("build/tmp/tests/winexe");
  84         } else {
  85             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  86         }
  87         tmpBase.mkdir();
  88     }
  89 
  90     @After
  91     public void maybeCleanupTmpDir() {
  92         if (!retain) {
  93             attemptDelete(tmpBase);
  94         }
  95     }
  96 
  97     private void attemptDelete(File tmpBase) {
  98         if (tmpBase.isDirectory()) {
  99             File[] children = tmpBase.listFiles();
 100             if (children != null) {
 101                 for (File f : children) {
 102                     attemptDelete(f);
 103                 }
 104             }
 105         }
 106         boolean success;
 107         try {
 108             success = !tmpBase.exists() || tmpBase.delete();
 109         } catch (SecurityException se) {
 110             success = false;
 111         }
 112         if (!success) {
 113             System.err.println("Could not clean up " + tmpBase.toString());
 114         }
 115     }
 116 
 117     /**
 118      * See if smoke comes out
 119      */
 120     @Test
 121     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 122         Bundler bundler = new WinExeBundler();
 123 
 124         assertNotNull(bundler.getName());
 125         assertNotNull(bundler.getID());
 126         assertNotNull(bundler.getDescription());
 127         //assertNotNull(bundler.getBundleParameters());
 128 
 129         Map<String, Object> bundleParams = new HashMap<>();
 130 
 131         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 132 
 133         bundleParams.put(APP_NAME.getID(), "Smoke");
 134         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 135         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 136         bundleParams.put(MAIN_JAR.getID(),
 137                 new RelativeFileSet(fakeMainJar.getParentFile(),
 138                         new HashSet<>(Arrays.asList(fakeMainJar)))
 139         );
 140         bundleParams.put(MAIN_JAR_CLASSPATH.getID(), fakeMainJar.toString());
 141         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 142         bundleParams.put(LICENSE_FILE.getID(), Arrays.asList("LICENSE", "LICENSE2"));
 143         bundleParams.put(COPYRIGHT.getID(), "Copyright(c) 2014 the testers \"who like to break stuff\"");
 144         bundleParams.put(VERBOSE.getID(), true);
 145 
 146         boolean valid = bundler.validate(bundleParams);
 147         assertTrue(valid);
 148 
 149         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 150         System.err.println("Bundle at - " + result);
 151         assertNotNull(result);
 152         assertTrue(result.exists());
 153     }
 154 
 155     /**
 156      * The bare minimum configuration needed to make it work
 157      * <ul>
 158      *     <li>Where to build it</li>
 159      *     <li>The jar containing the application (with a main-class attribute)</li>
 160      * </ul>
 161      * 
 162      * All other values will be driven off of those two values.
 163      */
 164     @Test
 165     public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 166         Bundler bundler = new WinExeBundler();
 167 
 168         Map<String, Object> bundleParams = new HashMap<>();
 169 
 170         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 171 
 172         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 173 
 174         File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 175         System.err.println("Bundle at - " + output);
 176         assertNotNull(output);
 177         assertTrue(output.exists());
 178     }
 179 
 180     /**
 181      * Test a misconfiguration where the iscc tools are misconfigured.
 182      */
 183     @Test
 184     public void configISSCBad() throws IOException, ConfigException, UnsupportedPlatformException {
 185         Bundler bundler = new WinExeBundler();
 186 
 187         Map<String, Object> bundleParams = new HashMap<>();
 188 
 189         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 190         bundleParams.put(VERBOSE.getID(), true);
 191 
 192         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 193 
 194         bundleParams.put(WinExeBundler.TOOL_INNO_SETUP_COMPILER_EXECUTABLE.getID(), "c:\\MissingTool.exe");
 195 
 196         File output = bundler.execute(bundleParams, new File(workDir, "BadISSC"));
 197         assertNull(output);
 198     }
 199 
 200     /**
 201      * Test a misconfiguration where the iscc tools are misconfigured.
 202      */
 203     @Test
 204     public void configISSCNull() throws IOException, ConfigException, UnsupportedPlatformException {
 205         Bundler bundler = new WinExeBundler();
 206 
 207         Map<String, Object> bundleParams = new HashMap<>();
 208 
 209         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 210         bundleParams.put(VERBOSE.getID(), true);
 211 
 212         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 213 
 214         bundleParams.put(WinExeBundler.TOOL_INNO_SETUP_COMPILER_EXECUTABLE.getID(), null);
 215 
 216         File output = bundler.execute(bundleParams, new File(workDir, "NullISSC"));
 217         assertNull(output);
 218     }
 219 
 220     @Test(expected = ConfigException.class)
 221     public void invalidLicenseFile() throws ConfigException, UnsupportedPlatformException {
 222         Bundler bundler = new WinExeBundler();
 223 
 224         Map<String, Object> bundleParams = new HashMap<>();
 225 
 226         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 227 
 228         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 229         bundleParams.put(LICENSE_FILE.getID(), "BOGUS_LICENSE");
 230 
 231         bundler.validate(bundleParams);
 232     }
 233 
 234     @Test(expected = ConfigException.class)
 235     public void invalidCopyright() throws ConfigException, UnsupportedPlatformException {
 236         Bundler bundler = new WinExeBundler();
 237 
 238         Map<String, Object> bundleParams = new HashMap<>();
 239 
 240         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 241 
 242         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 243         bundleParams.put(COPYRIGHT.getID(), "Copyright (c) 2014 The Testers\nWho like to break stuff");
 244 
 245         bundler.validate(bundleParams);
 246     }
 247 
 248     @Test(expected = ConfigException.class)
 249     public void longCopyright() throws ConfigException, UnsupportedPlatformException {
 250         Bundler bundler = new WinExeBundler();
 251 
 252         Map<String, Object> bundleParams = new HashMap<>();
 253 
 254         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 255 
 256         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 257         bundleParams.put(COPYRIGHT.getID(), "Copyright (c) 2014 Way to many people to name because there are just so may of them I don't know where to start but I will just start with Alice, Bob, Charlie, Dave, Eve, well, there are just too many to say.");
 258 
 259         bundler.validate(bundleParams);
 260     }
 261 }