1 /*
   2  * Copyright (c) 2013, 2016, 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 ArrayList<String> cmd = 
 136                 test.util.Util.createApplicationLaunchCommand(
 137                         testAppName,
 138                         testPldrName,
 139                         null
 140                         );
 141 
 142         final ProcessBuilder builder = new ProcessBuilder(cmd);
 143         
 144         if (headless) {
 145             // Set DISPLAY variable to empty to run in headless mode on Linux
 146             builder.environment().put("DISPLAY", "");
 147         }
 148         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
 149         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
 150         Process process = builder.start();
 151         int retVal = process.waitFor();
 152         switch (retVal) {
 153             case 0:// SUCCESS
 154             case ERROR_NONE:
 155                 if (retVal != testExitCode) {
 156                     throw new AssertionFailedError(testAppName
 157                             + ": Unexpected 'success' exit; expected:"
 158                             + testExitCode + " was:" + retVal);
 159                 }
 160                 return;
 161             case 1:
 162                 throw new AssertionFailedError(testAppName
 163                         + ": unable to launch java application");
 164             case ERROR_TOOLKIT_NOT_RUNNING:
 165                 throw new AssertionFailedError(testAppName
 166                         + ": Toolkit not running prior to loading application class");
 167             case ERROR_TOOLKIT_IS_RUNNING:
 168                 throw new AssertionFailedError(testAppName
 169                         + ": Toolkit is running but should not be");
 170 
 171             case ERROR_INIT_BEFORE_MAIN:
 172                 throw new AssertionFailedError(testAppName
 173                         + ": main method not called before init");
 174             case ERROR_START_BEFORE_MAIN:
 175                 throw new AssertionFailedError(testAppName
 176                         + ": main method not called before start");
 177             case ERROR_STOP_BEFORE_MAIN:
 178                 throw new AssertionFailedError(testAppName
 179                         + ": main method not called before stop");
 180 
 181             case ERROR_START_BEFORE_INIT:
 182                 throw new AssertionFailedError(testAppName
 183                         + ": init method not called before start");
 184             case ERROR_STOP_BEFORE_INIT:
 185                 throw new AssertionFailedError(testAppName
 186                         + ": init method not called before stop");
 187 
 188             case ERROR_STOP_BEFORE_START:
 189                 throw new AssertionFailedError(testAppName
 190                         + ": start method not called before stop");
 191 
 192             case ERROR_CLASS_INIT_WRONG_THREAD:
 193                 throw new AssertionFailedError(testAppName
 194                         + ": class initialization called on wrong thread");
 195             case ERROR_MAIN_WRONG_THREAD:
 196                 throw new AssertionFailedError(testAppName
 197                         + ": main called on wrong thread");
 198             case ERROR_CONSTRUCTOR_WRONG_THREAD:
 199                 throw new AssertionFailedError(testAppName
 200                         + ": constructor called on wrong thread");
 201             case ERROR_INIT_WRONG_THREAD:
 202                 throw new AssertionFailedError(testAppName
 203                         + ": init called on wrong thread");
 204             case ERROR_START_WRONG_THREAD:
 205                 throw new AssertionFailedError(testAppName
 206                         + ": start called on wrong thread");
 207             case ERROR_STOP_WRONG_THREAD:
 208                 throw new AssertionFailedError(testAppName
 209                         + ": stop called on wrong thread");
 210 
 211             case ERROR_PRELOADER_CLASS_INIT_WRONG_THREAD:
 212                 throw new AssertionFailedError(testAppName
 213                         + ": preloader class initialization called on wrong thread");
 214             case ERROR_PRELOADER_CONSTRUCTOR_WRONG_THREAD:
 215                 throw new AssertionFailedError(testAppName
 216                         + ": preloader constructor called on wrong thread");
 217             case ERROR_PRELOADER_INIT_WRONG_THREAD:
 218                 throw new AssertionFailedError(testAppName
 219                         + ": preloader init called on wrong thread");
 220             case ERROR_PRELOADER_START_WRONG_THREAD:
 221                 throw new AssertionFailedError(testAppName
 222                         + ": preloader start called on wrong thread");
 223             case ERROR_PRELOADER_STOP_WRONG_THREAD:
 224                 throw new AssertionFailedError(testAppName
 225                         + ": preloader stop called on wrong thread");
 226 
 227             case ERROR_CONSTRUCTOR_WRONG_CCL:
 228                 throw new AssertionFailedError(testAppName
 229                         + ": constructor has wrong CCL");
 230             case ERROR_START_WRONG_CCL:
 231                 throw new AssertionFailedError(testAppName
 232                         + ": start has wrong CCL");
 233             case ERROR_LAUNCH_SUCCEEDED:
 234                 throw new AssertionFailedError(testAppName
 235                 + ": Application.launch unexpectedly succeeded");
 236             case ERROR_STARTUP_SUCCEEDED:
 237                 throw new AssertionFailedError(testAppName
 238                 + ": Plataform.startup unexpectedly succeeded");
 239             case ERROR_STARTUP_FAILED:
 240                 throw new AssertionFailedError(testAppName
 241                 + ": Plataform.startup failed");
 242 
 243             case ERROR_ASSERTION_FAILURE:
 244                 throw new AssertionFailedError(testAppName
 245                 + ": Assertion failure in test application");
 246 
 247             case ERROR_UNEXPECTED_EXCEPTION:
 248                 throw new AssertionFailedError(testAppName
 249                 + ": unexpected exception");
 250 
 251             default:
 252                 throw new AssertionFailedError(testAppName
 253                         + ": Unexpected error exit: " + retVal);
 254         }
 255     }
 256 
 257 }