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.IntegerProperty;
  31 import javafx.beans.property.ObjectProperty;
  32 import javafx.beans.property.SimpleIntegerProperty;
  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 IntegerPropertyTest {
  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 int VALUE_1 = 42;
  49     private static final int VALUE_2 = -13;
  50     private static final int DEFAULT = 0;
  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 IntegerProperty p = new SimpleIntegerProperty(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 IntegerProperty p1 = new SimpleIntegerProperty(VALUE_2);
  75         final IntegerProperty p2 = new SimpleIntegerProperty(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 IntegerProperty v0 = new IntegerPropertyStub(NO_BEAN, NO_NAME_1);
 103         assertEquals("IntegerProperty [value: " + DEFAULT + "]", v0.toString());
 104         
 105         final IntegerProperty v1 = new IntegerPropertyStub(NO_BEAN, NO_NAME_2);
 106         assertEquals("IntegerProperty [value: " + DEFAULT + "]", v1.toString());
 107         
 108         final Object bean = new Object();
 109         final String name = "My name";
 110         final IntegerProperty v2 = new IntegerPropertyStub(bean, name);
 111         assertEquals("IntegerProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v2.toString());
 112         v2.set(VALUE_1);
 113         assertEquals("IntegerProperty [bean: " + bean.toString() + ", name: My name, value: " + VALUE_1 + "]", v2.toString());
 114         
 115         final IntegerProperty v3 = new IntegerPropertyStub(bean, NO_NAME_1);
 116         assertEquals("IntegerProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v3.toString());
 117         v3.set(VALUE_1);
 118         assertEquals("IntegerProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v3.toString());
 119 
 120         final IntegerProperty v4 = new IntegerPropertyStub(bean, NO_NAME_2);
 121         assertEquals("IntegerProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString());
 122         v4.set(VALUE_1);
 123         assertEquals("IntegerProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v4.toString());
 124 
 125         final IntegerProperty v5 = new IntegerPropertyStub(NO_BEAN, name);
 126         assertEquals("IntegerProperty [name: My name, value: " + DEFAULT + "]", v5.toString());
 127         v5.set(VALUE_1);
 128         assertEquals("IntegerProperty [name: My name, value: " + VALUE_1 + "]", v5.toString());
 129     }
 130     
 131     @Test
 132     public void testAsObject() {
 133         final IntegerProperty valueModel = new SimpleIntegerProperty();
 134         final ObjectProperty<Integer> exp = valueModel.asObject();
 135         
 136         assertEquals(Integer.valueOf(0), exp.getValue());
 137         valueModel.set(-4354);
 138         assertEquals(Integer.valueOf(-4354), exp.getValue());
 139         valueModel.set(5);
 140         assertEquals(Integer.valueOf(5), exp.getValue());
 141         
 142         exp.set(10);
 143         assertEquals(10, valueModel.intValue());
 144 
 145     }
 146     
 147     @Test
 148     public void testObjectToInteger() {
 149         final ObjectProperty<Integer> valueModel = new SimpleObjectProperty<Integer>(2);
 150         final IntegerProperty exp = IntegerProperty.integerProperty(valueModel);
 151         
 152         assertEquals(2, exp.intValue());
 153         valueModel.set(-4354);
 154         assertEquals(-4354, exp.intValue());
 155         valueModel.set(5);
 156         assertEquals(5, exp.intValue());
 157         
 158         exp.set(10);
 159         assertEquals(Integer.valueOf(10), valueModel.getValue());
 160 
 161     }
 162     
 163     private class IntegerPropertyStub extends IntegerProperty {
 164         
 165         private final Object bean;
 166         private final String name;
 167         private int value;
 168         
 169         private IntegerPropertyStub(Object bean, String name) {
 170             this.bean = bean;
 171             this.name = name;
 172         }
 173 
 174         @Override
 175         public Object getBean() {
 176             return bean;
 177         }
 178 
 179         @Override
 180         public String getName() {
 181             return name;
 182         }
 183 
 184         @Override
 185         public int get() {
 186             return value;
 187         }
 188 
 189         @Override
 190         public void set(int value) {
 191             this.value = value;
 192         }
 193         
 194         @Override
 195         public void bind(ObservableValue<? extends Number> observable) {
 196             fail("Not in use");
 197         }
 198 
 199         @Override
 200         public void unbind() {
 201             fail("Not in use");
 202         }
 203 
 204         @Override
 205         public boolean isBound() {
 206             fail("Not in use");
 207             return false;
 208         }
 209 
 210         @Override
 211         public void addListener(ChangeListener<? super Number> listener) {
 212             fail("Not in use");
 213         }
 214 
 215         @Override
 216         public void removeListener(ChangeListener<? super Number> listener) {
 217             fail("Not in use");
 218         }
 219 
 220         @Override
 221         public void addListener(InvalidationListener listener) {
 222             fail("Not in use");
 223         }
 224 
 225         @Override
 226         public void removeListener(InvalidationListener listener) {
 227             fail("Not in use");
 228         }
 229 
 230     }
 231 }