1 /*
   2  * Copyright (c) 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 launchertest;
  27 
  28 import java.util.ArrayList;
  29 import java.util.concurrent.CountDownLatch;
  30 import java.util.concurrent.Semaphore;
  31 import javafx.application.Platform;
  32 
  33 import static launchertest.Constants.*;
  34 
  35 /**
  36  * Test Platform.startup from class that is not an Application.
  37  * This is launched by MainLauncherTest.
  38  */
  39 public class TestStartupNotApplication {
  40 
  41     private static void assertEquals(String expected, String actual) {
  42         if (expected == null && actual == null) return;
  43         if (expected != null && expected.equals(actual)) return;
  44         System.err.println("Assertion failed: expected (" + expected + ") != actual (" + actual + ")");
  45         System.exit(ERROR_ASSERTION_FAILURE);
  46     }
  47 
  48     public static void main(String[] args) {
  49         try {
  50             Platform.runLater(() -> {
  51                 // do nothing
  52             });
  53             System.exit(ERROR_TOOLKIT_IS_RUNNING);
  54         } catch (IllegalStateException ex) {
  55             // OK
  56         } catch (RuntimeException ex) {
  57             ex.printStackTrace();
  58             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  59         }
  60 
  61         final Semaphore sem = new Semaphore(0);
  62         final ArrayList<String> list = new ArrayList<>();
  63         final String keyStartup = "Startup runnable";
  64         final String keyRunLater0 = "runLater #0";
  65         final String keyRunLater1 = "runLater #1";
  66         try {
  67             Platform.startup(() -> {
  68                 list.add(keyStartup);
  69                 sem.release();
  70             });
  71             Platform.runLater(() -> {
  72                 list.add(keyRunLater0);
  73                 sem.release();
  74             });
  75             sem.acquire(2);
  76         } catch (IllegalStateException ex) {
  77             ex.printStackTrace();
  78             System.exit(ERROR_STARTUP_FAILED);
  79         } catch (InterruptedException ex) {
  80             ex.printStackTrace();
  81             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  82         }
  83 
  84         Platform.runLater(() -> {
  85             list.add(keyRunLater1);
  86             sem.release();
  87         });
  88         try {
  89             sem.acquire();
  90         } catch (InterruptedException ex) {
  91             ex.printStackTrace();
  92             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  93         }
  94         assertEquals(keyStartup, list.get(0));
  95         assertEquals(keyRunLater0, list.get(1));
  96         assertEquals(keyRunLater1, list.get(2));
  97 
  98         System.exit(ERROR_NONE);
  99     }
 100 
 101 }