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.sun.javafx.tools.packager.Log;
  30 import com.sun.javafx.tools.packager.bundlers.*;
  31 import org.junit.After;
  32 import org.junit.Assume;
  33 import org.junit.Before;
  34 import org.junit.BeforeClass;
  35 import org.junit.Test;
  36 
  37 import java.io.ByteArrayOutputStream;
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.io.PrintStream;
  41 import java.nio.file.Files;
  42 import java.util.*;
  43 
  44 import static com.oracle.bundlers.StandardBundlerParam.*;
  45 import static org.junit.Assert.assertNotNull;
  46 import static org.junit.Assert.assertTrue;
  47 
  48 public class MacAppStoreBundlerTest {
  49 
  50     static File tmpBase;
  51     static File workDir;
  52     static File appResourcesDir;
  53     static File fakeMainJar;
  54     static Set<File> appResources;
  55     static boolean retain = false;
  56 
  57     @BeforeClass
  58     public static void prepareApp() throws IOException {
  59         // only run on mac
  60         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().contains("os x"));
  61 
  62         // make sure we have a default signing key
  63         String signingKeyName = MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>());
  64         Assume.assumeNotNull(signingKeyName);
  65         try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos)) {
  66             System.err.println("Checking for valid certificate");
  67             ProcessBuilder pb = new ProcessBuilder(
  68                     "security",
  69                     "find-certificate", "-c", signingKeyName);
  70 
  71             IOUtils.exec(pb, Log.isDebug(), false, ps);
  72 
  73             String commandOutput = baos.toString();
  74             Assume.assumeTrue(commandOutput.contains(signingKeyName));
  75             System.err.println("Valid certificate present");
  76         }
  77 
  78 
  79         Log.setLogger(new Log.Logger(true));
  80 
  81         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  82 
  83         workDir = new File("build/tmp/tests", "macappstore");
  84         appResourcesDir = new File("build/tmp/tests", "appResources");
  85         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  86 
  87         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
  88     }
  89 
  90     @Before
  91     public void createTmpDir() throws IOException {
  92         if (retain) {
  93             tmpBase = new File("build/tmp/tests/macappstore");
  94         } else {
  95             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  96         }
  97         tmpBase.mkdir();
  98     }
  99 
 100     @After
 101     public void maybeCleanupTmpDir() {
 102         if (!retain) {
 103             attemptDelete(tmpBase);
 104         }
 105     }
 106 
 107     private void attemptDelete(File tmpBase) {
 108         if (tmpBase.isDirectory()) {
 109             File[] children = tmpBase.listFiles();
 110             if (children != null) {
 111                 for (File f : children) {
 112                     attemptDelete(f);
 113                 }
 114             }
 115         }
 116         boolean success;
 117         try {
 118             success = !tmpBase.exists() || tmpBase.delete();
 119         } catch (SecurityException se) {
 120             success = false;
 121         }
 122         if (!success) {
 123             System.err.println("Could not clean up " + tmpBase.toString());
 124         }
 125     }
 126 
 127     @Test
 128     public void showSigningKeyNames() {
 129         System.err.println(MacAppStoreBundler.MAC_APP_STORE_SIGNING_KEY_USER.fetchFrom(new TreeMap<>()));
 130         System.err.println(MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>()));
 131     }
 132 
 133     /**
 134      * See if smoke comes out
 135      */
 136     @Test
 137     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 138         AbstractBundler bundler = new MacAppStoreBundler();
 139 
 140         assertNotNull(bundler.getName());
 141         assertNotNull(bundler.getID());
 142         assertNotNull(bundler.getDescription());
 143 
 144         Map<String, Object> bundleParams = new HashMap<>();
 145 
 146         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 147 
 148         bundleParams.put(APP_NAME.getID(), "Smoke");
 149         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 150         bundleParams.put(IDENTIFIER.getID(), "com.example.javapacakger.hello.TestPackager");
 151         bundleParams.put(MacAppBundler.MAC_CATEGORY.getID(), "public.app-category.developer-tools");
 152         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 153         bundleParams.put(VERBOSE.getID(), true);
 154 
 155         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 156         System.err.println("Bundle at - " + result);
 157         assertNotNull(result);
 158         assertTrue(result.exists());
 159     }
 160 
 161 //    /**
 162 //     * The bare minimum configuration needed to make it work
 163 //     * <ul>
 164 //     *     <li>Where to build it</li>
 165 //     *     <li>The jar containing the application (with a main-class attribute)</li>
 166 //     * </ul>
 167 //     *
 168 //     * All other values will be driven off of those two values.
 169 //     */
 170 //    @Test
 171 //    public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 172 //        Bundler bundler = new MacAppStoreBundler();
 173 //
 174 //        Map<String, Object> bundleParams = new HashMap<>();
 175 //
 176 //        bundleParams.put(StandardBundlerParam.BUILD_ROOT.getID(), tmpBase);
 177 //
 178 //        bundleParams.put(StandardBundlerParam.APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 179 //
 180 //        File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 181 //        System.err.println("Bundle written to " + output);
 182 //        assertTrue(output.isFile());
 183 //        //TODO assert file name
 184 //    }
 185 }