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 myapp4; 27 28 import java.lang.reflect.UndeclaredThrowableException; 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.Locale; 32 import java.util.logging.Handler; 33 import java.util.logging.LogRecord; 34 import java.util.logging.Logger; 35 import javafx.beans.binding.Bindings; 36 import javafx.beans.binding.DoubleBinding; 37 import javafx.beans.binding.ObjectBinding; 38 import myapp4.pkg3.MyProps; 39 40 import static myapp4.Constants.*; 41 42 // This logic is copied from AppBindingsUnexported. 43 44 /** 45 * Modular test application for testing JavaFX beans. 46 * This is launched by ModuleLauncherTest. 47 */ 48 public class AppBindingsQualExported { 49 50 /** 51 * @param args the command line arguments 52 */ 53 public static void main(String[] args) { 54 try { 55 new AppBindingsQualExported().doTest(); 56 System.exit(ERROR_NONE); 57 } catch (Throwable t) { 58 t.printStackTrace(System.err); 59 System.exit(ERROR_ASSERTION_FAILURE); 60 } 61 } 62 63 private void checkException(RuntimeException ex) { 64 Throwable cause = ex.getCause(); 65 if (! (cause instanceof IllegalAccessException)) { 66 System.err.println("ERROR: unexpected cause: " + cause); 67 throw ex; 68 } 69 70 String message = cause.getMessage(); 71 if (message == null) { 72 System.err.println("ERROR: detail message of cause is null"); 73 throw ex; 74 } 75 76 boolean badMessage = false; 77 if (!message.contains(" cannot access class ")) badMessage = true; 78 if (!message.contains(" does not open ")) badMessage = true; 79 if (!message.endsWith(" to javafx.base")) badMessage = true; 80 if (badMessage) { 81 System.err.println("ERROR: detail message not formatted correctly: " + message); 82 throw ex; 83 } 84 } 85 86 private final double EPSILON = 1.0e-4; 87 88 private void assertEquals(double expected, double observed) { 89 if (Math.abs(expected - observed) > EPSILON) { 90 throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">"); 91 } 92 } 93 94 private void assertEquals(String expected, String observed) { 95 if (!expected.equals(observed)) { 96 throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">"); 97 } 98 } 99 100 private void assertSame(Object expected, Object observed) { 101 if (expected != observed) { 102 throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">"); 103 } 104 } 105 106 private Logger logger; 107 private Handler logHandler; 108 private final List<Throwable> errs = new ArrayList<>(); 109 110 private void initLogger() { 111 Locale.setDefault(Locale.US); 112 113 // Initialize Logger 114 logHandler = new Handler() { 115 @Override 116 public void publish(LogRecord record) { 117 final Throwable t = record.getThrown(); 118 // detect any Throwable: 119 if (t != null) { 120 errs.add(t); 121 } 122 } 123 124 @Override 125 public void flush() { 126 } 127 128 @Override 129 public void close() { 130 } 131 }; 132 133 logger = Logger.getLogger("javafx.beans"); 134 logger.addHandler(logHandler); 135 } 136 137 public void doTest() throws Exception { 138 initLogger(); 139 140 MyProps root = new MyProps(); 141 MyProps a = new MyProps(); 142 MyProps b = new MyProps(); 143 144 root.setNext(a); 145 a.setNext(b); 146 a.setFoo(1.2); 147 b.setFoo(2.3); 148 149 try { 150 Bindings.selectDouble(root, "next", "foo"); 151 throw new AssertionError("ERROR: did not get the expected exception"); 152 } catch (UndeclaredThrowableException ex) { 153 checkException(ex); 154 } 155 156 try { 157 Bindings.select(root, "next", "next"); 158 throw new AssertionError("ERROR: did not get the expected exception"); 159 } catch (UndeclaredThrowableException ex) { 160 checkException(ex); 161 } 162 163 RootProps root2 = new RootProps(); 164 MyProps c = new MyProps(); 165 MyProps d = new MyProps(); 166 167 root2.setNext(c); 168 c.setNext(d); 169 c.setFoo(1.2); 170 d.setFoo(2.3); 171 172 // In this case, the binding will succeed; calling get() will return 0; 173 // the first time it is called it will log a warning. 174 DoubleBinding binding3 = Bindings.selectDouble(root2, "next", "foo"); 175 assertEquals(0, binding3.get()); // This will log a warning 176 c.setFoo(3.4); 177 assertEquals(0, binding3.get()); // No warning here 178 179 // In this case, the binding will succeed; calling get() will return null; 180 // the first time it is called it will log a warning. 181 ObjectBinding<MyProps> binding4 = Bindings.select(root2, "next", "next"); 182 assertSame(null, binding4.get()); // This will log a warning 183 assertSame(null, binding4.get()); // No warning here 184 185 // Assert that we got 2 warnings 186 final int expectedExceptions = 2; // First call to get for each binding 187 188 if (errs.isEmpty()) { 189 throw new AssertionError("ERROR: did not get the expected exception"); 190 } 191 192 assertEquals(expectedExceptions, errs.size()); 193 194 for (Throwable t : errs) { 195 if (!(t instanceof RuntimeException)) { 196 throw new AssertionError("ERROR: unexpected exception: ", t); 197 } 198 checkException((RuntimeException) t); 199 } 200 } 201 202 }