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.linux;
  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.LinuxAppBundler;
  33 import com.sun.javafx.tools.packager.bundlers.RelativeFileSet;
  34 import com.sun.javafx.tools.packager.bundlers.UnsupportedPlatformException;
  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.FileInputStream;
  43 import java.io.IOException;
  44 import java.nio.file.Files;
  45 import java.util.*;
  46 
  47 import static org.junit.Assert.assertFalse;
  48 import static org.junit.Assert.assertNotNull;
  49 import static org.junit.Assert.assertTrue;
  50 
  51 public class LinuxAppBundlerTest {
  52 
  53     static File tmpBase;
  54     static File workDir;
  55     static File appResourcesDir;
  56     static File fakeMainJar;
  57     static Set<File> appResources;
  58     static boolean retain = false;
  59 
  60     @BeforeClass
  61     public static void prepareApp() {
  62         // only run on linux
  63         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().startsWith("linux"));
  64 
  65         Log.setLogger(new Log.Logger(true));
  66 
  67         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  68 
  69         workDir = new File("build/tmp/tests", "linuxapp");
  70         appResourcesDir = new File("build/tmp/tests", "appResources");
  71         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  72 
  73         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
  74     }
  75 
  76     @Before
  77     public void createTmpDir() throws IOException {
  78         if (retain) {
  79             tmpBase = new File("build/tmp/tests/linuxapp");
  80         } else {
  81             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  82         }
  83         tmpBase.mkdir();
  84     }
  85 
  86     @After
  87     public void maybeCleanupTmpDir() {
  88         if (!retain) {
  89             attemptDelete(tmpBase);
  90         }
  91     }
  92 
  93     private void attemptDelete(File tmpBase) {
  94         if (tmpBase.isDirectory()) {
  95             File[] children = tmpBase.listFiles();
  96             if (children != null) {
  97                 for (File f : children) {
  98                     attemptDelete(f);
  99                 }
 100             }
 101         }
 102         boolean success;
 103         try {
 104             success = !tmpBase.exists() || tmpBase.delete();
 105         } catch (SecurityException se) {
 106             success = false;
 107         }
 108         if (!success) {
 109             System.err.println("Could not clean up " + tmpBase.toString());
 110         }
 111     }
 112 
 113     /**
 114      * See if smoke comes out
 115      */
 116     @Test
 117     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 118         Bundler bundler = new LinuxAppBundler();
 119         ((LinuxAppBundler)bundler).setVerbose(true);
 120         
 121         assertNotNull(bundler.getName());
 122         assertNotNull(bundler.getID());
 123         assertNotNull(bundler.getDescription());
 124         //assertNotNull(bundler.getBundleParameters());
 125 
 126         Map<String, Object> bundleParams = new HashMap<>();
 127 
 128         bundleParams.put(StandardBundlerParam.BUILD_ROOT.getID(), tmpBase);
 129 
 130         bundleParams.put(StandardBundlerParam.NAME.getID(), "Smoke");
 131         bundleParams.put(StandardBundlerParam.MAIN_CLASS.getID(), "hello.TestPackager");
 132         bundleParams.put(StandardBundlerParam.APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 133         
 134         File output = bundler.execute(bundleParams, new File(workDir, "smoke"));
 135         validatePackageCfg(output);
 136     }
 137 
 138     /**
 139      * The bare minimum configuration needed to make it work
 140      * <ul>
 141      *     <li>Where to build it</li>
 142      *     <li>The jar containing the application (with a main-class attribute)</li>
 143      * </ul>
 144      * 
 145      * All other values will be driven off of those two values.
 146      */
 147     @Test
 148     public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 149         Bundler bundler = new LinuxAppBundler();
 150         ((LinuxAppBundler)bundler).setVerbose(true);
 151 
 152         Map<String, Object> bundleParams = new HashMap<>();
 153 
 154         bundleParams.put(StandardBundlerParam.BUILD_ROOT.getID(), tmpBase);
 155 
 156         bundleParams.put(StandardBundlerParam.APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 157 
 158         File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 159         validatePackageCfg(output);
 160         assertTrue(output.isDirectory());
 161     }
 162 
 163     public void validatePackageCfg(File root) throws IOException {
 164         try (FileInputStream fis = new FileInputStream(new File(root, "app/package.cfg"))) {
 165             Properties p = new Properties();
 166             p.load(fis);
 167 
 168             // - verify we have app.mainjar, app.version, app.preferences, app.mainclass, and app.classpath
 169             assertNotNull(p.getProperty("app.mainjar"));
 170             assertNotNull(p.getProperty("app.version"));
 171             assertNotNull(p.getProperty("app.preferences.id"));
 172             assertNotNull(p.getProperty("app.mainclass"));
 173             assertNotNull(p.getProperty("app.classpath"));
 174 
 175             // - make sure 'app.classpath=null' doesn't show up, prefer 'app.classpath='
 176             assertFalse(p.getProperty("app.classpath").equals("null"));
 177         }
 178     }
 179 
 180 
 181 }