1 /* 2 * Copyright (c) 2011, 2015, 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.javafx.beans.property; 27 28 import javafx.beans.InvalidationListener; 29 import javafx.beans.property.LongProperty; 30 import javafx.beans.property.ObjectProperty; 31 import javafx.beans.property.SimpleLongProperty; 32 import javafx.beans.property.SimpleObjectProperty; 33 import javafx.beans.value.ChangeListener; 34 import javafx.beans.value.ObservableValue; 35 import test.com.sun.javafx.binding.ErrorLoggingUtiltity; 36 import org.junit.BeforeClass; 37 import org.junit.Test; 38 39 import static org.junit.Assert.*; 40 41 public class LongPropertyTest { 42 43 private static final Object NO_BEAN = null; 44 private static final String NO_NAME_1 = null; 45 private static final String NO_NAME_2 = ""; 46 private static final long VALUE_1 = 1234567890L; 47 private static final long VALUE_2 = -987654321L; 48 private static final long DEFAULT = 0L; 49 50 @BeforeClass 51 public static void setUpClass() { 52 ErrorLoggingUtiltity.reset(); 53 } 54 55 @Test 56 public void testSetValue_Null() { 57 final LongProperty p = new SimpleLongProperty(VALUE_1); 58 p.setValue(null); 59 assertEquals(DEFAULT, p.get()); 60 ErrorLoggingUtiltity.checkFine(NullPointerException.class); 61 } 62 63 @Test 64 public void testBindBidirectional() { 65 final LongProperty p1 = new SimpleLongProperty(VALUE_2); 66 final LongProperty p2 = new SimpleLongProperty(VALUE_1); 67 68 p1.bindBidirectional(p2); 69 assertEquals(VALUE_1, p1.get()); 70 assertEquals(VALUE_1, p2.get()); 71 72 p1.set(VALUE_2); 73 assertEquals(VALUE_2, p1.get()); 74 assertEquals(VALUE_2, p2.get()); 75 76 p2.set(VALUE_1); 77 assertEquals(VALUE_1, p1.get()); 78 assertEquals(VALUE_1, p2.get()); 79 80 p1.unbindBidirectional(p2); 81 p1.set(VALUE_2); 82 assertEquals(VALUE_2, p1.get()); 83 assertEquals(VALUE_1, p2.get()); 84 85 p1.set(VALUE_1); 86 p2.set(VALUE_2); 87 assertEquals(VALUE_1, p1.get()); 88 assertEquals(VALUE_2, p2.get()); 89 } 90 91 @Test 92 public void testToString() { 93 final LongProperty v0 = new LongPropertyStub(NO_BEAN, NO_NAME_1); 94 assertEquals("LongProperty [value: " + DEFAULT + "]", v0.toString()); 95 96 final LongProperty v1 = new LongPropertyStub(NO_BEAN, NO_NAME_2); 97 assertEquals("LongProperty [value: " + DEFAULT + "]", v1.toString()); 98 99 final Object bean = new Object(); 100 final String name = "My name"; 101 final LongProperty v2 = new LongPropertyStub(bean, name); 102 assertEquals("LongProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v2.toString()); 103 v2.set(VALUE_1); 104 assertEquals("LongProperty [bean: " + bean.toString() + ", name: My name, value: " + VALUE_1 + "]", v2.toString()); 105 106 final LongProperty v3 = new LongPropertyStub(bean, NO_NAME_1); 107 assertEquals("LongProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v3.toString()); 108 v3.set(VALUE_1); 109 assertEquals("LongProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v3.toString()); 110 111 final LongProperty v4 = new LongPropertyStub(bean, NO_NAME_2); 112 assertEquals("LongProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString()); 113 v4.set(VALUE_1); 114 assertEquals("LongProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v4.toString()); 115 116 final LongProperty v5 = new LongPropertyStub(NO_BEAN, name); 117 assertEquals("LongProperty [name: My name, value: " + DEFAULT + "]", v5.toString()); 118 v5.set(VALUE_1); 119 assertEquals("LongProperty [name: My name, value: " + VALUE_1 + "]", v5.toString()); 120 } 121 122 @Test 123 public void testAsObject() { 124 final LongProperty valueModel = new SimpleLongProperty(); 125 final ObjectProperty<Long> exp = valueModel.asObject(); 126 127 assertEquals(Long.valueOf(0L), exp.getValue()); 128 valueModel.set(-4354L); 129 assertEquals(Long.valueOf(-4354L), exp.getValue()); 130 valueModel.set(5L); 131 assertEquals(Long.valueOf(5L), exp.getValue()); 132 133 exp.set(10L); 134 assertEquals(10L, valueModel.longValue()); 135 } 136 137 @Test 138 public void testObjectToLong() { 139 final ObjectProperty<Long> valueModel = new SimpleObjectProperty<Long>(2L); 140 final LongProperty exp = LongProperty.longProperty(valueModel); 141 142 assertEquals(2L, exp.longValue()); 143 valueModel.set(-4354L); 144 assertEquals(-4354L, exp.longValue()); 145 valueModel.set(5L); 146 assertEquals(5L, exp.longValue()); 147 148 exp.set(10L); 149 assertEquals(Long.valueOf(10L), valueModel.getValue()); 150 } 151 152 private class LongPropertyStub extends LongProperty { 153 154 private final Object bean; 155 private final String name; 156 private long value; 157 158 private LongPropertyStub(Object bean, String name) { 159 this.bean = bean; 160 this.name = name; 161 } 162 163 @Override 164 public Object getBean() { 165 return bean; 166 } 167 168 @Override 169 public String getName() { 170 return name; 171 } 172 173 @Override 174 public long get() { 175 return value; 176 } 177 178 @Override 179 public void set(long value) { 180 this.value = value; 181 } 182 183 @Override 184 public void bind(ObservableValue<? extends Number> observable) { 185 fail("Not in use"); 186 } 187 188 @Override 189 public void unbind() { 190 fail("Not in use"); 191 } 192 193 @Override 194 public boolean isBound() { 195 fail("Not in use"); 196 return false; 197 } 198 199 @Override 200 public void addListener(ChangeListener<? super Number> listener) { 201 fail("Not in use"); 202 } 203 204 @Override 205 public void removeListener(ChangeListener<? super Number> listener) { 206 fail("Not in use"); 207 } 208 209 @Override 210 public void addListener(InvalidationListener listener) { 211 fail("Not in use"); 212 } 213 214 @Override 215 public void removeListener(InvalidationListener listener) { 216 fail("Not in use"); 217 } 218 219 } 220 }