1 /*
   2  * Copyright (c) 2011, 2015, 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 javafx.beans.InvalidationListener;
  29 import javafx.beans.property.DoubleProperty;
  30 import javafx.beans.property.ObjectProperty;
  31 import javafx.beans.property.SimpleDoubleProperty;
  32 import javafx.beans.property.SimpleObjectProperty;
  33 import javafx.beans.value.ChangeListener;
  34 import javafx.beans.value.ObservableValue;
  35 import test.com.sun.javafx.binding.ErrorLoggingUtiltity;
  36 import org.junit.BeforeClass;
  37 import org.junit.Test;
  38 
  39 import static org.junit.Assert.*;
  40 
  41 public class DoublePropertyTest {
  42 
  43     private static final Object NO_BEAN = null;
  44     private static final String NO_NAME_1 = null;
  45     private static final String NO_NAME_2 = "";
  46     private static final double VALUE_1 = Math.PI;
  47     private static final double VALUE_2 = -Math.E;
  48     private static final double DEFAULT = 0.0;
  49     private static final double EPSILON = 1e-12;
  50 
  51     @BeforeClass
  52     public static void setUpClass() {
  53         ErrorLoggingUtiltity.reset();
  54     }
  55 
  56     @Test
  57     public void testSetValue_Null() {
  58         final DoubleProperty p = new SimpleDoubleProperty(VALUE_1);
  59         p.setValue(null);
  60         assertEquals(DEFAULT, p.get(), EPSILON);
  61         ErrorLoggingUtiltity.checkFine(NullPointerException.class);
  62     }
  63 
  64     @Test
  65     public void testBindBidirectional() {
  66         final DoubleProperty p1 = new SimpleDoubleProperty(VALUE_2);
  67         final DoubleProperty p2 = new SimpleDoubleProperty(VALUE_1);
  68 
  69         p1.bindBidirectional(p2);
  70         assertEquals(VALUE_1, p1.get(), EPSILON);
  71         assertEquals(VALUE_1, p2.get(), EPSILON);
  72 
  73         p1.set(VALUE_2);
  74         assertEquals(VALUE_2, p1.get(), EPSILON);
  75         assertEquals(VALUE_2, p2.get(), EPSILON);
  76 
  77         p2.set(VALUE_1);
  78         assertEquals(VALUE_1, p1.get(), EPSILON);
  79         assertEquals(VALUE_1, p2.get(), EPSILON);
  80 
  81         p1.unbindBidirectional(p2);
  82         p1.set(VALUE_2);
  83         assertEquals(VALUE_2, p1.get(), EPSILON);
  84         assertEquals(VALUE_1, p2.get(), EPSILON);
  85 
  86         p1.set(VALUE_1);
  87         p2.set(VALUE_2);
  88         assertEquals(VALUE_1, p1.get(), EPSILON);
  89         assertEquals(VALUE_2, p2.get(), EPSILON);
  90     }
  91 
  92     @Test
  93     public void testToString() {
  94         final DoubleProperty v0 = new DoublePropertyStub(NO_BEAN, NO_NAME_1);
  95         assertEquals("DoubleProperty [value: " + DEFAULT + "]", v0.toString());
  96 
  97         final DoubleProperty v1 = new DoublePropertyStub(NO_BEAN, NO_NAME_2);
  98         assertEquals("DoubleProperty [value: " + DEFAULT + "]", v1.toString());
  99 
 100         final Object bean = new Object();
 101         final String name = "My name";
 102         final DoubleProperty v2 = new DoublePropertyStub(bean, name);
 103         assertEquals("DoubleProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v2.toString());
 104         v2.set(VALUE_1);
 105         assertEquals("DoubleProperty [bean: " + bean.toString() + ", name: My name, value: " + VALUE_1 + "]", v2.toString());
 106 
 107         final DoubleProperty v3 = new DoublePropertyStub(bean, NO_NAME_1);
 108         assertEquals("DoubleProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v3.toString());
 109         v3.set(VALUE_1);
 110         assertEquals("DoubleProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v3.toString());
 111 
 112         final DoubleProperty v4 = new DoublePropertyStub(bean, NO_NAME_2);
 113         assertEquals("DoubleProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString());
 114         v4.set(VALUE_1);
 115         assertEquals("DoubleProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v4.toString());
 116 
 117         final DoubleProperty v5 = new DoublePropertyStub(NO_BEAN, name);
 118         assertEquals("DoubleProperty [name: My name, value: " + DEFAULT + "]", v5.toString());
 119         v5.set(VALUE_1);
 120         assertEquals("DoubleProperty [name: My name, value: " + VALUE_1 + "]", v5.toString());
 121     }
 122 
 123     @Test
 124     public void testAsObject() {
 125         final DoubleProperty valueModel = new SimpleDoubleProperty(2d);
 126         final ObjectProperty<Double> exp = valueModel.asObject();
 127 
 128         assertEquals(2d, exp.getValue(), EPSILON);
 129         valueModel.set(-4354.3);
 130         assertEquals(-4354.3, exp.getValue(), EPSILON);
 131         valueModel.set(5e11);
 132         assertEquals(5e11, exp.getValue(), EPSILON);
 133 
 134         exp.set(1234.0);
 135         assertEquals(1234.0, valueModel.doubleValue(), EPSILON);
 136 
 137     }
 138 
 139     @Test
 140     public void testObjectToDouble() {
 141         final ObjectProperty<Double> valueModel = new SimpleObjectProperty<Double>();
 142         final DoubleProperty exp = DoubleProperty.doubleProperty(valueModel);
 143 
 144         assertEquals(0.0, exp.doubleValue(), EPSILON);
 145         valueModel.set(-4354.3);
 146         assertEquals(-4354.3, exp.doubleValue(), EPSILON);
 147         valueModel.set(5e11);
 148         assertEquals(5e11, exp.doubleValue(), EPSILON);
 149 
 150         exp.set(1234.0);
 151         assertEquals(1234.0, valueModel.getValue(), EPSILON);
 152     }
 153 
 154     private class DoublePropertyStub extends DoubleProperty {
 155 
 156         private final Object bean;
 157         private final String name;
 158         private double value;
 159 
 160         private DoublePropertyStub(Object bean, String name) {
 161             this.bean = bean;
 162             this.name = name;
 163         }
 164 
 165         @Override
 166         public Object getBean() {
 167             return bean;
 168         }
 169 
 170         @Override
 171         public String getName() {
 172             return name;
 173         }
 174 
 175         @Override
 176         public double get() {
 177             return value;
 178         }
 179 
 180         @Override
 181         public void set(double value) {
 182             this.value = value;
 183         }
 184 
 185         @Override
 186         public void bind(ObservableValue<? extends Number> observable) {
 187             fail("Not in use");
 188         }
 189 
 190         @Override
 191         public void unbind() {
 192             fail("Not in use");
 193         }
 194 
 195         @Override
 196         public boolean isBound() {
 197             fail("Not in use");
 198             return false;
 199         }
 200 
 201         @Override
 202         public void addListener(ChangeListener<? super Number> listener) {
 203             fail("Not in use");
 204         }
 205 
 206         @Override
 207         public void removeListener(ChangeListener<? super Number> listener) {
 208             fail("Not in use");
 209         }
 210 
 211         @Override
 212         public void addListener(InvalidationListener listener) {
 213             fail("Not in use");
 214         }
 215 
 216         @Override
 217         public void removeListener(InvalidationListener listener) {
 218             fail("Not in use");
 219         }
 220 
 221     }
 222 }