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.oracle.bundlers.StandardBundlerParam;
  30 import com.sun.javafx.tools.packager.Log;
  31 import com.sun.javafx.tools.packager.bundlers.ConfigException;
  32 import com.sun.javafx.tools.packager.bundlers.RelativeFileSet;
  33 import com.sun.javafx.tools.packager.bundlers.UnsupportedPlatformException;
  34 import com.sun.javafx.tools.packager.bundlers.WinMsiBundler;
  35 import org.junit.After;
  36 import org.junit.Assume;
  37 import org.junit.Before;
  38 import org.junit.BeforeClass;
  39 import org.junit.Test;
  40 
  41 import java.io.File;
  42 import java.io.IOException;
  43 import java.nio.file.Files;
  44 import java.util.*;
  45 
  46 import static org.junit.Assert.assertNotNull;
  47 import static org.junit.Assert.assertTrue;
  48 
  49 public class WinMSIBundlerTest {
  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 Wix tools installed
  64         Assume.assumeNotNull(WinMsiBundler.TOOL_LIGHT_EXECUTABLE.fetchFrom(new HashMap<>()));
  65         Assume.assumeNotNull(WinMsiBundler.TOOL_CANDLE_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", "winmsi");
  72         appResourcesDir = new File("build/tmp/tests", "appResources");
  73         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  74 
  75         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
  76     }
  77 
  78     @Before
  79     public void createTmpDir() throws IOException {
  80         if (retain) {
  81             tmpBase = new File("build/tmp/tests/winmsi");
  82         } else {
  83             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  84         }
  85         tmpBase.mkdir();
  86     }
  87 
  88     @After
  89     public void maybeCleanupTmpDir() {
  90         if (!retain) {
  91             attemptDelete(tmpBase);
  92         }
  93     }
  94 
  95     private void attemptDelete(File tmpBase) {
  96         if (tmpBase.isDirectory()) {
  97             File[] children = tmpBase.listFiles();
  98             if (children != null) {
  99                 for (File f : children) {
 100                     attemptDelete(f);
 101                 }
 102             }
 103         }
 104         boolean success;
 105         try {
 106             success = !tmpBase.exists() || tmpBase.delete();
 107         } catch (SecurityException se) {
 108             success = false;
 109         }
 110         if (!success) {
 111             System.err.println("Could not clean up " + tmpBase.toString());
 112         }
 113     }
 114 
 115     /**
 116      * See if smoke comes out
 117      */
 118     @Test
 119     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 120         Bundler bundler = new WinMsiBundler();
 121         ((WinMsiBundler)bundler).setVerbose(true);
 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(StandardBundlerParam.BUILD_ROOT.getID(), tmpBase);
 131         
 132         bundleParams.put(StandardBundlerParam.NAME.getID(), "Smoke");
 133         bundleParams.put(StandardBundlerParam.MAIN_CLASS.getID(), "hello.TestPackager");
 134         bundleParams.put(StandardBundlerParam.APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 135 
 136         bundler.execute(bundleParams, new File(workDir, "smoke"));
 137     }
 138 
 139     /**
 140      * The bare minimum configuration needed to make it work
 141      * <ul>
 142      *     <li>Where to build it</li>
 143      *     <li>The jar containing the application (with a main-class attribute)</li>
 144      * </ul>
 145      * 
 146      * All other values will be driven off of those two values.
 147      */
 148     @Test
 149     public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 150         Bundler bundler = new WinMsiBundler();
 151         ((WinMsiBundler)bundler).setVerbose(true);
 152 
 153         Map<String, Object> bundleParams = new HashMap<>();
 154 
 155         bundleParams.put(StandardBundlerParam.BUILD_ROOT.getID(), tmpBase);
 156 
 157         bundleParams.put(StandardBundlerParam.APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 158 
 159         File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 160         System.err.println("Bundle written to " + output);
 161         assertTrue(output.isFile());
 162     }
 163 
 164 }