001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.binding.support; 017 018 import java.beans.PropertyChangeEvent; 019 import java.beans.PropertyChangeListener; 020 import java.beans.PropertyChangeSupport; 021 022 import junit.framework.TestCase; 023 024 import org.springframework.binding.value.PropertyChangePublisher; 025 import org.springframework.binding.value.support.AbstractPropertyChangePublisher; 026 027 /** 028 * Tests class {@link PropertyChangeSupportUtils}. 029 * 030 * @author Oliver Hutchison 031 */ 032 public class PropertyChangeSupportUtilsTests extends TestCase { 033 034 /** 035 * Checks that #supportsBoundProperties detects observable classes. 036 */ 037 public void testDetectObservableClasses() { 038 Class[] observableClasses = new Class[] {PropertyChangePublisherImpl.class, StandardJavaBeanImpl.class}; 039 for (int i = 0; i < observableClasses.length; i++) { 040 Class beanClass = observableClasses[i]; 041 assertTrue("Could not detect that the class supports bound properties.", 042 PropertyChangeSupportUtils.supportsBoundProperties(beanClass)); 043 } 044 } 045 046 /** 047 * Checks that #supportsBoundProperties rejects unobservable classes. 048 */ 049 public void testRejectUnobservableClasses() { 050 Class[] unobservableClasses = new Class[] {Object.class, int.class}; 051 for (int i = 0; i < unobservableClasses.length; i++) { 052 Class beanClass = unobservableClasses[i]; 053 assertFalse("Failed to reject a class that supports no bound properties.", 054 PropertyChangeSupportUtils.supportsBoundProperties(beanClass)); 055 } 056 } 057 058 public void testAddRemovePropertyChangeListener() { 059 final PropertyChangeListener listener = new PropertyChangeListener() { 060 public void propertyChange(PropertyChangeEvent evt) { 061 } 062 }; 063 064 PropertyChangePublisherImpl bean1 = new PropertyChangePublisherImpl(); 065 PropertyChangeSupportUtils.addPropertyChangeListener(bean1, "propertName", listener); 066 PropertyChangeListener[] listeners = bean1.getPropertyChangeListeners("propertName"); 067 assertEquals(1, listeners.length); 068 assertSame(listener, listeners[0]); 069 PropertyChangeSupportUtils.removePropertyChangeListener(bean1, "propertName", listener); 070 listeners = bean1.getPropertyChangeListeners("propertName"); 071 assertEquals(0, listeners.length); 072 073 StandardJavaBeanImpl bean2 = new StandardJavaBeanImpl(); 074 PropertyChangeSupportUtils.addPropertyChangeListener(bean2, "propertName", listener); 075 listeners = bean2.getPropertyChangeListeners("propertName"); 076 assertEquals(1, listeners.length); 077 assertSame(listener, listeners[0]); 078 PropertyChangeSupportUtils.removePropertyChangeListener(bean2, "propertName", listener); 079 listeners = bean2.getPropertyChangeListeners("propertName"); 080 assertEquals(0, listeners.length); 081 } 082 083 private class PropertyChangePublisherImpl extends AbstractPropertyChangePublisher implements 084 PropertyChangePublisher { 085 } 086 087 private class StandardJavaBeanImpl extends PropertyChangeSupport { 088 public StandardJavaBeanImpl() { 089 super("whatever"); 090 } 091 } 092 }