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.ContentBinding; 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 ContentBindingSetTest { 41 42 private Set<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>(2, 1); 55 set2.add(2); 56 set2.add(1); 57 58 op1 = new HashSet<Integer>(set1); 59 op2 = FXCollections.observableSet(set2); 60 op3 = FXCollections.observableSet(set0); 61 } 62 63 @Test 64 public void testBind() { 65 Bindings.bindContent(op1, op2); 66 System.gc(); // making sure we did not not overdo weak references 67 assertEquals(set2, op1); 68 assertEquals(set2, op2); 69 70 op2.clear(); 71 op2.addAll(set1); 72 assertEquals(set1, op1); 73 assertEquals(set1, op2); 74 75 op2.clear(); 76 op2.addAll(set0); 77 assertEquals(set0, op1); 78 assertEquals(set0, op2); 79 80 op2.clear(); 81 op2.addAll(set2); 82 assertEquals(set2, op1); 83 assertEquals(set2, op2); 84 } 85 86 @Test(expected = NullPointerException.class) 87 public void testBind_Null_X() { 88 Bindings.bindContent(null, op2); 89 } 90 91 @Test(expected = NullPointerException.class) 92 public void testBind_X_Null() { 93 Bindings.bindContent(op1, null); 94 } 95 96 @Test(expected = IllegalArgumentException.class) 97 public void testBind_X_Self() { 98 Bindings.bindContent(op2, op2); 99 } 100 101 @Test 102 public void testUnbind() { 103 // unbind non-existing binding => no-op 104 Bindings.unbindContent(op1, op2); 105 106 Bindings.bindContent(op1, op2); 107 System.gc(); // making sure we did not not overdo weak references 108 assertEquals(set2, op1); 109 assertEquals(set2, op2); 110 111 Bindings.unbindContent(op1, op2); 112 System.gc(); 113 assertEquals(set2, op1); 114 assertEquals(set2, op2); 115 116 op1.clear(); 117 assertEquals(set0, op1); 118 assertEquals(set2, op2); 119 120 op2.clear(); 121 op2.addAll(set1); 122 assertEquals(set0, op1); 123 assertEquals(set1, op2); 124 } 125 126 @Test(expected = NullPointerException.class) 127 public void testUnbind_Null_X() { 128 Bindings.unbindContent(null, op2); 129 } 130 131 @Test(expected = NullPointerException.class) 132 public void testUnbind_X_Null() { 133 Bindings.unbindContent(op1, null); 134 } 135 136 @Test(expected = IllegalArgumentException.class) 137 public void testUnbind_X_Self() { 138 Bindings.unbindContent(op2, op2); 139 } 140 141 @Test 142 public void testChaining() { 143 Bindings.bindContent(op1, op2); 144 Bindings.bindContent(op2, op3); 145 System.gc(); // making sure we did not not overdo weak references 146 assertEquals(set0, op1); 147 assertEquals(set0, op2); 148 assertEquals(set0, op3); 149 150 op3.clear(); 151 op3.addAll(set1); 152 assertEquals(set1, op1); 153 assertEquals(set1, op2); 154 assertEquals(set1, op3); 155 156 // now unbind 157 Bindings.unbindContent(op1, op2); 158 System.gc(); // making sure we did not not overdo weak references 159 assertEquals(set1, op1); 160 assertEquals(set1, op2); 161 assertEquals(set1, op3); 162 163 op3.clear(); 164 op3.addAll(set2); 165 assertEquals(set1, op1); 166 assertEquals(set2, op2); 167 assertEquals(set2, op3); 168 } 169 170 @Test 171 public void testHashCode() { 172 final int hc1 = ContentBinding.bind(op1, op2).hashCode(); 173 ContentBinding.unbind(op1, op2); 174 final int hc2 = ContentBinding.bind(op1, op2).hashCode(); 175 assertEquals(hc1, hc2); 176 } 177 178 @Test 179 public void testEquals() { 180 final Object golden = ContentBinding.bind(op1, op2); 181 ContentBinding.unbind(op1, op2); 182 183 assertTrue(golden.equals(golden)); 184 assertFalse(golden.equals(null)); 185 assertFalse(golden.equals(op1)); 186 assertTrue(golden.equals(ContentBinding.bind(op1, op2))); 187 ContentBinding.unbind(op1, op2); 188 assertFalse(golden.equals(ContentBinding.bind(op3, op2))); 189 ContentBinding.unbind(op2, op3); 190 assertFalse(golden.equals(ContentBinding.bind(op2, op3))); 191 ContentBinding.unbind(op2, op3); 192 } 193 194 @Test 195 public void testEqualsWithGCedProperty() { 196 final Object binding1 = ContentBinding.bind(op1, op2); 197 ContentBinding.unbind(op1, op2); 198 final Object binding2 = ContentBinding.bind(op1, op2); 199 ContentBinding.unbind(op1, op2); 200 op1 = null; 201 System.gc(); 202 203 assertTrue(binding1.equals(binding1)); 204 assertFalse(binding1.equals(binding2)); 205 } 206 }