1 /*
   2  * Copyright (c) 2017, 2018, 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 java.io.File;
  29 import java.util.ArrayList;
  30 import junit.framework.AssertionFailedError;
  31 import org.junit.Test;
  32 
  33 import static org.junit.Assert.*;
  34 import static test.launchertest.Constants.*;
  35 
  36 /**
  37  * Unit test for launching modular FX applications
  38  */
  39 public class ModuleLauncherTest {
  40 
  41     private static final String modulePath2 = System.getProperty("launchertest.testapp2.module.path");
  42     private static final String modulePath3 = System.getProperty("launchertest.testapp3.module.path");
  43     private static final String modulePath4 = System.getProperty("launchertest.testapp4.module.path");
  44     private static final String modulePath5 = System.getProperty("launchertest.testapp5.module.path");
  45     private static final String modulePath6 = System.getProperty("launchertest.testapp6.module.path");
  46     private static final String moduleName = "mymod";
  47 
  48     private final int testExitCode = ERROR_NONE;
  49 
  50     private void doTestLaunchModule(String appModulePath, String testAppName) throws Exception {
  51         final String javafxModulePath = System.getProperty("worker.module.path");
  52         String modulePath;
  53         if (javafxModulePath != null) {
  54             modulePath = javafxModulePath + File.pathSeparator + appModulePath;
  55         } else {
  56             modulePath = appModulePath;
  57         }
  58         assertNotNull(testAppName);
  59         System.err.println("The following Unknown module WARNING messages are expected:");
  60         String mpArg = "--module-path=" + modulePath;
  61         String moduleAppName = "--module=" + moduleName + "/" + testAppName;
  62         final ArrayList<String> cmd =
  63                 test.util.Util.createApplicationLaunchCommand(
  64                         moduleAppName,
  65                         null,
  66                         null,
  67                         new String[] { mpArg }
  68                         );
  69 
  70         final ProcessBuilder builder = new ProcessBuilder(cmd);
  71 
  72         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
  73         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  74         Process process = builder.start();
  75         int retVal = process.waitFor();
  76         switch (retVal) {
  77             case 0:// SUCCESS
  78             case ERROR_NONE:
  79                 if (retVal != testExitCode) {
  80                     throw new AssertionFailedError(testAppName
  81                             + ": Unexpected 'success' exit; expected:"
  82                             + testExitCode + " was:" + retVal);
  83                 }
  84                 return;
  85 
  86             case 1:
  87                 throw new AssertionFailedError(testAppName
  88                         + ": unable to launch java application");
  89 
  90             case ERROR_TOOLKIT_NOT_RUNNING:
  91                 throw new AssertionFailedError(testAppName
  92                         + ": Toolkit not running prior to loading application class");
  93             case ERROR_TOOLKIT_IS_RUNNING:
  94                 throw new AssertionFailedError(testAppName
  95                         + ": Toolkit is running but should not be");
  96 
  97             case ERROR_ASSERTION_FAILURE:
  98                 throw new AssertionFailedError(testAppName
  99                 + ": Assertion failure in test application");
 100 
 101             case ERROR_UNEXPECTED_EXCEPTION:
 102                 throw new AssertionFailedError(testAppName
 103                 + ": unexpected exception");
 104 
 105             default:
 106                 throw new AssertionFailedError(testAppName
 107                         + ": Unexpected error exit: " + retVal);
 108         }
 109     }
 110 
 111 
 112     @Test (timeout = 15000)
 113     public void testLaunchModule() throws Exception {
 114         doTestLaunchModule(modulePath2, "testapp.TestApp");
 115     }
 116 
 117     @Test (timeout = 15000)
 118     public void testLaunchModuleNoMain() throws Exception {
 119         doTestLaunchModule(modulePath2, "testapp.TestAppNoMain");
 120     }
 121 
 122     @Test (timeout = 15000)
 123     public void testLaunchModuleNotApplication() throws Exception {
 124         doTestLaunchModule(modulePath2, "testapp.TestNotApplication");
 125     }
 126 
 127     @Test (timeout = 15000)
 128     public void testModuleTableViewUnexported() throws Exception {
 129         doTestLaunchModule(modulePath3, "myapp3.AppTableViewUnexported");
 130     }
 131 
 132     @Test (timeout = 15000)
 133     public void testModuleTableViewExported() throws Exception {
 134         doTestLaunchModule(modulePath3, "myapp3.AppTableViewExported");
 135     }
 136 
 137     @Test (timeout = 15000)
 138     public void testModuleTableViewQualExported() throws Exception {
 139         doTestLaunchModule(modulePath3, "myapp3.AppTableViewQualExported");
 140     }
 141 
 142     @Test (timeout = 15000)
 143     public void testModuleTableViewOpened() throws Exception {
 144         doTestLaunchModule(modulePath3, "myapp3.AppTableViewOpened");
 145     }
 146 
 147     @Test (timeout = 15000)
 148     public void testModuleTableViewQualOpened() throws Exception {
 149         doTestLaunchModule(modulePath3, "myapp3.AppTableViewQualOpened");
 150     }
 151 
 152     @Test (timeout = 15000)
 153     public void testModuleTreeTableViewUnexported() throws Exception {
 154         doTestLaunchModule(modulePath3, "myapp3.AppTreeTableViewUnexported");
 155     }
 156 
 157     @Test (timeout = 15000)
 158     public void testModuleTreeTableViewExported() throws Exception {
 159         doTestLaunchModule(modulePath3, "myapp3.AppTreeTableViewExported");
 160     }
 161 
 162     @Test (timeout = 15000)
 163     public void testModuleTreeTableViewQualExported() throws Exception {
 164         doTestLaunchModule(modulePath3, "myapp3.AppTreeTableViewQualExported");
 165     }
 166 
 167     @Test (timeout = 15000)
 168     public void testModuleTreeTableViewOpened() throws Exception {
 169         doTestLaunchModule(modulePath3, "myapp3.AppTreeTableViewOpened");
 170     }
 171 
 172     @Test (timeout = 15000)
 173     public void testModuleTreeTableViewQualOpened() throws Exception {
 174         doTestLaunchModule(modulePath3, "myapp3.AppTreeTableViewQualOpened");
 175     }
 176 
 177     @Test (timeout = 15000)
 178     public void testModuleBeansUnexported() throws Exception {
 179         doTestLaunchModule(modulePath4, "myapp4.AppBeansUnexported");
 180     }
 181 
 182     @Test (timeout = 15000)
 183     public void testModuleBeansExported() throws Exception {
 184         doTestLaunchModule(modulePath4, "myapp4.AppBeansExported");
 185     }
 186 
 187     @Test (timeout = 15000)
 188     public void testModuleBeansQualExported() throws Exception {
 189         doTestLaunchModule(modulePath4, "myapp4.AppBeansQualExported");
 190     }
 191 
 192     @Test (timeout = 15000)
 193     public void testModuleBeansOpened() throws Exception {
 194         doTestLaunchModule(modulePath4, "myapp4.AppBeansOpened");
 195     }
 196 
 197     @Test (timeout = 15000)
 198     public void testModuleBeansQualOpened() throws Exception {
 199         doTestLaunchModule(modulePath4, "myapp4.AppBeansQualOpened");
 200     }
 201 
 202     @Test (timeout = 15000)
 203     public void testModuleBindingsUnexported() throws Exception {
 204         doTestLaunchModule(modulePath4, "myapp4.AppBindingsUnexported");
 205     }
 206 
 207     @Test (timeout = 15000)
 208     public void testModuleBindingsExported() throws Exception {
 209         doTestLaunchModule(modulePath4, "myapp4.AppBindingsExported");
 210     }
 211 
 212     @Test (timeout = 15000)
 213     public void testModuleBindingsQualExported() throws Exception {
 214         doTestLaunchModule(modulePath4, "myapp4.AppBindingsQualExported");
 215     }
 216 
 217     @Test (timeout = 15000)
 218     public void testModuleBindingsOpened() throws Exception {
 219         doTestLaunchModule(modulePath4, "myapp4.AppBindingsOpened");
 220     }
 221 
 222     @Test (timeout = 15000)
 223     public void testModuleBindingsQualOpened() throws Exception {
 224         doTestLaunchModule(modulePath4, "myapp4.AppBindingsQualOpened");
 225     }
 226 
 227     @Test (timeout = 15000)
 228     public void testModuleJSCallbackUnexported() throws Exception {
 229         doTestLaunchModule(modulePath5, "myapp5.AppJSCallbackUnexported");
 230     }
 231 
 232     @Test (timeout = 15000)
 233     public void testModuleJSCallbackExported() throws Exception {
 234         doTestLaunchModule(modulePath5, "myapp5.AppJSCallbackExported");
 235     }
 236 
 237     @Test (timeout = 15000)
 238     public void testModuleJSCallbackQualExported() throws Exception {
 239         doTestLaunchModule(modulePath5, "myapp5.AppJSCallbackQualExported");
 240     }
 241 
 242     @Test (timeout = 15000)
 243     public void testModuleJSCallbackOpened() throws Exception {
 244         doTestLaunchModule(modulePath5, "myapp5.AppJSCallbackOpened");
 245     }
 246 
 247     @Test (timeout = 15000)
 248     public void testModuleJSCallbackQualOpened() throws Exception {
 249         doTestLaunchModule(modulePath5, "myapp5.AppJSCallbackQualOpened");
 250     }
 251 
 252     @Test (timeout = 15000)
 253     public void testModuleFXMLUnexported() throws Exception {
 254         doTestLaunchModule(modulePath6, "myapp6.AppFXMLUnexported");
 255     }
 256 
 257     @Test (timeout = 15000)
 258     public void testModuleFXMLExported() throws Exception {
 259         doTestLaunchModule(modulePath6, "myapp6.AppFXMLExported");
 260     }
 261 
 262     @Test (timeout = 15000)
 263     public void testModuleFXMLQualExported() throws Exception {
 264         doTestLaunchModule(modulePath6, "myapp6.AppFXMLQualExported");
 265     }
 266 
 267     @Test (timeout = 15000)
 268     public void testModuleFXMLOpened() throws Exception {
 269         doTestLaunchModule(modulePath6, "myapp6.AppFXMLOpened");
 270     }
 271 
 272     @Test (timeout = 15000)
 273     public void testModuleFXMLQualOpened() throws Exception {
 274         doTestLaunchModule(modulePath6, "myapp6.AppFXMLQualOpened");
 275     }
 276 
 277 }