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.bundlers.windows;
  27 
  28 import com.oracle.bundlers.Bundler;
  29 import com.sun.javafx.tools.packager.Log;
  30 import com.sun.javafx.tools.packager.bundlers.ConfigException;
  31 import com.sun.javafx.tools.packager.bundlers.RelativeFileSet;
  32 import com.sun.javafx.tools.packager.bundlers.UnsupportedPlatformException;
  33 import com.sun.javafx.tools.packager.bundlers.WinExeBundler;
  34 import org.junit.After;
  35 import org.junit.Assume;
  36 import org.junit.Before;
  37 import org.junit.BeforeClass;
  38 import org.junit.Test;
  39 
  40 import java.io.File;
  41 import java.io.IOException;
  42 import java.nio.file.Files;
  43 import java.util.*;
  44 
  45 import static com.oracle.bundlers.StandardBundlerParam.*;
  46 import static org.junit.Assert.assertNotNull;
  47 import static org.junit.Assert.assertNull;
  48 import static org.junit.Assert.assertTrue;
  49 
  50 public class WinEXEBundlerTest {
  51 
  52     static File tmpBase;
  53     static File workDir;
  54     static File appResourcesDir;
  55     static File fakeMainJar;
  56     static Set<File> appResources;
  57     static boolean retain = false;
  58 
  59     @BeforeClass
  60     public static void prepareApp() {
  61         // only run on windows
  62         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().startsWith("win"));
  63 
  64         // only run if we have InnoSetup installed
  65         Assume.assumeNotNull(WinExeBundler.TOOL_INNO_SETUP_COMPILER_EXECUTABLE.fetchFrom(new HashMap<>()));
  66 
  67         Log.setLogger(new Log.Logger(true));
  68 
  69         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  70 
  71         workDir = new File("build/tmp/tests", "winexe");
  72         appResourcesDir = new File("build/tmp/tests", "appResources");
  73         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  74 
  75         appResources = new HashSet<>(Arrays.asList(fakeMainJar,
  76                 new File(appResourcesDir, "LICENSE"),
  77                 new File(appResourcesDir, "LICENSE2")
  78         ));
  79     }
  80 
  81     @Before
  82     public void createTmpDir() throws IOException {
  83         if (retain) {
  84             tmpBase = new File("build/tmp/tests/winexe");
  85         } else {
  86             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  87         }
  88         tmpBase.mkdir();
  89     }
  90 
  91     @After
  92     public void maybeCleanupTmpDir() {
  93         if (!retain) {
  94             attemptDelete(tmpBase);
  95         }
  96     }
  97 
  98     private void attemptDelete(File tmpBase) {
  99         if (tmpBase.isDirectory()) {
 100             File[] children = tmpBase.listFiles();
 101             if (children != null) {
 102                 for (File f : children) {
 103                     attemptDelete(f);
 104                 }
 105             }
 106         }
 107         boolean success;
 108         try {
 109             success = !tmpBase.exists() || tmpBase.delete();
 110         } catch (SecurityException se) {
 111             success = false;
 112         }
 113         if (!success) {
 114             System.err.println("Could not clean up " + tmpBase.toString());
 115         }
 116     }
 117 
 118     /**
 119      * See if smoke comes out
 120      */
 121     @Test
 122     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 123         Bundler bundler = new WinExeBundler();
 124 
 125         assertNotNull(bundler.getName());
 126         assertNotNull(bundler.getID());
 127         assertNotNull(bundler.getDescription());
 128         //assertNotNull(bundler.getBundleParameters());
 129 
 130         Map<String, Object> bundleParams = new HashMap<>();
 131 
 132         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 133 
 134         bundleParams.put(APP_NAME.getID(), "Smoke");
 135         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 136         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 137         bundleParams.put(MAIN_JAR.getID(),
 138                 new RelativeFileSet(fakeMainJar.getParentFile(),
 139                         new HashSet<>(Arrays.asList(fakeMainJar)))
 140         );
 141         bundleParams.put(MAIN_JAR_CLASSPATH.getID(), fakeMainJar.toString());
 142         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 143         bundleParams.put(LICENSE_FILE.getID(), Arrays.asList("LICENSE", "LICENSE2"));
 144         bundleParams.put(COPYRIGHT.getID(), "Copyright(c) 2014 the testers \"who like to break stuff\"");
 145         bundleParams.put(VERBOSE.getID(), true);
 146 
 147         boolean valid = bundler.validate(bundleParams);
 148         assertTrue(valid);
 149 
 150         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 151         System.err.println("Bundle at - " + result);
 152         assertNotNull(result);
 153         assertTrue(result.exists());
 154     }
 155 
 156     /**
 157      * The bare minimum configuration needed to make it work
 158      * <ul>
 159      *     <li>Where to build it</li>
 160      *     <li>The jar containing the application (with a main-class attribute)</li>
 161      * </ul>
 162      * 
 163      * All other values will be driven off of those two values.
 164      */
 165     @Test
 166     public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 167         Bundler bundler = new WinExeBundler();
 168 
 169         Map<String, Object> bundleParams = new HashMap<>();
 170 
 171         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 172 
 173         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 174 
 175         File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 176         System.err.println("Bundle at - " + output);
 177         assertNotNull(output);
 178         assertTrue(output.exists());
 179     }
 180 
 181     /**
 182      * Test a misconfiguration where the iscc tools are misconfigured.
 183      */
 184     @Test
 185     public void configISSCBad() throws IOException, ConfigException, UnsupportedPlatformException {
 186         Bundler bundler = new WinExeBundler();
 187 
 188         Map<String, Object> bundleParams = new HashMap<>();
 189 
 190         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 191         bundleParams.put(VERBOSE.getID(), true);
 192 
 193         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 194 
 195         bundleParams.put(WinExeBundler.TOOL_INNO_SETUP_COMPILER_EXECUTABLE.getID(), "c:\\MissingTool.exe");
 196 
 197         File output = bundler.execute(bundleParams, new File(workDir, "BadISSC"));
 198         assertNull(output);
 199     }
 200 
 201     /**
 202      * Test a misconfiguration where the iscc tools are misconfigured.
 203      */
 204     @Test
 205     public void configISSCNull() throws IOException, ConfigException, UnsupportedPlatformException {
 206         Bundler bundler = new WinExeBundler();
 207 
 208         Map<String, Object> bundleParams = new HashMap<>();
 209 
 210         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 211         bundleParams.put(VERBOSE.getID(), true);
 212 
 213         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 214 
 215         bundleParams.put(WinExeBundler.TOOL_INNO_SETUP_COMPILER_EXECUTABLE.getID(), null);
 216 
 217         File output = bundler.execute(bundleParams, new File(workDir, "NullISSC"));
 218         assertNull(output);
 219     }
 220 
 221     @Test(expected = ConfigException.class)
 222     public void invalidLicenseFile() throws ConfigException, UnsupportedPlatformException {
 223         Bundler bundler = new WinExeBundler();
 224 
 225         Map<String, Object> bundleParams = new HashMap<>();
 226 
 227         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 228 
 229         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 230         bundleParams.put(LICENSE_FILE.getID(), "BOGUS_LICENSE");
 231 
 232         bundler.validate(bundleParams);
 233     }
 234 
 235     @Test(expected = ConfigException.class)
 236     public void invalidCopyright() throws ConfigException, UnsupportedPlatformException {
 237         Bundler bundler = new WinExeBundler();
 238 
 239         Map<String, Object> bundleParams = new HashMap<>();
 240 
 241         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 242 
 243         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 244         bundleParams.put(COPYRIGHT.getID(), "Copyright (c) 2014 The Testers\nWho like to break stuff");
 245 
 246         bundler.validate(bundleParams);
 247     }
 248 
 249     @Test(expected = ConfigException.class)
 250     public void longCopyright() throws ConfigException, UnsupportedPlatformException {
 251         Bundler bundler = new WinExeBundler();
 252 
 253         Map<String, Object> bundleParams = new HashMap<>();
 254 
 255         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 256 
 257         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 258         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.");
 259 
 260         bundler.validate(bundleParams);
 261     }
 262 }