1 /*
   2  * Copyright (c) 2013, 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 test.launchertest;
  27 
  28 import com.sun.javafx.PlatformUtil;
  29 import java.util.ArrayList;
  30 import java.util.Collection;
  31 import junit.framework.AssertionFailedError;
  32 import org.junit.runners.Parameterized.Parameters;
  33 import org.junit.runner.RunWith;
  34 import org.junit.runners.Parameterized;
  35 import org.junit.Test;
  36 
  37 import static test.launchertest.Constants.*;
  38 import static org.junit.Assume.*;
  39 
  40 /**
  41  * Unit test for FX support in Java 8 launcher
  42  */
  43 @RunWith(Parameterized.class)
  44 public class MainLauncherTest {
  45 
  46     private static final String className = MainLauncherTest.class.getName();
  47     private static final String pkgName = className.substring(0, className.lastIndexOf("."));
  48 
  49     private static Collection params = null;
  50 
  51     public static class TestData {
  52         final String appName;
  53         final String pldrName;
  54         final boolean headless;
  55         final int exitCode;
  56 
  57         public TestData(String appName) {
  58             this(appName, 0);
  59         }
  60 
  61         public TestData(String appName, int exitCode) {
  62             this(appName, null, exitCode);
  63         }
  64 
  65         public TestData(String appName, String pldrName, int exitCode) {
  66             this(appName, pldrName, false, exitCode);
  67         }
  68 
  69         public TestData(String appName, boolean headless, int exitCode) {
  70             this(appName, null, headless, exitCode);
  71         }
  72 
  73         public TestData(String appName, String pldrName, boolean headless, int exitCode) {
  74             this.appName = pkgName + "." + appName;
  75             this.pldrName = pldrName == null ? null : pkgName + "." +  pldrName;
  76             this.headless = headless;
  77             this.exitCode = exitCode;
  78         }
  79     }
  80 
  81     private static final TestData[] testData = {
  82         new TestData("TestApp"),
  83         new TestData("TestAppNoMain"),
  84         new TestData("TestNotApplication"),
  85         new TestData("TestStartupApp1", ERROR_NONE),
  86         new TestData("TestStartupApp2", ERROR_NONE),
  87         new TestData("TestStartupAppNoMain", ERROR_NONE),
  88         new TestData("TestStartupJFXPanel", ERROR_NONE),
  89         new TestData("TestStartupNotApplication", ERROR_NONE),
  90         new TestData("TestAppThreadCheck", ERROR_NONE),
  91         new TestData("TestAppNoMainThreadCheck", ERROR_NONE),
  92         new TestData("TestNotApplicationThreadCheck", ERROR_NONE),
  93         new TestData("TestAppThreadCheck", "TestPreloader", ERROR_NONE),
  94         new TestData("TestAppNoMainThreadCheck", "TestPreloader", ERROR_NONE),
  95         new TestData("TestAppCCL", ERROR_NONE),
  96         new TestData("TestAppCCL1", ERROR_NONE),
  97         new TestData("TestAppCCL2", ERROR_NONE),
  98         new TestData("TestAppNoMainCCL", ERROR_NONE),
  99         new TestData("TestAppNoMainCCL2", ERROR_NONE),
 100         new TestData("TestAppNoMainCCL3", ERROR_NONE),
 101         new TestData("TestNotApplicationCCL", ERROR_NONE),
 102         new TestData("TestHeadlessApp", true, ERROR_NONE),
 103     };
 104 
 105     @Parameters
 106     public static Collection getParams() {
 107         if (params == null) {
 108             params = new ArrayList();
 109             for (TestData data : testData) {
 110                 params.add(new TestData[] { data });
 111             }
 112         }
 113         return params;
 114     }
 115 
 116     private final String testAppName;
 117     private final String testPldrName;
 118     private final boolean headless;
 119     private final int testExitCode;
 120 
 121     public MainLauncherTest(TestData testData) {
 122         this.testAppName = testData.appName;
 123         this.testPldrName = testData.pldrName;
 124         this.headless = testData.headless;
 125         this.testExitCode = testData.exitCode;
 126     }
 127 
 128     @Test (timeout=5000)
 129     public void testMainLauncher() throws Exception {
 130         if (headless) {
 131             // Headless tests currently only run on Linux
 132             assumeTrue(PlatformUtil.isLinux());
 133         }
 134 
 135         final String classpath = System.getProperty("java.class.path");
 136         ProcessBuilder builder;
 137         if (testPldrName != null) {
 138             builder = new ProcessBuilder("java", "-cp", classpath,
 139                     "-Djavafx.preloader=" + testPldrName, testAppName);
 140         } else {
 141             builder = new ProcessBuilder("java", "-cp", classpath, testAppName);
 142         }
 143         if (headless) {
 144             // Set DISPLAY variable to empty to run in headless mode on Linux
 145             builder.environment().put("DISPLAY", "");
 146         }
 147         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
 148         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
 149         Process process = builder.start();
 150         int retVal = process.waitFor();
 151         switch (retVal) {
 152             case 0:// SUCCESS
 153             case ERROR_NONE:
 154                 if (retVal != testExitCode) {
 155                     throw new AssertionFailedError(testAppName
 156                             + ": Unexpected 'success' exit; expected:"
 157                             + testExitCode + " was:" + retVal);
 158                 }
 159                 return;
 160             case 1:
 161                 throw new AssertionFailedError(testAppName
 162                         + ": unable to launch java application");
 163             case ERROR_TOOLKIT_NOT_RUNNING:
 164                 throw new AssertionFailedError(testAppName
 165                         + ": Toolkit not running prior to loading application class");
 166             case ERROR_TOOLKIT_IS_RUNNING:
 167                 throw new AssertionFailedError(testAppName
 168                         + ": Toolkit is running but should not be");
 169 
 170             case ERROR_INIT_BEFORE_MAIN:
 171                 throw new AssertionFailedError(testAppName
 172                         + ": main method not called before init");
 173             case ERROR_START_BEFORE_MAIN:
 174                 throw new AssertionFailedError(testAppName
 175                         + ": main method not called before start");
 176             case ERROR_STOP_BEFORE_MAIN:
 177                 throw new AssertionFailedError(testAppName
 178                         + ": main method not called before stop");
 179 
 180             case ERROR_START_BEFORE_INIT:
 181                 throw new AssertionFailedError(testAppName
 182                         + ": init method not called before start");
 183             case ERROR_STOP_BEFORE_INIT:
 184                 throw new AssertionFailedError(testAppName
 185                         + ": init method not called before stop");
 186 
 187             case ERROR_STOP_BEFORE_START:
 188                 throw new AssertionFailedError(testAppName
 189                         + ": start method not called before stop");
 190 
 191             case ERROR_CLASS_INIT_WRONG_THREAD:
 192                 throw new AssertionFailedError(testAppName
 193                         + ": class initialization called on wrong thread");
 194             case ERROR_MAIN_WRONG_THREAD:
 195                 throw new AssertionFailedError(testAppName
 196                         + ": main called on wrong thread");
 197             case ERROR_CONSTRUCTOR_WRONG_THREAD:
 198                 throw new AssertionFailedError(testAppName
 199                         + ": constructor called on wrong thread");
 200             case ERROR_INIT_WRONG_THREAD:
 201                 throw new AssertionFailedError(testAppName
 202                         + ": init called on wrong thread");
 203             case ERROR_START_WRONG_THREAD:
 204                 throw new AssertionFailedError(testAppName
 205                         + ": start called on wrong thread");
 206             case ERROR_STOP_WRONG_THREAD:
 207                 throw new AssertionFailedError(testAppName
 208                         + ": stop called on wrong thread");
 209 
 210             case ERROR_PRELOADER_CLASS_INIT_WRONG_THREAD:
 211                 throw new AssertionFailedError(testAppName
 212                         + ": preloader class initialization called on wrong thread");
 213             case ERROR_PRELOADER_CONSTRUCTOR_WRONG_THREAD:
 214                 throw new AssertionFailedError(testAppName
 215                         + ": preloader constructor called on wrong thread");
 216             case ERROR_PRELOADER_INIT_WRONG_THREAD:
 217                 throw new AssertionFailedError(testAppName
 218                         + ": preloader init called on wrong thread");
 219             case ERROR_PRELOADER_START_WRONG_THREAD:
 220                 throw new AssertionFailedError(testAppName
 221                         + ": preloader start called on wrong thread");
 222             case ERROR_PRELOADER_STOP_WRONG_THREAD:
 223                 throw new AssertionFailedError(testAppName
 224                         + ": preloader stop called on wrong thread");
 225 
 226             case ERROR_CONSTRUCTOR_WRONG_CCL:
 227                 throw new AssertionFailedError(testAppName
 228                         + ": constructor has wrong CCL");
 229             case ERROR_START_WRONG_CCL:
 230                 throw new AssertionFailedError(testAppName
 231                         + ": start has wrong CCL");
 232             case ERROR_LAUNCH_SUCCEEDED:
 233                 throw new AssertionFailedError(testAppName
 234                 + ": Application.launch unexpectedly succeeded");
 235             case ERROR_STARTUP_SUCCEEDED:
 236                 throw new AssertionFailedError(testAppName
 237                 + ": Plataform.startup unexpectedly succeeded");
 238             case ERROR_STARTUP_FAILED:
 239                 throw new AssertionFailedError(testAppName
 240                 + ": Plataform.startup failed");
 241 
 242             case ERROR_ASSERTION_FAILURE:
 243                 throw new AssertionFailedError(testAppName
 244                 + ": Assertion failure in test application");
 245 
 246             case ERROR_UNEXPECTED_EXCEPTION:
 247                 throw new AssertionFailedError(testAppName
 248                 + ": unexpected exception");
 249 
 250             default:
 251                 throw new AssertionFailedError(testAppName
 252                         + ": Unexpected error exit: " + retVal);
 253         }
 254     }
 255 
 256 }