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