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