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