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
18package org.apache.harmony.beans.tests.java.beans;
19
20import java.beans.PropertyChangeEvent;
21import java.beans.PropertyChangeListener;
22import java.beans.PropertyChangeListenerProxy;
23
24import junit.framework.TestCase;
25
26/**
27 * Test for PropertyChangeListenerProxy
28 */
29public class PropertyChangeListenerProxyTest extends TestCase {
30
31    PropertyChangeListenerProxy proxy;
32
33    PropertyChangeListener listener = new MockPropertyChangeListener();
34
35    String name = "mock";
36
37    static PropertyChangeEvent event = null;
38
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42        proxy = new PropertyChangeListenerProxy(name, listener);
43    }
44
45    public void testPropertyChangeListenerProxy() {
46        proxy = new PropertyChangeListenerProxy(null, listener);
47        assertSame(listener, proxy.getListener());
48        assertNull(proxy.getPropertyName());
49        PropertyChangeEvent newevent = new PropertyChangeEvent(new Object(),
50                "name", new Object(), new Object());
51        proxy.propertyChange(newevent);
52        assertSame(newevent, event);
53        proxy = new PropertyChangeListenerProxy(name, null);
54        assertSame(name, proxy.getPropertyName());
55        assertNull(proxy.getListener());
56        try {
57            proxy.propertyChange(new PropertyChangeEvent(new Object(), "name",
58                    new Object(), new Object()));
59            fail("NullPointerException expected");
60        } catch (NullPointerException e) {
61        }
62
63        proxy = new PropertyChangeListenerProxy(name, listener);
64        assertSame(listener, proxy.getListener());
65        assertSame(name, proxy.getPropertyName());
66        newevent = new PropertyChangeEvent(new Object(), "name", new Object(),
67                new Object());
68        assertSame(name, proxy.getPropertyName());
69        proxy.propertyChange(newevent);
70        assertSame(newevent, event);
71    }
72
73    public void testPropertyChange() {
74        proxy.propertyChange(null);
75        assertNull(event);
76    }
77
78    /**
79     * Regression for HARMONY-407
80     */
81    public void testPropertyChange_PropertyChangeEvent() {
82        PropertyChangeListenerProxy proxy = new PropertyChangeListenerProxy(
83                "harmony", null);
84        try {
85            proxy.propertyChange(null);
86            fail("NullPointerException expected");
87        } catch (NullPointerException e) {
88        }
89    }
90
91    public static class MockPropertyChangeListener implements
92            PropertyChangeListener {
93        public void propertyChange(PropertyChangeEvent newevent) {
94            event = newevent;
95        }
96    }
97
98}
99