1 /*
   2  * Copyright (c) 2013, 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 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 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("TestAppThreadCheck", ERROR_NONE),
  86         new TestData("TestAppNoMainThreadCheck", ERROR_NONE),
  87         new TestData("TestNotApplicationThreadCheck", ERROR_NONE),
  88         new TestData("TestAppThreadCheck", "TestPreloader", ERROR_NONE),
  89         new TestData("TestAppNoMainThreadCheck", "TestPreloader", ERROR_NONE),
  90         new TestData("TestAppCCL", ERROR_NONE),
  91         new TestData("TestAppCCL1", ERROR_NONE),
  92         new TestData("TestAppCCL2", ERROR_NONE),
  93         new TestData("TestAppNoMainCCL", ERROR_NONE),
  94         new TestData("TestAppNoMainCCL2", ERROR_NONE),
  95         new TestData("TestAppNoMainCCL3", ERROR_NONE),
  96         new TestData("TestNotApplicationCCL", ERROR_NONE),
  97         new TestData("TestHeadlessApp", true, ERROR_NONE),
  98     };
  99 
 100     @Parameters
 101     public static Collection getParams() {
 102         if (params == null) {
 103             params = new ArrayList();
 104             for (TestData data : testData) {
 105                 params.add(new TestData[] { data });
 106             }
 107         }
 108         return params;
 109     }
 110 
 111     private final String testAppName;
 112     private final String testPldrName;
 113     private final boolean headless;
 114     private final int testExitCode;
 115 
 116     public MainLauncherTest(TestData testData) {
 117         this.testAppName = testData.appName;
 118         this.testPldrName = testData.pldrName;
 119         this.headless = testData.headless;
 120         this.testExitCode = testData.exitCode;
 121     }
 122 
 123     @Test (timeout=5000)
 124     public void testMainLauncher() throws Exception {
 125         if (headless) {
 126             // Headless tests currently only run on Linux
 127             assumeTrue(PlatformUtil.isLinux());
 128         }
 129 
 130         final String classpath = System.getProperty("java.class.path");
 131         ProcessBuilder builder;
 132         if (testPldrName != null) {
 133             builder = new ProcessBuilder("java", "-cp", classpath,
 134                     "-Djavafx.preloader=" + testPldrName, testAppName);
 135         } else {
 136             builder = new ProcessBuilder("java", "-cp", classpath, testAppName);
 137         }
 138         if (headless) {
 139             // Set DISPLAY variable to empty to run in headless mode on Linux
 140             builder.environment().put("DISPLAY", "");
 141         }
 142         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
 143         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
 144         Process process = builder.start();
 145         int retVal = process.waitFor();
 146         switch (retVal) {
 147             case 0:// SUCCESS
 148             case ERROR_NONE:
 149                 if (retVal != testExitCode) {
 150                     throw new AssertionFailedError(testAppName
 151                             + ": Unexpected 'success' exit; expected:"
 152                             + testExitCode + " was:" + retVal);
 153                 }
 154                 return;
 155             case 1:
 156                 throw new AssertionFailedError(testAppName
 157                         + ": unable to launch java application");
 158             case ERROR_TOOLKIT_NOT_RUNNING:
 159                 throw new AssertionFailedError(testAppName
 160                         + ": Toolkit not running prior to loading application class");
 161             case ERROR_TOOLKIT_IS_RUNNING:
 162                 throw new AssertionFailedError(testAppName
 163                         + ": Toolkit is running but should not be");
 164 
 165             case ERROR_INIT_BEFORE_MAIN:
 166                 throw new AssertionFailedError(testAppName
 167                         + ": main method not called before init");
 168             case ERROR_START_BEFORE_MAIN:
 169                 throw new AssertionFailedError(testAppName
 170                         + ": main method not called before start");
 171             case ERROR_STOP_BEFORE_MAIN:
 172                 throw new AssertionFailedError(testAppName
 173                         + ": main method not called before stop");
 174 
 175             case ERROR_START_BEFORE_INIT:
 176                 throw new AssertionFailedError(testAppName
 177                         + ": init method not called before start");
 178             case ERROR_STOP_BEFORE_INIT:
 179                 throw new AssertionFailedError(testAppName
 180                         + ": init method not called before stop");
 181 
 182             case ERROR_STOP_BEFORE_START:
 183                 throw new AssertionFailedError(testAppName
 184                         + ": start method not called before stop");
 185 
 186             case ERROR_CLASS_INIT_WRONG_THREAD:
 187                 throw new AssertionFailedError(testAppName
 188                         + ": class initialization called on wrong thread");
 189             case ERROR_MAIN_WRONG_THREAD:
 190                 throw new AssertionFailedError(testAppName
 191                         + ": main called on wrong thread");
 192             case ERROR_CONSTRUCTOR_WRONG_THREAD:
 193                 throw new AssertionFailedError(testAppName
 194                         + ": constructor called on wrong thread");
 195             case ERROR_INIT_WRONG_THREAD:
 196                 throw new AssertionFailedError(testAppName
 197                         + ": init called on wrong thread");
 198             case ERROR_START_WRONG_THREAD:
 199                 throw new AssertionFailedError(testAppName
 200                         + ": start called on wrong thread");
 201             case ERROR_STOP_WRONG_THREAD:
 202                 throw new AssertionFailedError(testAppName
 203                         + ": stop called on wrong thread");
 204 
 205             case ERROR_PRELOADER_CLASS_INIT_WRONG_THREAD:
 206                 throw new AssertionFailedError(testAppName
 207                         + ": preloader class initialization called on wrong thread");
 208             case ERROR_PRELOADER_CONSTRUCTOR_WRONG_THREAD:
 209                 throw new AssertionFailedError(testAppName
 210                         + ": preloader constructor called on wrong thread");
 211             case ERROR_PRELOADER_INIT_WRONG_THREAD:
 212                 throw new AssertionFailedError(testAppName
 213                         + ": preloader init called on wrong thread");
 214             case ERROR_PRELOADER_START_WRONG_THREAD:
 215                 throw new AssertionFailedError(testAppName
 216                         + ": preloader start called on wrong thread");
 217             case ERROR_PRELOADER_STOP_WRONG_THREAD:
 218                 throw new AssertionFailedError(testAppName
 219                         + ": preloader stop called on wrong thread");
 220 
 221             case ERROR_CONSTRUCTOR_WRONG_CCL:
 222                 throw new AssertionFailedError(testAppName
 223                         + ": constructor has wrong CCL");
 224             case ERROR_START_WRONG_CCL:
 225                 throw new AssertionFailedError(testAppName
 226                         + ": start has wrong CCL");
 227 
 228             case ERROR_UNEXPECTED_EXCEPTION:
 229                 throw new AssertionFailedError(testAppName
 230                 + ": unexpected exception");
 231             case ERROR_LAUNCH_SUCCEEDED:
 232                 throw new AssertionFailedError(testAppName
 233                 + ": Application.launch unexpectedly succeeded");
 234 
 235             default:
 236                 throw new AssertionFailedError(testAppName
 237                         + ": Unexpected error exit: " + retVal);
 238         }
 239     }
 240 
 241 }