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