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