1 /*
   2  * Copyright (c) 2012, 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 javafx.beans.property;
  27 
  28 import javafx.beans.InvalidationListenerMock;
  29 import javafx.beans.value.ChangeListenerMock;
  30 
  31 import org.junit.Before;
  32 import org.junit.Test;
  33 
  34 public class ReadOnlyStringPropertyBaseTest {
  35     
  36     private static final String UNDEFINED = null;
  37     private static final String DEFAULT = null;
  38     private static final String VALUE_1 = "Hello World!";
  39     private static final String VALUE_2 = "Goodbye World!";
  40     
  41     private ReadOnlyPropertyMock property;
  42     private InvalidationListenerMock invalidationListener;
  43     private ChangeListenerMock<String> changeListener;
  44 
  45     @Before
  46     public void setUp() throws Exception {
  47         property = new ReadOnlyPropertyMock();
  48         invalidationListener = new InvalidationListenerMock();
  49         changeListener = new ChangeListenerMock<String>(UNDEFINED);
  50     }
  51     
  52     @Test
  53     public void testInvalidationListener() {
  54         property.addListener(invalidationListener);
  55         property.get();
  56         invalidationListener.reset();
  57         property.set(VALUE_1);
  58         invalidationListener.check(property, 1);
  59         property.removeListener(invalidationListener);
  60         invalidationListener.reset();
  61         property.set(VALUE_2);
  62         invalidationListener.check(null, 0);
  63     }
  64 
  65     @Test
  66     public void testChangeListener() {
  67         property.addListener(changeListener);
  68         property.get();
  69         changeListener.reset();
  70         property.set(VALUE_1);
  71         changeListener.check(property, DEFAULT, VALUE_1, 1);
  72         property.removeListener(changeListener);
  73         changeListener.reset();
  74         property.set(VALUE_2);
  75         changeListener.check(null, UNDEFINED, UNDEFINED, 0);
  76     }
  77     
  78     private static class ReadOnlyPropertyMock extends ReadOnlyStringPropertyBase {
  79 
  80         private String value;
  81         
  82         @Override
  83         public Object getBean() {
  84             // not used
  85             return null;
  86         }
  87 
  88         @Override
  89         public String getName() {
  90             // not used
  91             return null;
  92         }
  93         
  94         private void set(String value) {
  95             this.value = value;
  96             fireValueChangedEvent();
  97         }
  98 
  99         @Override
 100         public String get() {
 101             return value;
 102         }
 103         
 104     }
 105 
 106 }