1 /* 2 * Copyright (c) 2011, 2013, 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 test.javafx.beans.property; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 import javafx.beans.InvalidationListener; 32 import javafx.beans.binding.ObjectExpression; 33 import javafx.beans.property.ReadOnlyDoubleProperty; 34 import javafx.beans.property.ReadOnlyDoubleWrapper; 35 import javafx.beans.property.ReadOnlyObjectProperty; 36 import javafx.beans.property.ReadOnlyObjectWrapper; 37 import javafx.beans.value.ChangeListener; 38 import static org.junit.Assert.assertEquals; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 43 public class ReadOnlyDoublePropertyTest { 44 45 private static final double DEFAULT = 0.0; 46 private static final float EPSILON = 1e-6f; 47 48 @Before 49 public void setUp() throws Exception { 50 } 51 52 @Test 53 public void testToString() { 54 final ReadOnlyDoubleProperty v1 = new ReadOnlyDoublePropertyStub(null, ""); 55 assertEquals("ReadOnlyDoubleProperty [value: " + DEFAULT + "]", v1.toString()); 56 57 final ReadOnlyDoubleProperty v2 = new ReadOnlyDoublePropertyStub(null, null); 58 assertEquals("ReadOnlyDoubleProperty [value: " + DEFAULT + "]", v2.toString()); 59 60 final Object bean = new Object(); 61 final String name = "My name"; 62 final ReadOnlyDoubleProperty v3 = new ReadOnlyDoublePropertyStub(bean, name); 63 assertEquals("ReadOnlyDoubleProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v3.toString()); 64 65 final ReadOnlyDoubleProperty v4 = new ReadOnlyDoublePropertyStub(bean, ""); 66 assertEquals("ReadOnlyDoubleProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString()); 67 68 final ReadOnlyDoubleProperty v5 = new ReadOnlyDoublePropertyStub(bean, null); 69 assertEquals("ReadOnlyDoubleProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v5.toString()); 70 71 final ReadOnlyDoubleProperty v6 = new ReadOnlyDoublePropertyStub(null, name); 72 assertEquals("ReadOnlyDoubleProperty [name: My name, value: " + DEFAULT + "]", v6.toString()); 73 74 } 75 76 @Test 77 public void testAsObject() { 78 final ReadOnlyDoubleWrapper valueModel = new ReadOnlyDoubleWrapper(); 79 final ReadOnlyObjectProperty<Double> exp = valueModel.getReadOnlyProperty().asObject(); 80 81 assertEquals(0.0, exp.getValue(), EPSILON); 82 valueModel.set(-4354.3); 83 assertEquals(-4354.3, exp.getValue(), EPSILON); 84 valueModel.set(5e11); 85 assertEquals(5e11, exp.getValue(), EPSILON); 86 } 87 88 @Test 89 public void testObjectToDouble() { 90 final ReadOnlyObjectWrapper<Double> valueModel = new ReadOnlyObjectWrapper<Double>(); 91 final ReadOnlyDoubleProperty exp = ReadOnlyDoubleProperty.readOnlyDoubleProperty(valueModel.getReadOnlyProperty()); 92 93 94 assertEquals(0.0, exp.doubleValue(), EPSILON); 95 valueModel.set(-4354.3); 96 assertEquals(-4354.3, exp.doubleValue(), EPSILON); 97 valueModel.set(5e11); 98 assertEquals(5e11, exp.doubleValue(), EPSILON); 99 } 100 101 private static class ReadOnlyDoublePropertyStub extends ReadOnlyDoubleProperty { 102 103 private final Object bean; 104 private final String name; 105 106 private ReadOnlyDoublePropertyStub(Object bean, String name) { 107 this.bean = bean; 108 this.name = name; 109 } 110 111 @Override public Object getBean() { return bean; } 112 @Override public String getName() { return name; } 113 @Override public double get() { return 0.0; } 114 115 @Override 116 public void addListener(ChangeListener<? super Number> listener) { 117 } 118 119 @Override 120 public void removeListener(ChangeListener<? super Number> listener) { 121 } 122 123 @Override 124 public void addListener(InvalidationListener listener) { 125 } 126 127 @Override 128 public void removeListener(InvalidationListener listener) { 129 } 130 131 } 132 133 }