1 /*
   2  * Copyright (c) 2011, 2013, 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.ReadOnlyBooleanProperty;
  31 import javafx.beans.property.ReadOnlyIntegerProperty;
  32 import javafx.beans.property.SetProperty;
  33 import javafx.beans.property.SimpleSetProperty;
  34 import javafx.beans.value.ChangeListener;
  35 import javafx.beans.value.ObservableValue;
  36 import javafx.collections.FXCollections;
  37 import javafx.collections.ObservableSet;
  38 import javafx.collections.SetChangeListener;
  39 import org.junit.Test;
  40 
  41 import static org.junit.Assert.assertEquals;
  42 import static org.junit.Assert.fail;
  43 
  44 public class SetPropertyTest {
  45 
  46     private static final Object NO_BEAN = null;
  47     private static final String NO_NAME_1 = null;
  48     private static final String NO_NAME_2 = "";
  49     private static final ObservableSet<Object> VALUE_1 = FXCollections.observableSet();
  50     private static final ObservableSet<Object> VALUE_2 = FXCollections.observableSet(new Object());
  51     private static final Object DEFAULT = null;
  52 
  53     @Test
  54     public void testBindBidirectional() {
  55         final SetProperty<Object> p1 = new SimpleSetProperty<Object>(VALUE_2);
  56         final SetProperty<Object> p2 = new SimpleSetProperty<Object>(VALUE_1);
  57         
  58         p1.bindBidirectional(p2);
  59         assertEquals(VALUE_1, p1.get());
  60         assertEquals(VALUE_1, p2.get());
  61         
  62         p1.set(VALUE_2);
  63         assertEquals(VALUE_2, p1.get());
  64         assertEquals(VALUE_2, p2.get());
  65         
  66         p2.set(VALUE_1);
  67         assertEquals(VALUE_1, p1.get());
  68         assertEquals(VALUE_1, p2.get());
  69         
  70         p1.unbindBidirectional(p2);
  71         p1.set(VALUE_2);
  72         assertEquals(VALUE_2, p1.get());
  73         assertEquals(VALUE_1, p2.get());
  74         
  75         p1.set(VALUE_1);
  76         p2.set(VALUE_2);
  77         assertEquals(VALUE_1, p1.get());
  78         assertEquals(VALUE_2, p2.get());
  79     }
  80     
  81     @Test
  82     public void testToString() {
  83         final SetProperty<Object> v0 = new SetPropertyStub(NO_BEAN, NO_NAME_1);
  84         assertEquals("SetProperty [value: " + DEFAULT + "]", v0.toString());
  85         
  86         final SetProperty<Object> v1 = new SetPropertyStub(NO_BEAN, NO_NAME_2);
  87         assertEquals("SetProperty [value: " + DEFAULT + "]", v1.toString());
  88         
  89         final Object bean = new Object();
  90         final String name = "My name";
  91         final SetProperty<Object> v2 = new SetPropertyStub(bean, name);
  92         assertEquals("SetProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v2.toString());
  93         v2.set(VALUE_1);
  94         assertEquals("SetProperty [bean: " + bean.toString() + ", name: My name, value: " + VALUE_1 + "]", v2.toString());
  95         
  96         final SetProperty<Object> v3 = new SetPropertyStub(bean, NO_NAME_1);
  97         assertEquals("SetProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v3.toString());
  98         v3.set(VALUE_1);
  99         assertEquals("SetProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v3.toString());
 100 
 101         final SetProperty<Object> v4 = new SetPropertyStub(bean, NO_NAME_2);
 102         assertEquals("SetProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString());
 103         v4.set(VALUE_1);
 104         assertEquals("SetProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v4.toString());
 105 
 106         final SetProperty<Object> v5 = new SetPropertyStub(NO_BEAN, name);
 107         assertEquals("SetProperty [name: My name, value: " + DEFAULT + "]", v5.toString());
 108         v5.set(VALUE_1);
 109         assertEquals("SetProperty [name: My name, value: " + VALUE_1 + "]", v5.toString());
 110     }
 111     
 112     private class SetPropertyStub extends SetProperty<Object> {
 113         
 114         private final Object bean;
 115         private final String name;
 116         private ObservableSet<Object> value;
 117         
 118         private SetPropertyStub(Object bean, String name) {
 119             this.bean = bean;
 120             this.name = name;
 121         }
 122 
 123         @Override
 124         public Object getBean() {
 125             return bean;
 126         }
 127 
 128         @Override
 129         public String getName() {
 130             return name;
 131         }
 132 
 133         @Override
 134         public ObservableSet<Object> get() {
 135             return value;
 136         }
 137 
 138         @Override
 139         public void set(ObservableSet<Object> value) {
 140             this.value = value;
 141         }
 142 
 143         @Override
 144         public void bind(ObservableValue<? extends ObservableSet<Object>> observable) {
 145             fail("Not in use");
 146         }
 147 
 148         @Override
 149         public void unbind() {
 150             fail("Not in use");
 151         }
 152 
 153         @Override
 154         public boolean isBound() {
 155             fail("Not in use");
 156             return false;
 157         }
 158 
 159         @Override
 160         public void addListener(ChangeListener<? super ObservableSet<Object>> listener) {
 161             fail("Not in use");
 162         }
 163 
 164         @Override
 165         public void removeListener(ChangeListener<? super ObservableSet<Object>> listener) {
 166             fail("Not in use");
 167         }
 168 
 169         @Override
 170         public void addListener(InvalidationListener listener) {
 171             fail("Not in use");
 172         }
 173 
 174         @Override
 175         public void removeListener(InvalidationListener listener) {
 176             fail("Not in use");
 177         }
 178 
 179         @Override
 180         public void addListener(SetChangeListener<? super Object> listChangeListener) {
 181             fail("Not in use");
 182         }
 183 
 184         @Override
 185         public void removeListener(SetChangeListener<? super Object> listChangeListener) {
 186             fail("Not in use");
 187         }
 188 
 189         @Override
 190         public ReadOnlyIntegerProperty sizeProperty() {
 191             fail("Not in use");
 192             return null;
 193         }
 194 
 195         @Override
 196         public ReadOnlyBooleanProperty emptyProperty() {
 197             fail("Not in use");
 198             return null;
 199         }
 200     }
 201 }