1 /*
   2  * Copyright (c) 2014, 2015, 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;
  27 
  28 import com.oracle.tools.packager.linux.LinuxAppBundler;
  29 import com.sun.javafx.tools.packager.PackagerException;
  30 import org.junit.*;
  31 
  32 import java.io.ByteArrayOutputStream;
  33 import java.io.File;
  34 import java.io.FileOutputStream;
  35 import java.io.PrintStream;
  36 import java.util.ResourceBundle;
  37 
  38 public class CLITest {
  39 
  40     static File tmpBase;
  41     static File workDir;
  42     static File appResourcesDir;
  43     static File fakeMainJar;
  44     static String runtimeJdk;
  45     static String runtimeJre;
  46 
  47     @BeforeClass
  48     public static void prepareApp() {
  49         Log.setLogger(new Log.Logger(true));
  50         Log.setDebug(true);
  51 
  52         String tmpBasePath = System.getProperty("tmpBase");
  53         if (tmpBasePath != null) {
  54             tmpBase = new File(System.getProperty("tmpBase"));
  55         } else {
  56             tmpBase = new File("build/tmp/tests");
  57         }
  58         tmpBase.mkdirs();
  59 
  60         workDir = new File(tmpBase, "cliapp");
  61         appResourcesDir = new File(tmpBase, "appResources");
  62         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  63 
  64         runtimeJdk = System.getenv("PACKAGER_JDK_ROOT");
  65         runtimeJre = System.getenv("PACKAGER_JRE_ROOT");
  66         
  67     }
  68 
  69     @Test
  70     public void simpleTest() throws Exception {
  71         // on mac, require a full test
  72         Assume.assumeTrue(!System.getProperty("os.name").toLowerCase().contains("os x") || Boolean.parseBoolean(System.getProperty("FULL_TEST")));
  73 
  74         com.sun.javafx.tools.packager.Main.main("-deploy",
  75                 "-verbose", // verbose is required or test will call System.exit() on failures and break the build
  76                 "-srcfiles", fakeMainJar.getCanonicalPath(),
  77                 "-outdir", workDir.getCanonicalPath(),
  78                 "-outfile", "SimpleTest",
  79                 "-appclass", "hello.HelloRectangle",
  80                 "-native", "jnlp",
  81                 "-name", "SimpleTest");
  82     }
  83 
  84     @Test
  85     public void smokeParams() throws Exception {
  86         File f = File.createTempFile("fx-param-test", ".properties");
  87         try (FileOutputStream fos = new FileOutputStream(f);
  88             PrintStream ps = new PrintStream(fos)) 
  89         {
  90             ps.println("param1=foo");
  91             ps.println("param2=bar");
  92             
  93             com.sun.javafx.tools.packager.Main.main("-deploy",
  94                     "-verbose", // verbose is required or test will call System.exit() on failures and break the build
  95                     "-srcfiles", fakeMainJar.getCanonicalPath(),
  96                     "-outdir", workDir.getCanonicalPath(),
  97                     "-outfile", "SmokeParams",
  98                     "-appclass", "hello.HelloRectangle",
  99                     "-nosign",
 100                     "-preloader", "hello.HelloPreloader",
 101                     "-argument", "argument1",
 102                     "-argument", "argument2",
 103                     "-paramFile", f.getPath(),
 104                     "-native", "image",
 105                     "-name", "SmokeParams",
 106                     "-BOptionThatWillNeverExist=true",
 107                     "-BuserJvmOptions=-Xmx=1g",
 108                     "-BuserJvmOptions=-Xms=512m",
 109                     "-BdesktopHint=false",
 110                     "-BshortcutHint=true",
 111                     "-Bruntime=" + (runtimeJdk == null ? System.getProperty("java.home") : runtimeJdk));
 112         }
 113     }
 114 
 115     @Test
 116     public void propsViaBundlerArgs() throws Exception {
 117         com.sun.javafx.tools.packager.Main.main("-deploy",
 118                 "-verbose", // verbose is required or test will call System.exit() on failures and break the build
 119                 "-srcfiles", fakeMainJar.getCanonicalPath(),
 120                 "-outdir", workDir.getCanonicalPath(),
 121                 "-outfile", "PropsViaBundlerArgs",
 122                 "-appclass", "hello.HelloRectangle",
 123                 "-native", "image",
 124                 "-name", "PropsViaBundlerArgs",
 125                 "-BjvmOptions=-Dsqe.foo.bar=baz -Dsqe.qux.corge=grault",
 126                 "-BuserJvmOptions=-Xmx=1g\n-Xms=512m",
 127                 "-BjvmProperties=sqe.aba.caba=dabacaba",
 128                 "-Bruntime=" + (runtimeJre == null ? System.getProperty("java.home") : runtimeJre)
 129         );
 130     }
 131 
 132     @Test(expected = PackagerException.class)
 133     public void duplicateNameClash() throws Exception {
 134         com.sun.javafx.tools.packager.Main.main("-deploy",
 135                 "-verbose", // verbose is required or test will call System.exit() on failures and break the build
 136                 "-srcfiles", fakeMainJar.getCanonicalPath(),
 137                 "-outdir", workDir.getCanonicalPath(),
 138                 "-outfile", "DuplicateNameClash",
 139                 "-appclass", "hello.HelloRectangle",
 140                 "-native", "image",
 141                 "-name", "DuplicateNameClash",
 142                 "-Bname=DuplicateTest");
 143     }
 144 
 145     @Test(expected = PackagerException.class)
 146     public void duplicateNameMatch() throws Exception {
 147         com.sun.javafx.tools.packager.Main.main("-deploy",
 148                 "-verbose", // verbose is required or test will call System.exit() on failures and break the build
 149                 "-srcfiles", fakeMainJar.getCanonicalPath(),
 150                 "-outdir", workDir.getCanonicalPath(),
 151                 "-outfile", "DuplicateNameMatch",
 152                 "-appclass", "hello.HelloRectangle",
 153                 "-native", "image",
 154                 "-name", "DuplicateNameMatch",
 155                 "-Bname=DuplicateNameMatch");
 156     }
 157 
 158     @Test(expected = PackagerException.class)
 159     public void invalidSrcDir() throws Exception {
 160         com.sun.javafx.tools.packager.Main.main("-deploy",
 161                 "-verbose", // verbose is required or test will call System.exit() on failures and break the build
 162                 "-srcdir", fakeMainJar.getCanonicalPath(), // should be a directory, not a jar
 163                 "-outdir", workDir.getCanonicalPath(),
 164                 "-outfile", "InvalidSrcDir",
 165                 "-appclass", "hello.HelloRectangle",
 166                 "-native", "image",
 167                 "-name", "InvalidSrcDir");
 168     }
 169 
 170     @Test
 171     public void userJvmArgNoValue() throws Exception {
 172         PrintStream oldOut = System.out;
 173         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
 174              PrintStream outStr = new PrintStream(baos)) 
 175         {
 176             System.setOut(outStr);
 177             com.sun.javafx.tools.packager.Main.main("-deploy",
 178                     "-verbose", // verbose is required or test will call System.exit() on failures and break the build
 179                     "-srcfiles", fakeMainJar.getCanonicalPath(),
 180                     "-outdir", workDir.getCanonicalPath(),
 181                     "-outfile", "DuplicateNameMatch",
 182                     "-appclass", "hello.HelloRectangle",
 183                     "-native", "image",
 184                     "-nosign",
 185                     "-name", "UserJvmArgNoValue",
 186                     "-BuserJvmOptions=-Xmx1g",
 187                     "-BuserJvmOptions=-Xms512m");
 188             ResourceBundle I18N = ResourceBundle.getBundle(LinuxAppBundler.class.getName());
 189             
 190             outStr.flush();
 191             oldOut.println(baos);
 192             Assert.assertTrue("Look for expected failure message", 
 193                     baos.toString().contains(I18N.getString("error.empty-user-jvm-option-value.advice")));
 194         } finally {
 195             System.setOut(oldOut);
 196         }
 197     }
 198 }