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 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertTrue;
  31 import javafx.beans.InvalidationListener;
  32 import javafx.beans.binding.ObjectExpression;
  33 import javafx.beans.property.ReadOnlyIntegerProperty;
  34 import javafx.beans.property.ReadOnlyIntegerWrapper;
  35 import javafx.beans.property.ReadOnlyObjectProperty;
  36 import javafx.beans.property.ReadOnlyObjectWrapper;
  37 import javafx.beans.value.ChangeListener;
  38 import static org.junit.Assert.assertEquals;
  39 
  40 import org.junit.Before;
  41 import org.junit.Test;
  42 
  43 public class ReadOnlyIntegerPropertyTest {
  44 
  45     private static final int DEFAULT = 0;
  46 
  47     @Before
  48     public void setUp() throws Exception {
  49     }
  50 
  51     @Test
  52     public void testToString() {
  53         final ReadOnlyIntegerProperty v1 = new ReadOnlyIntegerPropertyStub(null, "");
  54         assertEquals("ReadOnlyIntegerProperty [value: " + DEFAULT + "]", v1.toString());
  55         
  56         final ReadOnlyIntegerProperty v2 = new ReadOnlyIntegerPropertyStub(null, null);
  57         assertEquals("ReadOnlyIntegerProperty [value: " + DEFAULT + "]", v2.toString());
  58         
  59         final Object bean = new Object();
  60         final String name = "My name";
  61         final ReadOnlyIntegerProperty v3 = new ReadOnlyIntegerPropertyStub(bean, name);
  62         assertEquals("ReadOnlyIntegerProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v3.toString());
  63         
  64         final ReadOnlyIntegerProperty v4 = new ReadOnlyIntegerPropertyStub(bean, "");
  65         assertEquals("ReadOnlyIntegerProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString());
  66         
  67         final ReadOnlyIntegerProperty v5 = new ReadOnlyIntegerPropertyStub(bean, null);
  68         assertEquals("ReadOnlyIntegerProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v5.toString());
  69         
  70         final ReadOnlyIntegerProperty v6 = new ReadOnlyIntegerPropertyStub(null, name);
  71         assertEquals("ReadOnlyIntegerProperty [name: My name, value: " + DEFAULT + "]", v6.toString());
  72         
  73     }
  74     
  75     @Test
  76     public void testAsObject() {
  77         final ReadOnlyIntegerWrapper valueModel = new ReadOnlyIntegerWrapper();
  78         final ReadOnlyObjectProperty<Integer> exp = valueModel.getReadOnlyProperty().asObject();
  79 
  80         assertEquals(Integer.valueOf(0), exp.getValue());
  81         valueModel.set(-4354);
  82         assertEquals(Integer.valueOf(-4354), exp.getValue());
  83         valueModel.set(5);
  84         assertEquals(Integer.valueOf(5), exp.getValue());
  85     }
  86     
  87     @Test
  88     public void testObjectToInteger() {
  89         final ReadOnlyObjectWrapper<Integer> valueModel = new ReadOnlyObjectWrapper<Integer>();
  90         final ReadOnlyIntegerProperty exp = ReadOnlyIntegerProperty.readOnlyIntegerProperty(valueModel.getReadOnlyProperty());
  91 
  92         assertEquals(0, exp.intValue());
  93         valueModel.set(-4354);
  94         assertEquals(-4354, exp.intValue());
  95         valueModel.set(5);
  96         assertEquals(5, exp.intValue());
  97     }
  98     
  99     private static class ReadOnlyIntegerPropertyStub extends ReadOnlyIntegerProperty {
 100         
 101         private final Object bean;
 102         private final String name;
 103         
 104         private ReadOnlyIntegerPropertyStub(Object bean, String name) {
 105             this.bean = bean;
 106             this.name = name;
 107         }
 108 
 109         @Override public Object getBean() { return bean; }
 110         @Override public String getName() { return name; }
 111         @Override public int get() { return 0; }
 112 
 113         @Override
 114         public void addListener(ChangeListener<? super Number> listener) {
 115         }
 116 
 117         @Override
 118         public void removeListener(ChangeListener<? super Number> listener) {
 119         }
 120 
 121         @Override
 122         public void addListener(InvalidationListener listener) {
 123         }
 124 
 125         @Override
 126         public void removeListener(InvalidationListener listener) {
 127         }
 128         
 129     }
 130 
 131 }