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