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.tools.packager.linux;
  27 
  28 import com.oracle.tools.packager.Bundler;
  29 import com.oracle.tools.packager.Log;
  30 import com.oracle.tools.packager.ConfigException;
  31 import com.oracle.tools.packager.RelativeFileSet;
  32 import com.oracle.tools.packager.UnsupportedPlatformException;
  33 import org.junit.After;
  34 import org.junit.Assume;
  35 import org.junit.Before;
  36 import org.junit.BeforeClass;
  37 import org.junit.Test;
  38 
  39 import java.io.File;
  40 import java.io.IOException;
  41 import java.nio.file.Files;
  42 import java.util.*;
  43 
  44 import static com.oracle.tools.packager.StandardBundlerParam.*;
  45 import static org.junit.Assert.assertNotNull;
  46 import static org.junit.Assert.assertTrue;
  47 
  48 public class LinuxDebBundlerTest {
  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() {
  59         // only run on linux
  60         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().startsWith("linux"));
  61 
  62         Log.setLogger(new Log.Logger(true));
  63 
  64         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  65 
  66         workDir = new File("build/tmp/tests", "linuxdeb");
  67         appResourcesDir = new File("build/tmp/tests", "appResources");
  68         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  69 
  70         appResources = new HashSet<>(Arrays.asList(fakeMainJar,
  71                 new File(appResourcesDir, "LICENSE"),
  72                 new File(appResourcesDir, "LICENSE2")
  73         ));
  74     }
  75 
  76     @Before
  77     public void createTmpDir() throws IOException {
  78         if (retain) {
  79             tmpBase = new File("build/tmp/tests/linuxdeb");
  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 LinuxDebBundler();
 119 
 120         assertNotNull(bundler.getName());
 121         assertNotNull(bundler.getID());
 122         assertNotNull(bundler.getDescription());
 123         //assertNotNull(bundler.getBundleParameters());
 124 
 125         Map<String, Object> bundleParams = new HashMap<>();
 126 
 127         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 128 
 129         bundleParams.put(APP_NAME.getID(), "Smoke");
 130         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 131         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 132         bundleParams.put(MAIN_JAR.getID(),
 133                 new RelativeFileSet(fakeMainJar.getParentFile(),
 134                         new HashSet<>(Arrays.asList(fakeMainJar)))
 135         );
 136         bundleParams.put(MAIN_JAR_CLASSPATH.getID(), fakeMainJar.toString());
 137         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 138         bundleParams.put(LICENSE_FILE.getID(), Arrays.asList("LICENSE", "LICENSE2"));
 139         bundleParams.put(LICENSE_TYPE.getID(), "GPL2 + Classpath Exception");
 140         bundleParams.put(VERBOSE.getID(), true);
 141         bundleParams.put(ICON.getID(), "java-logo2.gif"); // force no signing
 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 LinuxDebBundler();
 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     @Test(expected = ConfigException.class)
 178     public void invalidLicenseFile() throws ConfigException, UnsupportedPlatformException {
 179         Bundler bundler = new LinuxDebBundler();
 180 
 181         Map<String, Object> bundleParams = new HashMap<>();
 182 
 183         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 184 
 185         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 186         bundleParams.put(LICENSE_FILE.getID(), "BOGUS_LICENSE");
 187 
 188         bundler.validate(bundleParams);
 189     }
 190 }