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 myapp6;
  27 
  28 import java.io.IOException;
  29 import java.net.URL;
  30 import javafx.application.Application;
  31 import javafx.application.Platform;
  32 import javafx.collections.ObservableList;
  33 import javafx.fxml.FXMLLoader;
  34 import javafx.scene.Node;
  35 import javafx.scene.layout.StackPane;
  36 import javafx.scene.text.Text;
  37 import javafx.stage.Stage;
  38 import myapp6.pkg5.AnnotatedController;
  39 import myapp6.pkg5.CustomNode;
  40 import myapp6.pkg5.SimpleController;
  41 
  42 import static myapp6.Constants.*;
  43 
  44 /**
  45  * Modular test application for testing FXML.
  46  * This is launched by ModuleLauncherTest.
  47  */
  48 public class AppFXMLQualOpened extends Application {
  49 
  50     /**
  51      * @param args the command line arguments
  52      */
  53     public static void main(String[] args) {
  54         try {
  55             Application.launch(args);
  56         } catch (Throwable t) {
  57             System.err.println("ERROR: caught unexpected exception: " + t);
  58             t.printStackTrace(System.err);
  59             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  60         }
  61     }
  62 
  63     // Load test FXML file -- no controller
  64     private void doTestNone() throws IOException {
  65         final URL fxmlURL = Util.getURL(SimpleController.class, "TestNone");
  66 
  67         FXMLLoader loader = new FXMLLoader(fxmlURL);
  68         Node fxmlRoot = loader.load();
  69 
  70         // Verify that the root node is a StackPane with the expected ID
  71         Util.assertNotNull(fxmlRoot);
  72         Util.assertTrue("fxmlRoot is not instance of StackPane", fxmlRoot instanceof StackPane);
  73         Util.assertEquals("RootTestNone", fxmlRoot.getId());
  74     }
  75 
  76     // Load test FXML file with reference to CustomNode -- no controller
  77     private void doTestCustomNode() throws IOException {
  78         final URL fxmlURL = Util.getURL(SimpleController.class, "TestCustomNode");
  79 
  80         FXMLLoader loader = new FXMLLoader(fxmlURL);
  81         Node fxmlRoot = loader.load();
  82 
  83         // Verify that the root node is a StackPane with the expected ID
  84         Util.assertNotNull(fxmlRoot);
  85         Util.assertTrue("fxmlRoot is not instance of StackPane", fxmlRoot instanceof StackPane);
  86         Util.assertEquals("RootTestCustomNode", fxmlRoot.getId());
  87 
  88         // Verify that there is at least one child node
  89         ObservableList<Node> children = ((StackPane) fxmlRoot).getChildren();
  90         Util.assertNotNull(children);
  91         Util.assertFalse("fxmlRoot has no children", children.isEmpty());
  92 
  93         // Verify that the first child is our CustomNode
  94         Node child = children.get(0);
  95         Util.assertNotNull(child);
  96         Util.assertTrue("child is not instance of CustomNode", child instanceof CustomNode);
  97         String name = ((CustomNode) child).getName();
  98         Util.assertEquals("Duke", name);
  99     }
 100 
 101     // Load test FXML file -- SimpleController
 102     private void doTestSimple() throws IOException {
 103         final URL fxmlURL = Util.getURL(SimpleController.class, "TestSimple");
 104 
 105         FXMLLoader loader = new FXMLLoader(fxmlURL);
 106         Node fxmlRoot = loader.load();
 107 
 108         // Verify that the controller was constructed and its initialize method called
 109         SimpleController controller = loader.getController();
 110         Util.assertNotNull(controller);
 111         Util.assertTrue("Controller not initialized", controller.isInitialized());
 112 
 113         // Verify that the root node is a StackPane with the expected ID
 114         Util.assertNotNull(fxmlRoot);
 115         Util.assertTrue("fxmlRoot is not instance of StackPane", fxmlRoot instanceof StackPane);
 116         Util.assertEquals("RootTestSimple", fxmlRoot.getId());
 117     }
 118 
 119     // Load test FXML file -- AnnotatedController
 120     private void doTestAnnotated() throws IOException {
 121         final URL fxmlURL = Util.getURL(SimpleController.class, "TestAnnotated");
 122 
 123         FXMLLoader loader = new FXMLLoader(fxmlURL);
 124         Node fxmlRoot = loader.load();
 125 
 126         // Verify that the controller was constructed and its initialize method called
 127         AnnotatedController controller = loader.getController();
 128         Util.assertNotNull(controller);
 129         Util.assertTrue("Controller not initialized", controller.isInitialized());
 130 
 131         // Verify that the root node is a StackPane with the expected ID
 132         Util.assertNotNull(fxmlRoot);
 133         Util.assertTrue("fxmlRoot is not instance of StackPane", fxmlRoot instanceof StackPane);
 134         Util.assertEquals("RootTestAnnotated", fxmlRoot.getId());
 135 
 136         // Verify that the root node and text node can be retrieved from the controller
 137         StackPane cRoot = controller.getRoot();
 138         Util.assertNotNull(cRoot);
 139         Util.assertSame(fxmlRoot, cRoot);
 140         Text cText = controller.getTheText();
 141         Util.assertNotNull(cText);
 142         Util.assertEquals("theText", cText.getId());
 143     }
 144 
 145     @Override
 146     public void start(Stage stage) {
 147         try {
 148             doTestNone();
 149             doTestCustomNode();
 150             doTestSimple();
 151             doTestAnnotated();
 152         } catch (AssertionError ex) {
 153             ex.printStackTrace(System.err);
 154             System.exit(ERROR_ASSERTION_FAILURE);
 155         } catch (Error | Exception ex) {
 156             System.err.println("ERROR: caught unexpected exception: " + ex);
 157             ex.printStackTrace(System.err);
 158             System.exit(ERROR_UNEXPECTED_EXCEPTION);
 159         }
 160         Platform.exit();
 161     }
 162 
 163     @Override public void stop() {
 164         System.exit(ERROR_NONE);
 165     }
 166 
 167 }