1 /*
   2  * Copyright (c) 2017, 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.util.ArrayList;
  29 import junit.framework.AssertionFailedError;
  30 import org.junit.Test;
  31 
  32 import static org.junit.Assert.*;
  33 import static test.launchertest.Constants.*;
  34 
  35 /**
  36  * Unit test for launching modular FX applications
  37  */
  38 public class ModuleLauncherTest {
  39 
  40     private final String modulePath = System.getProperty("launchertest.testapp2.module.path");
  41     private final String moduleName = "mymod";
  42     private final int testExitCode = ERROR_NONE;
  43 
  44     private void doTestLaunchModule(String testAppName) throws Exception {
  45         assertNotNull(testAppName);
  46         String mpArg = "--module-path=" + modulePath;
  47         String moduleAppName = "--module=" + moduleName + "/" + testAppName;
  48         final ArrayList<String> cmd =
  49                 test.util.Util.createApplicationLaunchCommand(
  50                         moduleAppName,
  51                         null,
  52                         null,
  53                         new String[] { mpArg }
  54                         );
  55 
  56         final ProcessBuilder builder = new ProcessBuilder(cmd);
  57 
  58         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
  59         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  60         Process process = builder.start();
  61         int retVal = process.waitFor();
  62         switch (retVal) {
  63             case 0:// SUCCESS
  64             case ERROR_NONE:
  65                 if (retVal != testExitCode) {
  66                     throw new AssertionFailedError(testAppName
  67                             + ": Unexpected 'success' exit; expected:"
  68                             + testExitCode + " was:" + retVal);
  69                 }
  70                 return;
  71 
  72             case 1:
  73                 throw new AssertionFailedError(testAppName
  74                         + ": unable to launch java application");
  75 
  76             case ERROR_TOOLKIT_NOT_RUNNING:
  77                 throw new AssertionFailedError(testAppName
  78                         + ": Toolkit not running prior to loading application class");
  79             case ERROR_TOOLKIT_IS_RUNNING:
  80                 throw new AssertionFailedError(testAppName
  81                         + ": Toolkit is running but should not be");
  82 
  83             case ERROR_UNEXPECTED_EXCEPTION:
  84                 throw new AssertionFailedError(testAppName
  85                 + ": unexpected exception");
  86 
  87             default:
  88                 throw new AssertionFailedError(testAppName
  89                         + ": Unexpected error exit: " + retVal);
  90         }
  91     }
  92 
  93 
  94     @Test (timeout=15000)
  95     public void testLaunchModule() throws Exception {
  96         doTestLaunchModule("testapp.TestApp");
  97     }
  98 
  99     @Test (timeout=15000)
 100     public void testLaunchModuleNoMain() throws Exception {
 101         doTestLaunchModule("testapp.TestAppNoMain");
 102     }
 103 
 104     @Test (timeout=15000)
 105     public void testLaunchModuleNotApplication() throws Exception {
 106         doTestLaunchModule("testapp.TestNotApplication");
 107     }
 108 
 109 }