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.WinMsiBundler;
  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 WinMSIBundlerTest {
  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 Wix tools installed
  65         Assume.assumeNotNull(WinMsiBundler.TOOL_LIGHT_EXECUTABLE.fetchFrom(new HashMap<>()));
  66         Assume.assumeNotNull(WinMsiBundler.TOOL_CANDLE_EXECUTABLE.fetchFrom(new HashMap<>()));
  67 
  68         Log.setLogger(new Log.Logger(true));
  69 
  70         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  71 
  72         workDir = new File("build/tmp/tests", "winmsi");
  73         appResourcesDir = new File("build/tmp/tests", "appResources");
  74         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  75 
  76         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
  77     }
  78 
  79     @Before
  80     public void createTmpDir() throws IOException {
  81         if (retain) {
  82             tmpBase = new File("build/tmp/tests/winmsi");
  83         } else {
  84             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  85         }
  86         tmpBase.mkdir();
  87     }
  88 
  89     @After
  90     public void maybeCleanupTmpDir() {
  91         if (!retain) {
  92             attemptDelete(tmpBase);
  93         }
  94     }
  95 
  96     private void attemptDelete(File tmpBase) {
  97         if (tmpBase.isDirectory()) {
  98             File[] children = tmpBase.listFiles();
  99             if (children != null) {
 100                 for (File f : children) {
 101                     attemptDelete(f);
 102                 }
 103             }
 104         }
 105         boolean success;
 106         try {
 107             success = !tmpBase.exists() || tmpBase.delete();
 108         } catch (SecurityException se) {
 109             success = false;
 110         }
 111         if (!success) {
 112             System.err.println("Could not clean up " + tmpBase.toString());
 113         }
 114     }
 115 
 116     /**
 117      * See if smoke comes out
 118      */
 119     @Test
 120     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 121         Bundler bundler = new WinMsiBundler();
 122 
 123         assertNotNull(bundler.getName());
 124         assertNotNull(bundler.getID());
 125         assertNotNull(bundler.getDescription());
 126         //assertNotNull(bundler.getBundleParameters());
 127 
 128         Map<String, Object> bundleParams = new HashMap<>();
 129 
 130         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 131         
 132         bundleParams.put(APP_NAME.getID(), "Smoke");
 133         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 134         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 135         bundleParams.put(MAIN_JAR.getID(),
 136                 new RelativeFileSet(fakeMainJar.getParentFile(),
 137                         new HashSet<>(Arrays.asList(fakeMainJar)))
 138         );
 139         bundleParams.put(MAIN_JAR_CLASSPATH.getID(), fakeMainJar.toString());
 140         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 141         bundleParams.put(VERBOSE.getID(), true);
 142 
 143         boolean valid = bundler.validate(bundleParams);
 144         assertTrue(valid);
 145 
 146         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 147         System.err.println("Bundle at - " + result);
 148         assertNotNull(result);
 149         assertTrue(result.exists());
 150     }
 151 
 152     /**
 153      * The bare minimum configuration needed to make it work
 154      * <ul>
 155      *     <li>Where to build it</li>
 156      *     <li>The jar containing the application (with a main-class attribute)</li>
 157      * </ul>
 158      * 
 159      * All other values will be driven off of those two values.
 160      */
 161     @Test
 162     public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 163         Bundler bundler = new WinMsiBundler();
 164 
 165         Map<String, Object> bundleParams = new HashMap<>();
 166 
 167         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 168 
 169         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 170 
 171         File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 172         System.err.println("Bundle at - " + output);
 173         assertNotNull(output);
 174         assertTrue(output.exists());
 175     }
 176 
 177     /**
 178      * Test a misconfiguration where the wix tools are misconfigured.
 179      */
 180     @Test
 181     public void configLightBad() throws IOException, ConfigException, UnsupportedPlatformException {
 182         Bundler bundler = new WinMsiBundler();
 183 
 184         Map<String, Object> bundleParams = new HashMap<>();
 185 
 186         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 187         bundleParams.put(VERBOSE.getID(), true);
 188 
 189         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 190 
 191         bundleParams.put(WinMsiBundler.TOOL_LIGHT_EXECUTABLE.getID(), "c:\\MissingTool.exe");
 192 
 193         File output = bundler.execute(bundleParams, new File(workDir, "BadLight"));
 194         assertNull(output);
 195     }
 196 
 197     /**
 198      * Test a misconfiguration where the wix tools are misconfigured.
 199      */
 200     @Test
 201     public void configLightNull() throws IOException, ConfigException, UnsupportedPlatformException {
 202         Bundler bundler = new WinMsiBundler();
 203 
 204         Map<String, Object> bundleParams = new HashMap<>();
 205 
 206         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 207         bundleParams.put(VERBOSE.getID(), true);
 208 
 209         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 210 
 211         bundleParams.put(WinMsiBundler.TOOL_LIGHT_EXECUTABLE.getID(), null);
 212 
 213         File output = bundler.execute(bundleParams, new File(workDir, "NullLight"));
 214         assertNull(output);
 215     }
 216 
 217     /**
 218      * Test a misconfiguration where the Wix tools are misconfigured.
 219      */
 220     @Test
 221     public void configCandleBad() throws IOException, ConfigException, UnsupportedPlatformException {
 222         Bundler bundler = new WinMsiBundler();
 223 
 224         Map<String, Object> bundleParams = new HashMap<>();
 225 
 226         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 227         bundleParams.put(VERBOSE.getID(), true);
 228 
 229         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 230 
 231         bundleParams.put(WinMsiBundler.TOOL_CANDLE_EXECUTABLE.getID(), "c:\\MissingTool.exe");
 232 
 233         File output = bundler.execute(bundleParams, new File(workDir, "BadCandle"));
 234         assertNull(output);
 235     }
 236 
 237     /**
 238      * Test a misconfiguration where the wix tools are misconfigured.
 239      */
 240     @Test
 241     public void configCandleNull() throws IOException, ConfigException, UnsupportedPlatformException {
 242         Bundler bundler = new WinMsiBundler();
 243 
 244         Map<String, Object> bundleParams = new HashMap<>();
 245 
 246         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 247         bundleParams.put(VERBOSE.getID(), true);
 248 
 249         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 250 
 251         bundleParams.put(WinMsiBundler.TOOL_CANDLE_EXECUTABLE.getID(), null);
 252 
 253         File output = bundler.execute(bundleParams, new File(workDir, "NullCandle"));
 254         assertNull(output);
 255     }
 256 
 257 
 258 }