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 javafx.beans.property.adapter.JavaBeanDoubleProperty; 29 import javafx.beans.property.adapter.JavaBeanDoublePropertyBuilder; 30 import javafx.beans.property.adapter.JavaBeanObjectProperty; 31 import javafx.beans.property.adapter.JavaBeanObjectPropertyBuilder; 32 import javafx.beans.property.adapter.ReadOnlyJavaBeanStringProperty; 33 import javafx.beans.property.adapter.ReadOnlyJavaBeanStringPropertyBuilder; 34 import myapp4.pkg2.POJO; 35 import myapp4.pkg2.RefClass; 36 37 import static myapp4.Constants.*; 38 39 /** 40 * Modular test application for testing JavaFX beans. 41 * This is launched by ModuleLauncherTest. 42 */ 43 public class AppBeansExported { 44 45 /** 46 * @param args the command line arguments 47 */ 48 public static void main(String[] args) { 49 try { 50 new AppBeansExported().doTest(); 51 System.exit(ERROR_NONE); 52 } catch (Throwable t) { 53 t.printStackTrace(System.err); 54 System.exit(ERROR_ASSERTION_FAILURE); 55 } 56 } 57 58 private final double EPSILON = 1.0e-4; 59 60 private void assertEquals(double expected, double observed) { 61 if (Math.abs(expected - observed) > EPSILON) { 62 throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">"); 63 } 64 } 65 66 private void assertEquals(String expected, String observed) { 67 if (!expected.equals(observed)) { 68 throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">"); 69 } 70 } 71 72 private void assertSame(Object expected, Object observed) { 73 if (expected != observed) { 74 throw new AssertionError("expected:<" + expected + "> but was:<" + observed + ">"); 75 } 76 } 77 78 public void doTest() throws Exception { 79 String name = "test object"; 80 double val = 1.2; 81 RefClass obj = new RefClass(); 82 83 POJO bean = new POJO(name, val, obj); 84 85 JavaBeanDoubleProperty valProp = JavaBeanDoublePropertyBuilder.create() 86 .bean(bean) 87 .name("val") 88 .build(); 89 90 double retVal = valProp.get(); 91 assertEquals(val, retVal); 92 93 val = 2.5; 94 valProp.set(val); 95 retVal = valProp.get(); 96 assertEquals(val, retVal); 97 98 JavaBeanObjectProperty<RefClass> objProp = JavaBeanObjectPropertyBuilder.create() 99 .bean(bean) 100 .name("obj") 101 .build(); 102 103 RefClass retObj = objProp.get(); 104 assertSame(obj, retObj); 105 106 obj = new RefClass(); 107 objProp.set(obj); 108 retObj = objProp.get(); 109 assertSame(obj, retObj); 110 111 ReadOnlyJavaBeanStringProperty namePropRO = ReadOnlyJavaBeanStringPropertyBuilder.create() 112 .bean(bean) 113 .name("name") 114 .build(); 115 116 String retName = namePropRO.get(); 117 assertEquals(name, retName); 118 } 119 120 }