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.mac;
  27 
  28 import com.oracle.bundlers.AbstractBundler;
  29 import com.oracle.bundlers.Bundler;
  30 import com.sun.javafx.tools.packager.Log;
  31 import com.sun.javafx.tools.packager.bundlers.*;
  32 import org.junit.After;
  33 import org.junit.Assume;
  34 import org.junit.Before;
  35 import org.junit.BeforeClass;
  36 import org.junit.Test;
  37 
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.nio.file.Files;
  41 import java.util.*;
  42 
  43 import static com.oracle.bundlers.StandardBundlerParam.*;
  44 import static com.sun.javafx.tools.packager.bundlers.MacAppBundler.DEVELOPER_ID_APP_SIGNING_KEY;
  45 import static com.sun.javafx.tools.packager.bundlers.MacAppBundler.MAC_CF_BUNDLE_NAME;
  46 import static org.junit.Assert.assertNotNull;
  47 import static org.junit.Assert.assertTrue;
  48 
  49 public class MacAppBundlerTest {
  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 mac
  61         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().contains("os x"));
  62 
  63         Log.setLogger(new Log.Logger(true));
  64 
  65         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  66 
  67         workDir = new File("build/tmp/tests", "macapp");
  68         appResourcesDir = new File("build/tmp/tests", "appResources");
  69         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  70 
  71         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
  72     }
  73 
  74     @Before
  75     public void createTmpDir() throws IOException {
  76         if (retain) {
  77             tmpBase = new File("build/tmp/tests/macapp");
  78         } else {
  79             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  80         }
  81         tmpBase.mkdir();
  82     }
  83 
  84     @After
  85     public void maybeCleanupTmpDir() {
  86         if (!retain) {
  87             attemptDelete(tmpBase);
  88         }
  89     }
  90 
  91     private void attemptDelete(File tmpBase) {
  92         if (tmpBase.isDirectory()) {
  93             File[] children = tmpBase.listFiles();
  94             if (children != null) {
  95                 for (File f : children) {
  96                     attemptDelete(f);
  97                 }
  98             }
  99         }
 100         boolean success;
 101         try {
 102             success = !tmpBase.exists() || tmpBase.delete();
 103         } catch (SecurityException se) {
 104             success = false;
 105         }
 106         if (!success) {
 107             System.err.println("Could not clean up " + tmpBase.toString());
 108         }
 109     }
 110 
 111     /**
 112      * See if smoke comes out
 113      */
 114     @Test
 115     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 116         AbstractBundler bundler = new MacAppBundler();
 117 
 118         assertNotNull(bundler.getName());
 119         assertNotNull(bundler.getID());
 120         assertNotNull(bundler.getDescription());
 121         //assertNotNull(bundler.getBundleParameters());
 122 
 123         Map<String, Object> bundleParams = new HashMap<>();
 124 
 125         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 126 
 127         bundleParams.put(APP_NAME.getID(), "Smoke Test App");
 128         bundleParams.put(MAC_CF_BUNDLE_NAME.getID(), "Smoke");
 129         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 130         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 131         bundleParams.put(MAIN_JAR.getID(),
 132                 new RelativeFileSet(fakeMainJar.getParentFile(),
 133                         new HashSet<>(Arrays.asList(fakeMainJar)))
 134         );
 135         bundleParams.put(MAIN_JAR_CLASSPATH.getID(), fakeMainJar.toString());
 136         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 137         bundleParams.put(VERBOSE.getID(), true);
 138         bundleParams.put(DEVELOPER_ID_APP_SIGNING_KEY.getID(), null); // force no signing
 139         bundleParams.put(ICON.getID(), "java-logo2.gif"); // force no signing
 140 
 141         boolean valid = bundler.validate(bundleParams);
 142         assertTrue(valid);
 143 
 144         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 145         System.err.println("Bundle at - " + result);
 146         assertNotNull(result);
 147         assertTrue(result.exists());
 148     }
 149 
 150     /**
 151      * Build smoke test and mark it as quarantined, possibly signed
 152      */
 153     @Test
 154     public void quarantinedAppTest() throws IOException, ConfigException, UnsupportedPlatformException {
 155         AbstractBundler bundler = new MacAppBundler();
 156 
 157         assertNotNull(bundler.getName());
 158         assertNotNull(bundler.getID());
 159         assertNotNull(bundler.getDescription());
 160         //assertNotNull(bundler.getBundleParameters());
 161 
 162         Map<String, Object> bundleParams = new HashMap<>();
 163 
 164         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 165 
 166         bundleParams.put(APP_NAME.getID(), "Quarantined Test App");
 167         bundleParams.put(MAC_CF_BUNDLE_NAME.getID(), "Quarantine");
 168         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 169         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 170         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 171         bundleParams.put(VERBOSE.getID(), true);
 172 
 173         boolean valid = bundler.validate(bundleParams);
 174         assertTrue(valid);
 175 
 176         File result = bundler.execute(bundleParams, new File(workDir, "quarantine"));
 177         System.err.println("Bundle at - " + result);
 178         assertNotNull(result);
 179         assertTrue(result.exists());
 180 
 181         // mark it as though it's been downloaded
 182         ProcessBuilder pb = new ProcessBuilder(
 183                 "xattr", "-w", "com.apple.quarantine",
 184                 "0000;" + Long.toHexString(System.currentTimeMillis() / 1000L) + ";Java Unit Tests;|com.oracle.jvm.8u",
 185                 result.toString());
 186         IOUtils.exec(pb, true);
 187     }
 188 
 189     /**
 190      * The bare minimum configuration needed to make it work
 191      * <ul>
 192      *     <li>Where to build it</li>
 193      *     <li>The jar containing the application (with a main-class attribute)</li>
 194      * </ul>
 195      *
 196      * All other values will be driven off of those two values.
 197      */
 198     @Test
 199     public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 200         Bundler bundler = new MacAppBundler();
 201 
 202         Map<String, Object> bundleParams = new HashMap<>();
 203 
 204         // not part of the typical setup, for testing
 205         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 206 
 207         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 208 
 209         File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 210         System.err.println("Bundle at - " + output);
 211         assertNotNull(output);
 212         assertTrue(output.exists());
 213     }
 214 }