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