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