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