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 com.sun.javafx.binding; 27 28 import javafx.beans.binding.Bindings; 29 import javafx.collections.FXCollections; 30 import javafx.collections.ObservableMap; 31 import org.junit.Before; 32 import org.junit.Test; 33 34 import java.util.HashMap; 35 import java.util.Map; 36 37 import static org.junit.Assert.*; 38 39 public class BidirectionalContentBindingMapTest { 40 41 private static final String key1 = "Key1"; 42 private static final String key2_1 = "Key2_1"; 43 private static final String key2_2 = "Key2_2"; 44 45 private ObservableMap<String, Integer> op1; 46 private ObservableMap<String, Integer> op2; 47 private ObservableMap<String, Integer> op3; 48 private Map<String, Integer> map0; 49 private Map<String, Integer> map1; 50 private Map<String, Integer> map2; 51 52 @Before 53 public void setUp() { 54 map0 = new HashMap<String, Integer>(); 55 map1 = new HashMap<String, Integer>(); 56 map1.put(key1, -1); 57 map2 = new HashMap<String, Integer>(); 58 map2.put(key2_1, 2); 59 map2.put(key2_2, 1); 60 61 op1 = FXCollections.observableMap(map1); 62 op2 = FXCollections.observableMap(map2); 63 op3 = FXCollections.observableMap(map0); 64 } 65 66 @Test 67 public void testBind() { 68 Bindings.bindContentBidirectional(op1, op2); 69 System.gc(); // making sure we did not not overdo weak references 70 assertEquals(map2, op1); 71 assertEquals(map2, op2); 72 73 op1.clear(); 74 op1.putAll(map1); 75 assertEquals(map1, op1); 76 assertEquals(map1, op2); 77 78 op1.clear(); 79 op1.putAll(map0); 80 assertEquals(map0, op1); 81 assertEquals(map0, op2); 82 83 op1.clear(); 84 op1.putAll(map2); 85 assertEquals(map2, op1); 86 assertEquals(map2, op2); 87 88 op2.clear(); 89 op2.putAll(map1); 90 assertEquals(map1, op1); 91 assertEquals(map1, op2); 92 93 op2.clear(); 94 op2.putAll(map0); 95 assertEquals(map0, op1); 96 assertEquals(map0, op2); 97 98 op2.clear(); 99 op2.putAll(map2); 100 assertEquals(map2, op1); 101 assertEquals(map2, op2); 102 } 103 104 @Test(expected = NullPointerException.class) 105 public void testBind_Null_X() { 106 Bindings.bindContentBidirectional(null, op2); 107 } 108 109 @Test(expected = NullPointerException.class) 110 public void testBind_X_Null() { 111 Bindings.bindContentBidirectional(op1, null); 112 } 113 114 @Test(expected = IllegalArgumentException.class) 115 public void testBind_X_Self() { 116 Bindings.bindContentBidirectional(op1, op1); 117 } 118 119 @Test 120 public void testUnbind() { 121 // unbind non-existing binding => no-op 122 Bindings.unbindContentBidirectional(op1, op2); 123 124 Bindings.bindContentBidirectional(op1, op2); 125 System.gc(); // making sure we did not not overdo weak references 126 assertEquals(map2, op1); 127 assertEquals(map2, op2); 128 129 Bindings.unbindContentBidirectional(op1, op2); 130 System.gc(); 131 assertEquals(map2, op1); 132 assertEquals(map2, op2); 133 134 op1.clear(); 135 op1.putAll(map1); 136 assertEquals(map1, op1); 137 assertEquals(map2, op2); 138 139 op2.clear(); 140 op2.putAll(map0); 141 assertEquals(map1, op1); 142 assertEquals(map0, op2); 143 144 // unbind in flipped order 145 Bindings.bindContentBidirectional(op1, op2); 146 System.gc(); // making sure we did not not overdo weak references 147 assertEquals(map0, op1); 148 assertEquals(map0, op2); 149 150 Bindings.unbindContentBidirectional(op2, op1); 151 System.gc(); 152 assertEquals(map0, op1); 153 assertEquals(map0, op2); 154 155 op1.clear(); 156 op1.putAll(map1); 157 assertEquals(map1, op1); 158 assertEquals(map0, op2); 159 160 op2.clear(); 161 op2.putAll(map2); 162 assertEquals(map1, op1); 163 assertEquals(map2, op2); 164 } 165 166 @Test(expected = NullPointerException.class) 167 public void testUnbind_Null_X() { 168 Bindings.unbindContentBidirectional(null, op2); 169 } 170 171 @Test(expected = NullPointerException.class) 172 public void testUnbind_X_Null() { 173 Bindings.unbindContentBidirectional(op1, null); 174 } 175 176 @Test(expected = IllegalArgumentException.class) 177 public void testUnbind_X_Self() { 178 Bindings.unbindContentBidirectional(op1, op1); 179 } 180 181 @Test 182 public void testChaining() { 183 Bindings.bindContentBidirectional(op1, op2); 184 Bindings.bindContentBidirectional(op2, op3); 185 System.gc(); // making sure we did not not overdo weak references 186 assertEquals(map0, op1); 187 assertEquals(map0, op2); 188 assertEquals(map0, op3); 189 190 op1.clear(); 191 op1.putAll(map1); 192 assertEquals(map1, op1); 193 assertEquals(map1, op2); 194 assertEquals(map1, op3); 195 196 op2.clear(); 197 op2.putAll(map2); 198 assertEquals(map2, op1); 199 assertEquals(map2, op2); 200 assertEquals(map2, op3); 201 202 op3.clear(); 203 op3.putAll(map0); 204 assertEquals(map0, op1); 205 assertEquals(map0, op2); 206 assertEquals(map0, op3); 207 208 // now unbind 209 Bindings.unbindContentBidirectional(op1, op2); 210 System.gc(); // making sure we did not not overdo weak references 211 assertEquals(map0, op1); 212 assertEquals(map0, op2); 213 assertEquals(map0, op3); 214 215 op1.clear(); 216 op1.putAll(map1); 217 assertEquals(map1, op1); 218 assertEquals(map0, op2); 219 assertEquals(map0, op3); 220 221 op2.clear(); 222 op2.putAll(map2); 223 assertEquals(map1, op1); 224 assertEquals(map2, op2); 225 assertEquals(map2, op3); 226 227 op3.clear(); 228 op3.putAll(map0); 229 assertEquals(map1, op1); 230 assertEquals(map0, op2); 231 assertEquals(map0, op3); 232 } 233 234 @Test 235 public void testHashCode() { 236 final int hc1 = BidirectionalContentBinding.bind(op1, op2).hashCode(); 237 BidirectionalContentBinding.unbind(op1, op2); 238 final int hc2 = BidirectionalContentBinding.bind(op2, op1).hashCode(); 239 assertEquals(hc1, hc2); 240 } 241 242 @Test 243 public void testEquals() { 244 final Object golden = BidirectionalContentBinding.bind(op1, op2); 245 BidirectionalContentBinding.unbind(op1, op2); 246 247 assertTrue(golden.equals(golden)); 248 assertFalse(golden.equals(null)); 249 assertFalse(golden.equals(op1)); 250 assertTrue(golden.equals(BidirectionalContentBinding.bind(op1, op2))); 251 BidirectionalContentBinding.unbind(op1, op2); 252 assertTrue(golden.equals(BidirectionalContentBinding.bind(op2, op1))); 253 BidirectionalContentBinding.unbind(op1, op2); 254 assertFalse(golden.equals(BidirectionalContentBinding.bind(op1, op3))); 255 BidirectionalContentBinding.unbind(op1, op3); 256 assertFalse(golden.equals(BidirectionalContentBinding.bind(op3, op1))); 257 BidirectionalContentBinding.unbind(op1, op3); 258 assertFalse(golden.equals(BidirectionalContentBinding.bind(op3, op2))); 259 BidirectionalContentBinding.unbind(op2, op3); 260 assertFalse(golden.equals(BidirectionalContentBinding.bind(op2, op3))); 261 BidirectionalContentBinding.unbind(op2, op3); 262 } 263 264 @Test 265 public void testEqualsWithGCedProperty() { 266 final Object binding1 = BidirectionalContentBinding.bind(op1, op2); 267 BidirectionalContentBinding.unbind(op1, op2); 268 final Object binding2 = BidirectionalContentBinding.bind(op1, op2); 269 BidirectionalContentBinding.unbind(op1, op2); 270 final Object binding3 = BidirectionalContentBinding.bind(op2, op1); 271 BidirectionalContentBinding.unbind(op1, op2); 272 final Object binding4 = BidirectionalContentBinding.bind(op2, op1); 273 BidirectionalContentBinding.unbind(op1, op2); 274 op1 = null; 275 System.gc(); 276 277 assertTrue(binding1.equals(binding1)); 278 assertFalse(binding1.equals(binding2)); 279 assertFalse(binding1.equals(binding3)); 280 281 assertTrue(binding3.equals(binding3)); 282 assertFalse(binding3.equals(binding1)); 283 assertFalse(binding3.equals(binding4)); 284 } 285 286 @Test 287 public void testChangeValue() { 288 Bindings.bindContentBidirectional(op1, op2); 289 assertEquals(2, op2.get(key2_1).intValue()); 290 op1.put(key2_1, 1); 291 assertEquals(1, op2.get(key2_1).intValue()); 292 } 293 }