Home | History | Annotate | Download | only in beans
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.beans.tests.java.beans;
     19 
     20 import java.beans.PropertyChangeEvent;
     21 import java.io.Serializable;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import org.apache.harmony.testframework.serialization.SerializationTest;
     26 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
     27 
     28 /**
     29  * Test class java.beans.PropertyChangeEvent.
     30  */
     31 public class PropertyChangeEventTest extends TestCase {
     32     /*
     33      * Test the constructor with normal parameters.
     34      */
     35     public void testConstructor_Normal() {
     36         Object src = new Object();
     37         Object oldValue = new Object();
     38         Object newValue = new Object();
     39 
     40         PropertyChangeEvent event = new PropertyChangeEvent(src, "myPropName",
     41                 oldValue, newValue);
     42         assertSame(src, event.getSource());
     43         assertEquals("myPropName", event.getPropertyName());
     44         assertSame(oldValue, event.getOldValue());
     45         assertSame(newValue, event.getNewValue());
     46         assertNull(event.getPropagationId());
     47     }
     48 
     49     /*
     50      * Test the constructor with null parameters except the source parameter.
     51      */
     52     public void testConstructor_Null() {
     53         Object src = new Object();
     54         PropertyChangeEvent event = new PropertyChangeEvent(src, null, null,
     55                 null);
     56         assertSame(src, event.getSource());
     57         assertNull(event.getPropertyName());
     58         assertSame(null, event.getOldValue());
     59         assertSame(null, event.getNewValue());
     60         assertNull(event.getPropagationId());
     61     }
     62 
     63     /*
     64      * Test the constructor with null properties but non-null old and new
     65      * values.
     66      */
     67     public void testConstructor_NullProperty() {
     68         Object src = new Object();
     69         Object oldValue = new Object();
     70         Object newValue = new Object();
     71         PropertyChangeEvent event = new PropertyChangeEvent(src, null,
     72                 oldValue, newValue);
     73         assertSame(src, event.getSource());
     74         assertNull(event.getPropertyName());
     75         assertSame(oldValue, event.getOldValue());
     76         assertSame(newValue, event.getNewValue());
     77         assertNull(event.getPropagationId());
     78     }
     79 
     80     /*
     81      * Test the constructor with null source parameter.
     82      */
     83     public void testConstructor_NullSrc() {
     84         try {
     85             new PropertyChangeEvent(null, "prop", new Object(), new Object());
     86             fail("Should throw IllegalArgumentException!");
     87         } catch (IllegalArgumentException ex) {
     88             // expected
     89         }
     90     }
     91 
     92     /*
     93      * Test the method setPropagationId() with a normal value.
     94      */
     95     public void testSetPropagationId_Normal() {
     96         Object src = new Object();
     97         Object oldValue = new Object();
     98         Object newValue = new Object();
     99 
    100         PropertyChangeEvent event = new PropertyChangeEvent(src, "myPropName",
    101                 oldValue, newValue);
    102         assertNull(event.getPropagationId());
    103 
    104         Object pid = new Object();
    105         event.setPropagationId(pid);
    106 
    107         assertSame(src, event.getSource());
    108         assertEquals("myPropName", event.getPropertyName());
    109         assertSame(oldValue, event.getOldValue());
    110         assertSame(newValue, event.getNewValue());
    111         assertSame(pid, event.getPropagationId());
    112     }
    113 
    114     /*
    115      * Test the method setPropagationId() with a null value.
    116      */
    117     public void testSetPropagationId_Null() {
    118         Object src = new Object();
    119         Object oldValue = new Object();
    120         Object newValue = new Object();
    121 
    122         PropertyChangeEvent event = new PropertyChangeEvent(src, "myPropName",
    123                 oldValue, newValue);
    124         assertNull(event.getPropagationId());
    125 
    126         // set null when already null
    127         event.setPropagationId(null);
    128         assertNull(event.getPropagationId());
    129 
    130         // set a non-null value
    131         Object pid = new Object();
    132         event.setPropagationId(pid);
    133         assertSame(src, event.getSource());
    134         assertEquals("myPropName", event.getPropertyName());
    135         assertSame(oldValue, event.getOldValue());
    136         assertSame(newValue, event.getNewValue());
    137         assertSame(pid, event.getPropagationId());
    138 
    139         // reset to null
    140         event.setPropagationId(null);
    141         assertNull(event.getPropagationId());
    142     }
    143 
    144     // comparator for PropertyChangeEvent objects
    145     public static final SerializableAssert comparator = new SerializableAssert() {
    146         public void assertDeserialized(Serializable initial,
    147                 Serializable deserialized) {
    148 
    149             PropertyChangeEvent initEv = (PropertyChangeEvent) initial;
    150             PropertyChangeEvent desrEv = (PropertyChangeEvent) deserialized;
    151 
    152             assertEquals("NewValue", initEv.getNewValue(), desrEv.getNewValue());
    153             assertEquals("OldValue", initEv.getOldValue(), desrEv.getOldValue());
    154             assertEquals("PropagationId", initEv.getPropagationId(), desrEv
    155                     .getPropagationId());
    156             assertEquals("PropertyName", initEv.getPropertyName(), desrEv
    157                     .getPropertyName());
    158         }
    159     };
    160 
    161     /**
    162      * @tests serialization/deserialization.
    163      */
    164     public void testSerializationSelf() throws Exception {
    165 
    166         SerializationTest.verifySelf(new PropertyChangeEvent(new Object(),
    167                 "myPropName", "oldValue", "newValue"), comparator);
    168     }
    169 
    170     /**
    171      * @tests serialization/deserialization compatibility with RI.
    172      */
    173     public void testSerializationCompatibility() throws Exception {
    174 
    175         SerializationTest
    176                 .verifyGolden(this, new PropertyChangeEvent(new Object(),
    177                         "myPropName", "oldValue", "newValue"), comparator);
    178     }
    179 }
    180