1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.binding.support;
17  
18  import java.beans.PropertyChangeEvent;
19  import java.beans.PropertyChangeListener;
20  import java.beans.PropertyChangeSupport;
21  
22  import junit.framework.TestCase;
23  
24  import org.springframework.binding.value.PropertyChangePublisher;
25  import org.springframework.binding.value.support.AbstractPropertyChangePublisher;
26  
27  /**
28   * Tests class {@link PropertyChangeSupportUtils}.
29   * 
30   * @author Oliver Hutchison
31   */
32  public class PropertyChangeSupportUtilsTests extends TestCase {
33  
34      /**
35       * Checks that #supportsBoundProperties detects observable classes.
36       */
37      public void testDetectObservableClasses() {
38          Class[] observableClasses = new Class[] {PropertyChangePublisherImpl.class, StandardJavaBeanImpl.class};
39          for (int i = 0; i < observableClasses.length; i++) {
40              Class beanClass = observableClasses[i];
41              assertTrue("Could not detect that the class supports bound properties.",
42                      PropertyChangeSupportUtils.supportsBoundProperties(beanClass));
43          }
44      }
45  
46      /**
47       * Checks that #supportsBoundProperties rejects unobservable classes.
48       */
49      public void testRejectUnobservableClasses() {
50          Class[] unobservableClasses = new Class[] {Object.class, int.class};
51          for (int i = 0; i < unobservableClasses.length; i++) {
52              Class beanClass = unobservableClasses[i];
53              assertFalse("Failed to reject a class that supports no bound properties.",
54                      PropertyChangeSupportUtils.supportsBoundProperties(beanClass));
55          }
56      }
57  
58      public void testAddRemovePropertyChangeListener() {
59          final PropertyChangeListener listener = new PropertyChangeListener() {
60              public void propertyChange(PropertyChangeEvent evt) {
61              }
62          };
63  
64          PropertyChangePublisherImpl bean1 = new PropertyChangePublisherImpl();
65          PropertyChangeSupportUtils.addPropertyChangeListener(bean1, "propertName", listener);
66          PropertyChangeListener[] listeners = bean1.getPropertyChangeListeners("propertName");
67          assertEquals(1, listeners.length);
68          assertSame(listener, listeners[0]);
69          PropertyChangeSupportUtils.removePropertyChangeListener(bean1, "propertName", listener);
70          listeners = bean1.getPropertyChangeListeners("propertName");
71          assertEquals(0, listeners.length);
72  
73          StandardJavaBeanImpl bean2 = new StandardJavaBeanImpl();
74          PropertyChangeSupportUtils.addPropertyChangeListener(bean2, "propertName", listener);
75          listeners = bean2.getPropertyChangeListeners("propertName");
76          assertEquals(1, listeners.length);
77          assertSame(listener, listeners[0]);
78          PropertyChangeSupportUtils.removePropertyChangeListener(bean2, "propertName", listener);
79          listeners = bean2.getPropertyChangeListeners("propertName");
80          assertEquals(0, listeners.length);
81      }
82  
83      private class PropertyChangePublisherImpl extends AbstractPropertyChangePublisher implements
84              PropertyChangePublisher {
85      }
86  
87      private class StandardJavaBeanImpl extends PropertyChangeSupport {
88          public StandardJavaBeanImpl() {
89              super("whatever");
90          }
91      }
92  }