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