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