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.javafx.beans.property;
  27 
  28 
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.fail;
  31 import javafx.beans.InvalidationListener;
  32 import javafx.beans.property.SimpleStringProperty;
  33 import javafx.beans.property.StringProperty;
  34 import javafx.beans.value.ChangeListener;
  35 import javafx.beans.value.ObservableValue;
  36 
  37 import org.junit.Test;
  38 
  39 public class StringPropertyTest {
  40     
  41     private static final Object NO_BEAN = null;
  42     private static final String NO_NAME_1 = null;
  43     private static final String NO_NAME_2 = "";
  44     private static final String VALUE_1 = "Hello World";
  45     private static final String VALUE_2 = "Goodbye World";
  46     private static final String DEFAULT = null;
  47 
  48     @Test
  49     public void testBindBidirectional() {
  50         final StringProperty p1 = new SimpleStringProperty(VALUE_2);
  51         final StringProperty p2 = new SimpleStringProperty(VALUE_1);
  52         
  53         p1.bindBidirectional(p2);
  54         assertEquals(VALUE_1, p1.get());
  55         assertEquals(VALUE_1, p2.get());
  56         
  57         p1.set(VALUE_2);
  58         assertEquals(VALUE_2, p1.get());
  59         assertEquals(VALUE_2, p2.get());
  60         
  61         p2.set(VALUE_1);
  62         assertEquals(VALUE_1, p1.get());
  63         assertEquals(VALUE_1, p2.get());
  64         
  65         p1.unbindBidirectional(p2);
  66         p1.set(VALUE_2);
  67         assertEquals(VALUE_2, p1.get());
  68         assertEquals(VALUE_1, p2.get());
  69         
  70         p1.set(VALUE_1);
  71         p2.set(VALUE_2);
  72         assertEquals(VALUE_1, p1.get());
  73         assertEquals(VALUE_2, p2.get());
  74     }
  75     
  76     @Test
  77     public void testToString() {
  78         final StringProperty v0 = new StringPropertyStub(NO_BEAN, NO_NAME_1);
  79         assertEquals("StringProperty [value: " + DEFAULT + "]", v0.toString());
  80         
  81         final StringProperty v1 = new StringPropertyStub(NO_BEAN, NO_NAME_2);
  82         assertEquals("StringProperty [value: " + DEFAULT + "]", v1.toString());
  83         
  84         final Object bean = new Object();
  85         final String name = "My name";
  86         final StringProperty v2 = new StringPropertyStub(bean, name);
  87         assertEquals("StringProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v2.toString());
  88         v2.set(VALUE_1);
  89         assertEquals("StringProperty [bean: " + bean.toString() + ", name: My name, value: " + VALUE_1 + "]", v2.toString());
  90         
  91         final StringProperty v3 = new StringPropertyStub(bean, NO_NAME_1);
  92         assertEquals("StringProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v3.toString());
  93         v3.set(VALUE_1);
  94         assertEquals("StringProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v3.toString());
  95 
  96         final StringProperty v4 = new StringPropertyStub(bean, NO_NAME_2);
  97         assertEquals("StringProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString());
  98         v4.set(VALUE_1);
  99         assertEquals("StringProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v4.toString());
 100 
 101         final StringProperty v5 = new StringPropertyStub(NO_BEAN, name);
 102         assertEquals("StringProperty [name: My name, value: " + DEFAULT + "]", v5.toString());
 103         v5.set(VALUE_1);
 104         assertEquals("StringProperty [name: My name, value: " + VALUE_1 + "]", v5.toString());
 105     }
 106     
 107     private class StringPropertyStub extends StringProperty {
 108         
 109         private final Object bean;
 110         private final String name;
 111         private String value;
 112         
 113         private StringPropertyStub(Object bean, String name) {
 114             this.bean = bean;
 115             this.name = name;
 116         }
 117 
 118         @Override
 119         public Object getBean() {
 120             return bean;
 121         }
 122 
 123         @Override
 124         public String getName() {
 125             return name;
 126         }
 127 
 128         @Override
 129         public String get() {
 130             return value;
 131         }
 132 
 133         @Override
 134         public void set(String value) {
 135             this.value = value;
 136         }
 137         
 138         @Override
 139         public void bind(ObservableValue<? extends String> observable) {
 140             fail("Not in use");
 141         }
 142 
 143         @Override
 144         public void unbind() {
 145             fail("Not in use");
 146         }
 147 
 148         @Override
 149         public boolean isBound() {
 150             fail("Not in use");
 151             return false;
 152         }
 153 
 154         @Override
 155         public void addListener(ChangeListener<? super String> listener) {
 156             fail("Not in use");
 157         }
 158 
 159         @Override
 160         public void removeListener(ChangeListener<? super String> listener) {
 161             fail("Not in use");
 162         }
 163 
 164         @Override
 165         public void addListener(InvalidationListener listener) {
 166             fail("Not in use");
 167         }
 168 
 169         @Override
 170         public void removeListener(InvalidationListener listener) {
 171             fail("Not in use");
 172         }
 173 
 174     }
 175 }