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 javafx.binding;
  27 
  28 import static org.junit.Assert.assertFalse;
  29 import static org.junit.Assert.assertTrue;
  30 
  31 import javafx.beans.InvalidationListenerMock;
  32 import javafx.beans.binding.Bindings;
  33 import javafx.beans.binding.BooleanBinding;
  34 import javafx.beans.property.ObjectProperty;
  35 import javafx.beans.property.SimpleObjectProperty;
  36 import javafx.beans.property.SimpleStringProperty;
  37 import javafx.beans.property.StringProperty;
  38 
  39 import org.junit.Before;
  40 import org.junit.Test;
  41 
  42 public class BindingsIsNullTest {
  43 
  44     private ObjectProperty<Object> oo;
  45     private StringProperty os;
  46     private InvalidationListenerMock observer;
  47 
  48     @Before
  49     public void setUp() {
  50         oo = new SimpleObjectProperty<Object>();
  51         os = new SimpleStringProperty();
  52         observer = new InvalidationListenerMock();
  53     }
  54 
  55     @Test
  56     public void test_Object_IsNull() {
  57         final BooleanBinding binding = Bindings.isNull(oo);
  58         binding.addListener(observer);
  59 
  60         // check initial value
  61         assertTrue(binding.get());
  62         DependencyUtils.checkDependencies(binding.getDependencies(), oo);
  63         observer.reset();
  64 
  65         // change operand
  66         oo.set(new Object());
  67         assertFalse(binding.get());
  68         observer.check(binding, 1);
  69 
  70         // change again
  71         oo.set(null);
  72         assertTrue(binding.get());
  73         observer.check(binding, 1);
  74     }
  75     
  76     @Test
  77     public void test_Object_IsNotNull() {
  78         final BooleanBinding binding = Bindings.isNotNull(oo);
  79         binding.addListener(observer);
  80 
  81         // check initial value
  82         assertFalse(binding.get());
  83         DependencyUtils.checkDependencies(binding.getDependencies(), oo);
  84         observer.reset();
  85 
  86         // change operand
  87         oo.set(new Object());
  88         assertTrue(binding.get());
  89         observer.check(binding, 1);
  90 
  91         // change again
  92         oo.set(null);
  93         assertFalse(binding.get());
  94         observer.check(binding, 1);
  95     }
  96 
  97     @Test
  98     public void test_String_IsNull() {
  99         final BooleanBinding binding = Bindings.isNull(os);
 100         binding.addListener(observer);
 101 
 102         // check initial value
 103         assertTrue(binding.get());
 104         DependencyUtils.checkDependencies(binding.getDependencies(), os);
 105         observer.reset();
 106 
 107         // change operand
 108         os.set("Hello World");
 109         assertFalse(binding.get());
 110         observer.check(binding, 1);
 111 
 112         // change again
 113         os.set(null);
 114         assertTrue(binding.get());
 115         observer.check(binding, 1);
 116     }
 117     
 118     @Test
 119     public void test_String_IsNotNull() {
 120         final BooleanBinding binding = Bindings.isNotNull(os);
 121         binding.addListener(observer);
 122 
 123         // check initial value
 124         assertFalse(binding.get());
 125         DependencyUtils.checkDependencies(binding.getDependencies(), os);
 126         observer.reset();
 127 
 128         // change operand
 129         os.set("Hello World");
 130         assertTrue(binding.get());
 131         observer.check(binding, 1);
 132 
 133         // change again
 134         os.set(null);
 135         assertFalse(binding.get());
 136         observer.check(binding, 1);
 137     }
 138 
 139     @Test(expected=NullPointerException.class)
 140     public void test_IsNull_NPE() {
 141         Bindings.isNull(null);
 142     }
 143 
 144     @Test(expected=NullPointerException.class)
 145     public void test_IsNotNull_NPE() {
 146         Bindings.isNotNull(null);
 147     }
 148 
 149 }