Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 91462 invoked from network); 18 Apr 2006 12:26:08 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 18 Apr 2006 12:26:08 -0000 Received: (qmail 99453 invoked by uid 500); 18 Apr 2006 12:26:06 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 99413 invoked by uid 500); 18 Apr 2006 12:26:06 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 99397 invoked by uid 99); 18 Apr 2006 12:26:06 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 18 Apr 2006 05:26:06 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 18 Apr 2006 05:26:03 -0700 Received: (qmail 90853 invoked by uid 65534); 18 Apr 2006 12:24:57 -0000 Message-ID: <20060418122457.90769.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r394923 [8/16] - in /incubator/harmony/enhanced/classlib/trunk/modules/beans: make/common/ src/test/java.injected/ src/test/java.injected/java/ src/test/java.injected/java/beans/ src/test/java/tests/ src/test/java/tests/api/ src/test/java/t... Date: Tue, 18 Apr 2006 12:11:45 -0000 To: harmony-commits@incubator.apache.org From: mloenko@apache.org X-Mailer: svnmailer-1.0.8 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyChangeSupportTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyChangeSupportTest.java?rev=394923&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyChangeSupportTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyChangeSupportTest.java Tue Apr 18 05:11:09 2006 @@ -0,0 +1,1360 @@ +/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests.api.java.beans; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeListenerProxy; +import java.beans.PropertyChangeSupport; +import java.io.Serializable; +import java.util.Arrays; + +import junit.framework.TestCase; +import tests.util.SerializationTester; + +/** + * Test class PropertyeChangeSupport. + */ +public class PropertyChangeSupportTest extends TestCase { + + /* + * Test the constructor with a normal parameter. + */ + public void testConstructor_Normal() { + Object src = new Object(); + new PropertyChangeSupport(src); + } + + /* + * Test the constructor with a null parameter. + */ + public void testConstructor_Null() { + try { + new PropertyChangeSupport(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method addPropertyChangeListener(PropertyeChangeListener) with a + * normal listener parameter. + */ + public void testAddPropertyChangeListener_PropertyChangeListener_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + PropertyChangeListener l4 = new PropertyChangeListenerProxy("myProp", + l3); + + sup.addPropertyChangeListener(l1); + + assertEquals(1, sup.getPropertyChangeListeners().length); + assertSame(l1, sup.getPropertyChangeListeners()[0]); + + sup.removePropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + assertEquals(1, sup.getPropertyChangeListeners().length); + assertSame(l2, ((PropertyChangeListenerProxy) sup + .getPropertyChangeListeners()[0]).getListener()); + assertNotSame(l3, sup.getPropertyChangeListeners()[0]); + + sup.removePropertyChangeListener(sup.getPropertyChangeListeners()[0]); + assertEquals(0, sup.getPropertyChangeListeners().length); + sup.addPropertyChangeListener(l4); + assertNotSame(l3, ((PropertyChangeListenerProxy) sup + .getPropertyChangeListeners()[0]).getListener()); + assertNotSame(l4, sup.getPropertyChangeListeners()[0]); + assertSame( + l2, + ((PropertyChangeListenerProxy) ((PropertyChangeListenerProxy) sup + .getPropertyChangeListeners()[0]).getListener()) + .getListener()); + } + + /* + * Test the method addPropertyChangeListener(PropertyeChangeListener) with a + * null listener parameter. + */ + public void testAddPropertyChangeListener_PropertyChangeListener_Null() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + + sup.addPropertyChangeListener(null); + PropertyChangeListener[] listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + assertSame(null, listeners[0]); + } + + /* + * Test the method addPropertyChangeListener(PropertyeChangeListener) with a + * listener parameter that has already been registered. + */ + public void testAddPropertyChangeListener_PropertyChangeListener_Duplicate() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l1); + + PropertyChangeListener[] listeners = sup.getPropertyChangeListeners(); + assertEquals(2, listeners.length); + assertSame(l1, listeners[0]); + assertSame(l1, listeners[1]); + } + + /* + * Test the method addPropertyChangeListener(PropertyeChangeListener, + * String) with a normal listener parameter and property name parameter. + */ + public void testAddPropertyChangeListener_PropertyChangeListener_String_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + + sup.addPropertyChangeListener("myProp2", l1); + + PropertyChangeListener[] listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + assertSame(l1, ((PropertyChangeListenerProxy) listeners[0]) + .getListener()); + + sup.removePropertyChangeListener(listeners[0]); + sup.addPropertyChangeListener("myProp3", l3); + listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + // pay attention to this recursive proxy + assertNotSame(l3, ((PropertyChangeListenerProxy) listeners[0]) + .getListener()); + assertNotSame(l3, ((PropertyChangeListenerProxy) listeners[0])); + assertSame( + l2, + ((PropertyChangeListenerProxy) ((PropertyChangeListenerProxy) listeners[0]) + .getListener()).getListener()); + + listeners = sup.getPropertyChangeListeners("myProp"); + assertEquals(0, listeners.length); + + listeners = sup.getPropertyChangeListeners("myProp3"); + assertEquals(1, listeners.length); + // pay attention to this recursive proxy + assertNotSame(l3, ((PropertyChangeListenerProxy) listeners[0]) + .getListener()); + assertNotSame(l3, ((PropertyChangeListenerProxy) listeners[0])); + assertSame(l2, ((PropertyChangeListenerProxy) listeners[0]) + .getListener()); + + } + + /* + * Test the method addPropertyChangeListener(PropertyeChangeListener, + * String) with a null listener parameter and a normal property name + * parameter. + */ + public void testAddPropertyChangeListener_PropertyChangeListener_String_NullListener() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + + sup.addPropertyChangeListener("myProp", null); + + PropertyChangeListener[] listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + assertSame(null, ((PropertyChangeListenerProxy) listeners[0]) + .getListener()); + + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + null); + assertEquals(1, listeners.length); + assertSame(null, ((PropertyChangeListenerProxy) listeners[0]) + .getListener()); + } + + /* + * Test the method addPropertyChangeListener(PropertyeChangeListener, + * String) with a normal listener parameter and a null property name + * parameter. + */ + public void testAddPropertyChangeListener_PropertyChangeListener_String_NullProperty() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + + try { + sup.addPropertyChangeListener(null, l1); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + + try { + sup.addPropertyChangeListener(null, l3); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + + l3 = new PropertyChangeListenerProxy(null, l2); + try { + sup.addPropertyChangeListener(l3); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method addPropertyChangeListener(PropertyeChangeListener, + * String) with a listener parameter that has already been registered for + * the named property. + */ + public void testAddPropertyChangeListener_PropertyChangeListener_String_Duplicate() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + + sup.addPropertyChangeListener("myProp2", l1); + sup.addPropertyChangeListener("myProp2", l1); + + PropertyChangeListener[] listeners = sup.getPropertyChangeListeners(); + assertEquals(2, listeners.length); + assertSame(l1, ((PropertyChangeListenerProxy) listeners[0]) + .getListener()); + assertSame(l1, ((PropertyChangeListenerProxy) listeners[1]) + .getListener()); + + sup.removePropertyChangeListener(listeners[0]); + sup.removePropertyChangeListener(listeners[1]); + sup.addPropertyChangeListener("myProp3", l3); + sup.addPropertyChangeListener("myProp3", l3); + listeners = sup.getPropertyChangeListeners(); + assertEquals(2, listeners.length); + assertSame( + l2, + ((PropertyChangeListenerProxy) ((PropertyChangeListenerProxy) listeners[0]) + .getListener()).getListener()); + assertSame( + l2, + ((PropertyChangeListenerProxy) ((PropertyChangeListenerProxy) listeners[1]) + .getListener()).getListener()); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener) + * with a normal listener parameter. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + + sup.addPropertyChangeListener(l1); + + PropertyChangeListener[] listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + sup.removePropertyChangeListener(l1); + listeners = sup.getPropertyChangeListeners(); + assertEquals(0, listeners.length); + sup.addPropertyChangeListener(l3); + listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + sup.removePropertyChangeListener(l3); + listeners = sup.getPropertyChangeListeners(); + assertEquals(0, listeners.length); + sup.addPropertyChangeListener("myProp3", l2); + listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + sup.removePropertyChangeListener(l2); + listeners = sup.getPropertyChangeListeners(); + assertEquals(1, listeners.length); + sup.removePropertyChangeListener(listeners[0]); + listeners = sup.getPropertyChangeListeners(); + assertEquals(0, listeners.length); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener) + * with a null listener parameter. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_Null() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + sup.removePropertyChangeListener(null); + assertEquals(0, sup.getPropertyChangeListeners().length); + sup.addPropertyChangeListener(null); + assertEquals(1, sup.getPropertyChangeListeners().length); + sup.removePropertyChangeListener(null); + assertEquals(0, sup.getPropertyChangeListeners().length); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener) + * with a non-registered listener parameter. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_NonRegistered() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + sup.removePropertyChangeListener(new MockPropertyChangeListener()); + assertEquals(0, sup.getPropertyChangeListeners().length); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener, + * String) when a listener for all properties has been registered. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_String_AllRegistered() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = new Object(); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + + sup.addPropertyChangeListener(l1); + + sup.removePropertyChangeListener("myProp", l1); + assertEquals(1, sup.getPropertyChangeListeners().length); + assertEquals(0, sup.getPropertyChangeListeners("myProp").length); + sup.firePropertyChange("myProp", oldValue, newValue); + l1.assertCalled(); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener, + * String) when a listener for the named property has been registered. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_String_PropertyRegistered() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + sup.addPropertyChangeListener("myProp", l1); + assertEquals(1, sup.getPropertyChangeListeners().length); + + sup.removePropertyChangeListener("myProp", l1); + assertEquals(0, sup.getPropertyChangeListeners().length); + assertEquals(0, sup.getPropertyChangeListeners("myProp").length); + + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + sup.addPropertyChangeListener(l3); + assertEquals(1, sup.getPropertyChangeListeners().length); + sup.removePropertyChangeListener("myProp", l2); + assertEquals(0, sup.getPropertyChangeListeners().length); + assertEquals(0, sup.getPropertyChangeListeners("myProp").length); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener, + * String) with a non-registered listener parameter. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_String_NonRegistered() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + sup.removePropertyChangeListener("myProp", + new MockPropertyChangeListener()); + assertEquals(0, sup.getPropertyChangeListeners().length); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener, + * String) with a null listener parameter. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_String_NullListener() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + + sup.removePropertyChangeListener("myProp", null); + assertEquals(0, sup.getPropertyChangeListeners().length); + sup.addPropertyChangeListener("myProp", null); + assertEquals(1, sup.getPropertyChangeListeners().length); + sup.removePropertyChangeListener("myProp", null); + assertEquals(0, sup.getPropertyChangeListeners().length); + } + + /* + * Test the method removePropertyChangeListener(PropertyeChangeListener, + * String) with a null property name parameter. + */ + public void testRemovePropertyChangeListener_PropertyChangeListener_String_NullProperty() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + // sup + // .removePropertyChangeListener(null, + // new MockPropertyChangeListener()); + sup.addPropertyChangeListener("myProp", + new MockPropertyChangeListener()); + try { + sup.removePropertyChangeListener(null, + new MockPropertyChangeListener()); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method getPropertyChangeListeners() when there is one listener + * for all properties and one for a named property. + */ + public void testGetPropertyChangeListener_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + PropertyChangeListener l4 = new MockPropertyChangeListener(); + + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp2", l4); + + assertEquals(3, sup.getPropertyChangeListeners().length); + } + + /* + * Test the method getPropertyChangeListeners() when there is no listeners. + */ + public void testGetPropertyChangeListener_Empty() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + assertEquals(0, sup.getPropertyChangeListeners().length); + } + + /* + * Test the method getPropertyChangeListeners(String) when there is one + * listener for all properties and one for the named property and a third + * for another named property. + */ + public void testGetPropertyChangeListener_String_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + PropertyChangeListener l4 = new MockPropertyChangeListener(); + + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp2", l4); + + assertEquals(1, sup.getPropertyChangeListeners("myProp").length); + assertSame(l2, sup.getPropertyChangeListeners("myProp")[0]); + sup.addPropertyChangeListener("myProp", + new MockPropertyChangeListener()); + assertEquals(2, sup.getPropertyChangeListeners("myProp").length); + } + + /* + * Test the method getPropertyChangeListeners(String) when there is no + * listener for the named property but there is one for another named + * property. + */ + public void testGetPropertyChangeListener_String_None() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp2", + l2); + PropertyChangeListener l4 = new MockPropertyChangeListener(); + + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp3", l4); + + assertEquals(0, sup.getPropertyChangeListeners("myProp").length); + } + + /* + * Test the method getPropertyChangeListeners(String) with a null parameter. + */ + public void testGetPropertyChangeListener_String_Null() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + // sup.getPropertyChangeListeners(null); + sup.addPropertyChangeListener("myProp", + new MockPropertyChangeListener()); + try { + sup.getPropertyChangeListeners(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method hasListeners(String) when there is one listener for all + * properties. + */ + public void testHasListener_AllRegistered() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + + assertFalse(sup.hasListeners("myProp")); + sup.addPropertyChangeListener(l1); + assertTrue(sup.hasListeners("myProp")); + sup.removePropertyChangeListener(l1); + assertFalse(sup.hasListeners("myProp")); + sup.addPropertyChangeListener(l3); + assertTrue(sup.hasListeners("myProp")); + } + + /* + * Test the method hasListeners(String) when there is one listener for the + * named property. + */ + public void testHasListener_PropertyRegistered() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeListener l1 = new MockPropertyChangeListener(); + PropertyChangeListener l2 = new MockPropertyChangeListener(); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp2", + l2); + + assertFalse(sup.hasListeners("myProp")); + sup.addPropertyChangeListener("myProP", l1); + assertFalse(sup.hasListeners("myProp")); + sup.addPropertyChangeListener("myProp", l2); + assertTrue(sup.hasListeners("myProp")); + sup.removePropertyChangeListener("myProp", l2); + assertFalse(sup.hasListeners("myProp")); + sup.addPropertyChangeListener("myProp", l3); + assertFalse(sup.hasListeners("myProp")); + } + + /* + * Test the method hasListeners(String) when there is no listeners. + */ + public void testHasListener_None() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + assertFalse(sup.hasListeners("myProp")); + } + + /* + * Test the method hasListeners(String) with a null parameter. + */ + public void testHasListener_Null() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + // assertFalse(sup.hasListeners(null)); + + PropertyChangeListener l1 = new MockPropertyChangeListener(); + sup.addPropertyChangeListener("myProP", l1); + try { + sup.hasListeners(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method firePropertyChange(String, Object, Object) with normal + * parameters, when there is no listeners. + */ + public void testFirePropertyChange_Object_NoListeners() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + sup.firePropertyChange("myProp", new Object(), new Object()); + } + + /* + * Test the method firePropertyChange(String, Object, Object) with normal + * parameters, when there is a listener for all properties and another for + * the named property. + */ + public void testFirePropertyChange_Object_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = new Object(); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", oldValue, newValue); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(String, Object, Object) with equal old + * and new non-null values, when there is a listener for all properties and + * another for the named property. + */ + public void testFirePropertyChange_Object_EqualValues() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = newValue; + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", oldValue, newValue); + l1.assertNotCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(String, Object, Object) with null old + * and new values, when there is a listener for all properties and another + * for the named property. + */ + public void testFirePropertyChange_Object_NullValues() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = null; + Object oldValue = null; + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", oldValue, newValue); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(String, Object, Object) with a + * non-null old value and a null new value, when there is a listener for all + * properties and another for the named property. + */ + public void testFirePropertyChange_Object_NullNewValue() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = null; + Object oldValue = new Object(); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", oldValue, newValue); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(String, Object, Object) with a null + * old value and a non-null new value, when there is a listener for all + * properties and another for the named property. + */ + public void testFirePropertyChange_Object_NullOldValue() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = null; + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", oldValue, newValue); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(String, Object, Object) with a null + * property name parameter. + */ + public void testFirePropertyChange_Object_NullProperty() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = new Object(); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(null, oldValue, newValue); + l1.assertCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(String, Object, Object) when a null + * listener has been registered. + */ + public void testFirePropertyChange_Object_NullListener() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = new Object(); + + sup.addPropertyChangeListener(null); + try { + sup.firePropertyChange("myProp", oldValue, newValue); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) with normal + * parameters, when there is a listener for all properties and another for + * the named property. + */ + public void testFirePropertyChange_PropertyChangeEvent_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = new Object(); + Object src2 = new Object(); + PropertyChangeEvent event = new PropertyChangeEvent(src2, "myProp", + oldValue, newValue); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(event); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) with equal old + * and new non-null values, when there is a listener for all properties and + * another for the named property. + */ + public void testFirePropertyChange_PropertyChangeEvent_EqualValues() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = newValue; + Object src2 = new Object(); + PropertyChangeEvent event = new PropertyChangeEvent(src2, "myProp", + oldValue, newValue); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(event); + l1.assertNotCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) with null old and + * new values, when there is a listener for all properties and another for + * the named property. + */ + public void testFirePropertyChange_PropertyChangeEvent_NullValues() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = null; + Object oldValue = null; + Object src2 = new Object(); + PropertyChangeEvent event = new PropertyChangeEvent(src2, "myProp", + oldValue, newValue); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(event); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) with a non-null + * old value and a null new value, when there is a listener for all + * properties and another for the named property. + */ + public void testFirePropertyChange_PropertyChangeEvent_NullNewValue() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = null; + Object oldValue = new Object(); + Object src2 = new Object(); + PropertyChangeEvent event = new PropertyChangeEvent(src2, "myProp", + oldValue, newValue); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(event); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) with a null old + * value and a non-null new value, when there is a listener for all + * properties and another for the named property. + */ + public void testFirePropertyChange_PropertyChangeEvent_NullOldValue() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = null; + Object src2 = new Object(); + PropertyChangeEvent event = new PropertyChangeEvent(src2, "myProp", + oldValue, newValue); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src2, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(event); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) with a null + * property name parameter. + */ + public void testFirePropertyChange_PropertyChangeEvent_NullProperty() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = new Object(); + Object src2 = new Object(); + PropertyChangeEvent event = new PropertyChangeEvent(src2, null, + oldValue, newValue); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src2, + null, oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src2, + null, oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src2, + null, oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(event); + l1.assertCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) when null. + */ + public void testFirePropertyChange_PropertyChangeEvent_Null() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + + try { + sup.firePropertyChange(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method firePropertyChange(PropertyChangeEvent) when a null + * listener has been registered. + */ + public void testFirePropertyChange_PropertyChangeEvent_NullListener() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = new Object(); + Object oldValue = new Object(); + Object src2 = new Object(); + PropertyChangeEvent event = new PropertyChangeEvent(src2, "myProp", + oldValue, newValue); + + sup.addPropertyChangeListener(null); + try { + sup.firePropertyChange(event); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method firePropertyChange(String, boolean, boolean) with normal + * parameters, when there is a listener for all properties and another for + * the named property. + */ + public void testFirePropertyChange_Boolean_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = Boolean.TRUE; + Object oldValue = Boolean.FALSE; + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", false, true); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(String, boolean, boolean) with equal + * old and new non-null values, when there is a listener for all properties + * and another for the named property. + */ + public void testFirePropertyChange_Boolean_EqualValues() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = Boolean.TRUE; + Object oldValue = newValue; + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", true, true); + l1.assertNotCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(String, boolean, boolean) with a null + * property name parameter. + */ + public void testFirePropertyChange_Boolean_NullProperty() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Object newValue = Boolean.TRUE; + Object oldValue = Boolean.FALSE; + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(null, false, true); + l1.assertCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(String, boolean, boolean) when a null + * listener has been registered. + */ + public void testFirePropertyChange_Boolean_NullListener() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + + sup.addPropertyChangeListener(null); + try { + sup.firePropertyChange("myProp", true, false); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test the method firePropertyChange(String, int, int) with normal + * parameters, when there is a listener for all properties and another for + * the named property. + */ + public void testFirePropertyChange_Int_Normal() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Integer newValue = new Integer(1); + Integer oldValue = new Integer(2); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", oldValue.intValue(), newValue + .intValue()); + l1.assertCalled(); + l2.assertCalled(); + l4.assertCalled(); + } + + /* + * Test the method firePropertyChange(String, int, int) with equal old and + * new non-null values, when there is a listener for all properties and + * another for the named property. + */ + public void testFirePropertyChange_Int_EqualValues() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Integer newValue = new Integer(1); + Integer oldValue = newValue; + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange("myProp", oldValue.intValue(), newValue + .intValue()); + l1.assertNotCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(String, int, int) with a null property + * name parameter. + */ + public void testFirePropertyChange_Int_NullProperty() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + Integer newValue = new Integer(1); + Integer oldValue = new Integer(2); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + MockPropertyChangeListener l2 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp", + l2); + MockPropertyChangeListener l4 = new MockPropertyChangeListener(src, + null, oldValue, newValue); + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener(l3); + sup.addPropertyChangeListener("myProp", l4); + + sup.firePropertyChange(null, oldValue.intValue(), newValue.intValue()); + l1.assertCalled(); + l2.assertNotCalled(); + l4.assertNotCalled(); + } + + /* + * Test the method firePropertyChange(String, int, int) when a null listener + * has been registered. + */ + public void testFirePropertyChange_Int_NullListener() { + Object src = new Object(); + PropertyChangeSupport sup = new PropertyChangeSupport(src); + + sup.addPropertyChangeListener(null); + try { + sup.firePropertyChange("myProp", 1, 2); + fail("Should throw NullPointerException!"); + } catch (NullPointerException ex) { + // expected + } + } + + /* + * Test serialization/deserialization. + */ + public void testSerialization() throws Exception { + Object src = "PropertyChangeSupportSerializationTest"; + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeSupport sup2 = new PropertyChangeSupport(src); + Integer newValue = new Integer(1); + Integer oldValue = new Integer(2); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener2 l2 = new MockPropertyChangeListener2(); + + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener("myProp", l1); + sup.addPropertyChangeListener("myProp", l2); + sup2.addPropertyChangeListener(l1); + sup2.addPropertyChangeListener("myProp", l1); + + PropertyChangeSupport deSup = (PropertyChangeSupport) SerializationTester + .getDeserilizedObject(sup); + assertEquals(sup2.getPropertyChangeListeners()[0], deSup + .getPropertyChangeListeners()[0]); + assertEquals(((PropertyChangeListenerProxy) sup2 + .getPropertyChangeListeners()[1]).getListener(), + ((PropertyChangeListenerProxy) deSup + .getPropertyChangeListeners()[1]).getListener()); + } + + /* + * Test serialization/deserialization compatibility + */ + public void testSerializationCompatibility() throws Exception { + Object src = "PropertyChangeSupportSerializationTest"; + PropertyChangeSupport sup = new PropertyChangeSupport(src); + PropertyChangeSupport sup2 = new PropertyChangeSupport(src); + Integer newValue = new Integer(1); + Integer oldValue = new Integer(2); + + MockPropertyChangeListener l1 = new MockPropertyChangeListener(src, + "myProp", oldValue, newValue); + MockPropertyChangeListener2 l2 = new MockPropertyChangeListener2(); + + sup.addPropertyChangeListener(l1); + sup.addPropertyChangeListener("myProp", l1); + sup.addPropertyChangeListener("myProp", l2); + sup2.addPropertyChangeListener(l1); + sup2.addPropertyChangeListener("myProp", l1); + + PropertyChangeSupport deSup = (PropertyChangeSupport) SerializationTester + .readObject(sup, + "tests/api/java/beans/PropertyChangeSupport.ser"); + assertEquals(sup2.getPropertyChangeListeners()[0], deSup + .getPropertyChangeListeners()[0]); + assertEquals(((PropertyChangeListenerProxy) sup2 + .getPropertyChangeListeners()[1]).getListener(), + ((PropertyChangeListenerProxy) deSup + .getPropertyChangeListeners()[1]).getListener()); + } + + /* + * Mock PropertyChangeListener. + */ + static class MockPropertyChangeListener implements PropertyChangeListener, + Serializable { + + private transient Object expSrc; + + private String expPropName; + + private transient Object expOldValue; + + private transient Object expNewValue; + + private transient PropertyChangeEvent event; + + private transient boolean called = false; + + public MockPropertyChangeListener() { + } + + public MockPropertyChangeListener(Object src, String propName, + Object oldValue, Object newValue) { + this.expSrc = src; + this.expPropName = propName; + this.expOldValue = oldValue; + this.expNewValue = newValue; + } + + public void setAll(Object src, String propName, Object oldValue, + Object newValue) { + this.expSrc = src; + this.expPropName = propName; + this.expOldValue = oldValue; + this.expNewValue = newValue; + } + + public void propertyChange(PropertyChangeEvent event) { + this.event = event; + } + + public void assertCalled() { + assertSame(expSrc, event.getSource()); + assertEquals(expPropName, event.getPropertyName()); + assertEquals(expOldValue, event.getOldValue()); + assertEquals(expNewValue, event.getNewValue()); + assertNull(event.getPropagationId()); + } + + public void assertNotCalled() { + assertNull(event); + assertFalse(called); + } + + public boolean equals(Object obj) { + if (obj instanceof MockPropertyChangeListener) { + MockPropertyChangeListener l = (MockPropertyChangeListener) obj; + return null == this.expPropName ? null == l.expPropName + : this.expPropName.equals(l.expPropName); + } + return false; + } + } + + /* + * Mock PropertyChangeListener which is not serializable. + */ + static class MockPropertyChangeListener2 implements PropertyChangeListener { + + public void propertyChange(PropertyChangeEvent event) { + } + } +} Added: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyDescriptorTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyDescriptorTest.java?rev=394923&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyDescriptorTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/tests/api/java/beans/PropertyDescriptorTest.java Tue Apr 18 05:11:09 2006 @@ -0,0 +1,977 @@ +/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests.api.java.beans; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.io.Serializable; +import java.lang.reflect.Method; + +import junit.framework.TestCase; + +/** + * Unit test for PropertyDescriptor. + */ +public class PropertyDescriptorTest extends TestCase { + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testEquals() throws IntrospectionException, SecurityException, + NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass); + + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + + pd.setName("different name"); + assertTrue(pd.equals(pd2)); + assertTrue(pd.equals(pd)); + assertTrue(pd2.equals(pd)); + assertFalse(pd.equals(null)); + } + + public void testEquals_ReadMethod() throws IntrospectionException, + SecurityException, NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, null); + + String propertyName2 = "PropertyThree"; + Method readMethod2 = beanClass.getMethod("get" + propertyName2, null); + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, + readMethod2, null); + + assertFalse(pd.equals(pd2)); + } + + public void testEquals_ReadMethod_Null() throws IntrospectionException, + SecurityException, NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method readMethod = null; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, null); + + String propertyName2 = "PropertyThree"; + Method readMethod2 = beanClass.getMethod("get" + propertyName2, null); + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, + readMethod2, null); + + assertFalse(pd.equals(pd2)); + } + + public void testEquals_ReadMethod_Null_Null() + throws IntrospectionException, SecurityException, + NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method readMethod = null; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, null); + + String propertyName2 = "PropertyThree"; + Method readMethod2 = null; + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, + readMethod2, null); + + assertTrue(pd.equals(pd2)); + } + + public void testEquals_WriteMethod() throws IntrospectionException, + SecurityException, NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, + writeMethod); + + String propertyName2 = "PropertyThree"; + Method writeMethod2 = beanClass.getMethod("set" + propertyName2, + new Class[] { String.class }); + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, null, + writeMethod2); + + assertFalse(pd.equals(pd2)); + } + + public void testEquals_WriteMethod_Null() throws IntrospectionException, + SecurityException, NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method writeMethod = null; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, + writeMethod); + + String propertyName2 = "PropertyThree"; + Method writeMethod2 = beanClass.getMethod("set" + propertyName2, + new Class[] { String.class }); + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, null, + writeMethod2); + + assertFalse(pd.equals(pd2)); + } + + public void testEquals_Bound() throws IntrospectionException, + SecurityException, NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + String propertyName2 = "PropertyThree"; + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, null, + null); + pd.setBound(true); + assertFalse(pd.equals(pd2)); + } + + public void testEquals_Contrained() throws IntrospectionException, + SecurityException, NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + String propertyName2 = "PropertyThree"; + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, null, + null); + pd.setConstrained(true); + assertFalse(pd.equals(pd2)); + } + + public void testEquals_PropertyEditorClass() throws IntrospectionException, + SecurityException, NoSuchMethodException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + String propertyName2 = "PropertyThree"; + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2, null, + null); + + pd.setPropertyEditorClass(String.class); + assertFalse(pd.equals(pd2)); + } + + public void testEquals_PropertyType() throws IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass); + + Class beanClass2 = PropertyDescriptorTest.MockBeanPropertyDesc.class; + PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, + beanClass2); + assertFalse(pd.equals(pd2)); + } + + /* + * Class under test for void PropertyDescriptor(String, Class) + */ + public void testPropertyDescriptorStringClass() + throws IntrospectionException { + String propertyName = "propertyOne"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass); + + String capitalName = propertyName.substring(0, 1).toUpperCase() + + propertyName.substring(1); + assertEquals(String.class, pd.getPropertyType()); + assertEquals("get" + capitalName, pd.getReadMethod().getName()); + assertEquals("set" + capitalName, pd.getWriteMethod().getName()); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testPropertyDescriptorStringClass_PropertyNameCapital() + throws IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass); + assertEquals(propertyName, pd.getName()); + } + + public void testPropertyDescriptorStringClass_PropertyNameEmpty() + throws IntrospectionException { + String propertyName = ""; + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + beanClass); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException exception) { + } + } + + public void testPropertyDescriptorStringClass_PropertyNameNull() { + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(null, beanClass); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException exception) { + } + } + + public void testPropertyDescriptorStringClass_BeanClassNull() + throws IntrospectionException { + String propertyName = "propertyOne"; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException exception) { + } + } + + public void testPropertyDescriptorStringClass_PropertyNameInvalid() { + String propertyName = "not a property name"; + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + beanClass); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException exception) { + } + } + + public void testPropertyDescriptorStringClass_ProtectedGetter() { + String propertyName = "protectedProp"; + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + beanClass); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException exception) { + } + } + + /* + * Class under test for void PropertyDescriptor(String, Class, String, + * String) + */ + public void testPropertyDescriptorStringClassStringString() + throws IntrospectionException { + String propertyName = "PropertyTwo"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass, + "get" + propertyName, "set" + propertyName); + + assertEquals(Integer.class, pd.getPropertyType()); + assertEquals("get" + propertyName, pd.getReadMethod().getName()); + assertEquals("set" + propertyName, pd.getWriteMethod().getName()); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testPropertyDescriptorStringClassStringString_PropertyNameNull() { + String propertyName = "PropertyTwo"; + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(null, beanClass, + "get" + propertyName, "set" + propertyName); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + public void testPropertyDescriptorStringClassStringString_BeanClassNull() + throws IntrospectionException { + String propertyName = "PropertyTwo"; + Class beanClass = null; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + beanClass, "get" + propertyName, "set" + propertyName); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + public void testPropertyDescriptorStringClassStringString_ReadMethodNull() + throws IntrospectionException { + String propertyName = "PropertyTwo"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass, + null, "set" + propertyName); + + assertEquals(Integer.class, pd.getPropertyType()); + assertNull(pd.getReadMethod()); + assertEquals("set" + propertyName, pd.getWriteMethod().getName()); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testPropertyDescriptorStringClassStringString_ReadMethodInvalid() + throws IntrospectionException { + String propertyName = "PropertyTwo"; + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + beanClass, "getXX", "set" + propertyName); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + public void testPropertyDescriptorStringClassStringString_WriteMethodNull() + throws IntrospectionException { + String propertyName = "PropertyTwo"; + Class beanClass = MockJavaBean.class; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass, + "get" + propertyName, null); + + assertEquals(Integer.class, pd.getPropertyType()); + assertEquals("get" + propertyName, pd.getReadMethod().getName()); + assertNull(pd.getWriteMethod()); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testPropertyDescriptorStringClassStringString_WriteMethodEmpty() + throws IntrospectionException { + String propertyName = "PropertyTwo"; + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + beanClass, "get" + propertyName, ""); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + + } + + public void testPropertyDescriptorStringClassStringString_WriteMethodInvalid() + throws IntrospectionException { + String propertyName = "PropertyTwo"; + Class beanClass = MockJavaBean.class; + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + beanClass, "get" + propertyName, "setXXX"); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + + } + + /* + * Class under test for void PropertyDescriptor(String, Method, Method) + */ + public void testPropertyDescriptorStringMethodMethod() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + + assertEquals(String.class, pd.getPropertyType()); + assertEquals("get" + propertyName, pd.getReadMethod().getName()); + assertEquals("set" + propertyName, pd.getWriteMethod().getName()); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testPropertyDescriptorStringMethodMethod_PropertyNameNull() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + try { + PropertyDescriptor pd = new PropertyDescriptor(null, readMethod, + writeMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + + } + + public void testPropertyDescriptorStringMethodMethod_ReadMethodNull() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method readMethod = null; + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + + assertEquals(String.class, pd.getPropertyType()); + assertNull(pd.getReadMethod()); + assertEquals("set" + propertyName, pd.getWriteMethod().getName()); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testPropertyDescriptorStringMethodMethod_ReadMethodInvalid() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + String anotherProp = "PropertyTwo"; + + Method readMethod = beanClass.getMethod("get" + anotherProp, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + + } + + public void testPropertyDescriptorStringMethodMethod_WriteMethodNull() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = null; + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + + assertEquals(String.class, pd.getPropertyType()); + assertEquals("get" + propertyName, pd.getReadMethod().getName()); + assertNull(pd.getWriteMethod()); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testPropertyDescriptorStringMethodMethod_WriteMethodInvalid() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + String propertyName = "PropertyOne"; + Class beanClass = MockJavaBean.class; + String anotherProp = "PropertyTwo"; + + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + anotherProp, + new Class[] { Integer.class }); + try { + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + + } + + /** + * @throws Exception + */ + public void testPropertyDescriptorStringClassMethodMethod_SubClass() + throws Exception { + try { + PropertyDescriptor pd = new PropertyDescriptor( + "prop1", SubMockJavaBean.class, null, "setPropertyOne"); //$NON-NLS-1$ //$NON-NLS-2$ + assertNull(pd.getReadMethod()); + assertEquals(pd.getWriteMethod().getName(), "setPropertyOne"); //$NON-NLS-1$ + + pd = new PropertyDescriptor( + "prop1", SubMockJavaBean.class, "getPropertyOne", "setPropertyOne"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + assertEquals(pd.getReadMethod().getName(), "getPropertyOne"); //$NON-NLS-1$ + assertEquals(pd.getWriteMethod().getName(), "setPropertyOne"); //$NON-NLS-1$ + + pd = new PropertyDescriptor( + "prop1", SubMockJavaBean.class, "getPropertyOne", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + assertEquals(pd.getReadMethod().getName(), "getPropertyOne"); //$NON-NLS-1$ + assertNull(pd.getWriteMethod()); //$NON-NLS-1$ + } catch (Exception e) { + fail("Throw " + e.getClass().getName()); //$NON-NLS-1$ + } + } + + public void testSetReadMethod() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + assertNull(pd.getReadMethod()); + pd.setReadMethod(readMethod); + assertSame(readMethod, pd.getReadMethod()); + } + + public void testSetReadMethod_Null() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, null); + + assertSame(readMethod, pd.getReadMethod()); + pd.setReadMethod(null); + assertNull(pd.getReadMethod()); + } + + /** + * Read method is incompatible with property name getPropertyTwo vs. + * PropertyOne (writeMethod=null) + */ + public void testSetReadMethod_Invalid() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + "PropertyTwo", null); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + assertNull(pd.getReadMethod()); + pd.setReadMethod(readMethod); + assertSame(readMethod, pd.getReadMethod()); + } + + /** + * String invalidGetMethod(String arg) + */ + public void testSetReadMethod_Invalid_withArg() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("invalidGetMethod", + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + assertNull(pd.getReadMethod()); + try { + pd.setReadMethod(readMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + /** + * String invalidGetMethod(String arg) + */ + public void testSetReadMethod_Invalid_returnVoid() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("invalidGetMethod", null); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + assertNull(pd.getReadMethod()); + try { + pd.setReadMethod(readMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + /** + * Read method is incompatible with write method getPropertyOn vs. + * setPropertyTow + */ + public void testSetReadMethod_ReadWriteIncompatible() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + "PropertyOne", null); + Method writeMethod = beanClass.getMethod("set" + "PropertyTwo", + new Class[] { Integer.class }); + + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, + writeMethod); + + assertNull(pd.getReadMethod()); + try { + pd.setReadMethod(readMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + /** + * normal input + */ + public void testSetWriteMethod() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + assertNull(pd.getWriteMethod()); + pd.setWriteMethod(writeMethod); + assertSame(writeMethod, pd.getWriteMethod()); + } + + /** + * setWriteMethod(null) + */ + public void testSetWriteMethod_Null() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, + writeMethod); + + assertSame(writeMethod, pd.getWriteMethod()); + pd.setWriteMethod(null); + assertNull(pd.getWriteMethod()); + } + + /** + * write method is incompatible with property name (read method is null) + */ + public void testSetWriteMethod_Invalid() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method writeMethod = beanClass.getMethod("set" + "PropertyTwo", + new Class[] { Integer.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + assertNull(pd.getWriteMethod()); + pd.setWriteMethod(writeMethod); + assertSame(writeMethod, pd.getWriteMethod()); + } + + /** + * write method without argument + */ + public void testSetWriteMethod_Invalid_NoArgs() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method writeMethod = beanClass.getMethod("get" + "PropertyTwo", null); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null); + + assertNull(pd.getWriteMethod()); + try { + pd.setWriteMethod(writeMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + /** + * write method is incompatible with read method + */ + public void testSetWriteMethod_WriteReadIncompatible() + throws SecurityException, NoSuchMethodException, + IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + "PropertyTwo", null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, null); + + assertNull(pd.getWriteMethod()); + try { + pd.setWriteMethod(writeMethod); + fail("Should throw IntrospectionException."); + } catch (IntrospectionException e) { + } + } + + public void testSetBound_true() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + pd.setBound(true); + + assertTrue(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testSetBound_false() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + pd.setBound(false); + + assertFalse(pd.isBound()); + assertFalse(pd.isConstrained()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testSetConstrained_true() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + pd.setConstrained(true); + + assertTrue(pd.isConstrained()); + assertFalse(pd.isBound()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + public void testSetConstrained_false() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + pd.setConstrained(false); + + assertFalse(pd.isConstrained()); + assertFalse(pd.isBound()); + assertNull(pd.getPropertyEditorClass()); + + assertEquals(propertyName, pd.getDisplayName()); + assertEquals(propertyName, pd.getName()); + assertEquals(propertyName, pd.getShortDescription()); + + assertNotNull(pd.attributeNames()); + + assertFalse(pd.isExpert()); + assertFalse(pd.isHidden()); + assertFalse(pd.isPreferred()); + } + + /** + * PropertyDescriptor does not ensure if the editor class is valid or not. + */ + public void testSetPropertyEditorClass() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + Class editorClass = "This is an invalid editor class".getClass(); + pd.setPropertyEditorClass(editorClass); + assertSame(editorClass, pd.getPropertyEditorClass()); + } + + public void testSetPropertyEditorClass_null() throws SecurityException, + NoSuchMethodException, IntrospectionException { + Class beanClass = MockJavaBean.class; + String propertyName = "PropertyOne"; + Method readMethod = beanClass.getMethod("get" + propertyName, null); + Method writeMethod = beanClass.getMethod("set" + propertyName, + new Class[] { String.class }); + PropertyDescriptor pd = new PropertyDescriptor(propertyName, + readMethod, writeMethod); + + pd.setPropertyEditorClass(null); + assertNull(pd.getPropertyEditorClass()); + } + + public void testConstructor_1() throws IntrospectionException { + PropertyDescriptor pd = new PropertyDescriptor("fox01", FakeFox01.class); + } + + static class FakeFox01 { + public String getFox01() { + return null; + } + + public void setFox01(String value) { + + } + + } + + class MockBeanPropertyDesc implements Serializable { + /** + * Comment for serialVersionUID + */ + private static final long serialVersionUID = 1L; + + Integer propertyOne; + + /** + * @return Returns the propertyOne. + */ + public Integer getPropertyOne() { + return propertyOne; + } + + /** + * @param propertyOne + * The propertyOne to set. + */ + public void setPropertyOne(Integer propertyOne) { + this.propertyOne = propertyOne; + } + } + + class SubMockJavaBean extends MockJavaBean { + /** + * Comment for serialVersionUID + */ + private static final long serialVersionUID = 7423254295680570566L; + // + } +}