KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > looks > EventDeliveryTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.looks;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.lang.reflect.*;
26 import java.util.*;
27 import org.netbeans.api.nodes2looks.Nodes;
28 import org.netbeans.api.nodes2looks.TestUtil;
29 import org.netbeans.spi.looks.*;
30
31 import org.netbeans.junit.*;
32 import org.netbeans.modules.looks.LookListener;
33 import org.openide.nodes.Node;
34 import org.openide.nodes.NodeListener;
35
36 /** Tests the event delivery from looks.
37  *
38  * @author Petr Hrebejk
39  */

40 public class EventDeliveryTest extends NbTestCase {
41     
42     // Methods of testCase -----------------------------------------------------
43

44     public EventDeliveryTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     public static void main( String JavaDoc[] args ) {
49         junit.textui.TestRunner.run(suite());
50     }
51
52     public static NbTest suite() {
53         NbTestSuite suite = new NbTestSuite(EventDeliveryTest.class);
54         return suite;
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59         TestUtil.setUpRegistryToDefault();
60     }
61
62     protected void tearDown() throws Exception JavaDoc {
63         super.tearDown();
64     }
65     
66     // Test methods ------------------------------------------------------------
67

68     /** Tests that the event delivery works well for multiple views
69      */

70     public void testMultipleViewsDetach() {
71         
72         BadGuyLook look = new BadGuyLook( "BAD_GUY_LOOK" );
73         BadGuy ro = new BadGuy( "First name", new HashSet() );
74         
75         doTest( look, ro );
76         
77     }
78     
79     
80     /** Tests that the event delivery works well for multiple views
81      */

82     
83     public void testMultipleViewsEventsCount() {
84         
85         BadGuyLook look = new BadGuyLook( "BAD_GUY_LOOK" );
86         BadGuy ro = new BadGuy( "First name", new ArrayList() );
87         
88         doTest( look, ro );
89         
90     }
91     
92     
93     private void doTest( BadGuyLook look, BadGuy ro ) {
94         // These nodes represent the two views
95
Node n1 = Nodes.node( ro, look );
96         Node n2 = Nodes.node( ro, look );
97         
98         BadGuyNodeListener bgnl1 = new BadGuyNodeListener();
99         BadGuyNodeListener bgnl2 = new BadGuyNodeListener();
100         
101         n1.addNodeListener( bgnl1 );
102         n2.addNodeListener( bgnl2 );
103         
104         ro.setName( "Second name" );
105         
106         assertEquals( "There should be one prop change", 1, bgnl1.names.size() );
107         assertEquals( "There should be one prop change", 1, bgnl2.names.size() );
108         
109         assertEquals( "Attach should be called once", 1, look.attachTo );
110         
111         WeakReference JavaDoc ref = new WeakReference JavaDoc ( n1 );
112         n1 = null; // Lets forget the node
113
assertGC ("The node must disapear", ref);
114         
115         ro.setName( "Third name" );
116         
117         assertEquals( "There should be one prop change", 1, bgnl1.names.size() );
118         assertEquals( "There should be one prop change", 2, bgnl2.names.size() );
119     }
120     
121     
122     /** Tests that detached look won't fire any events
123      */

124     public void testEventAfterDetach() {
125         
126         BadGuyLook look = new BadGuyLook( "BAD_GUY_LOOK" );
127         BadGuy ro = new BadGuy( "First name", new ArrayList() );
128         BadGuyLookListener ll = new BadGuyLookListener();
129         
130         org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( look, ro, ll );
131         ro.setName( "Second name" );
132         
133         assertEquals( "There should be one name change", 1, ll.names.size() );
134         
135         org.netbeans.modules.looks.Accessor.DEFAULT.removeLookListener( look, ro, ll );
136         
137         ro.setName( "Third name" );
138         
139         assertEquals( "There should be still only one name change", 1, ll.names.size() );
140     }
141     
142
143     // Innerclasses ------------------------------------------------------------
144

145     /** This is a bad represented object allows for using different
146      * collections for listener list.
147      */

148     
149     public static class BadGuy {
150         
151         private Collection listeners;
152         
153         private String JavaDoc name;
154         
155         public BadGuy( String JavaDoc name, Collection listeners ) {
156             this.name = name;
157             this.listeners = listeners;
158         }
159         
160         public String JavaDoc getName() {
161             return name;
162         }
163         
164         public void setName( String JavaDoc name ) {
165             String JavaDoc oldName = this.name;
166             this.name = name;
167             
168             for( Iterator it = listeners.iterator(); it.hasNext(); ) {
169                 ((PropertyChangeListener JavaDoc)it.next()).propertyChange( new PropertyChangeEvent JavaDoc( this, "NAME", oldName, name ) );
170             }
171         }
172         
173         public void addPropertyChangeListener( PropertyChangeListener JavaDoc listener ) {
174             listeners.add( listener );
175         }
176         
177         public void removePropertyChangeListener( PropertyChangeListener JavaDoc listener ) {
178             listeners.remove( listener );
179         }
180         
181     }
182     
183     
184     public static class BadGuyLook extends Look implements PropertyChangeListener JavaDoc {
185         
186         private int attachTo;
187         private int detachFrom;
188         
189         private Class JavaDoc exClass;
190                 
191         public BadGuyLook( String JavaDoc name ) {
192             super( name );
193         }
194         
195         public void attachTo( Object JavaDoc representedObject ) {
196             ((BadGuy)representedObject).addPropertyChangeListener( this );
197             attachTo++;
198         }
199         
200         public void detachFrom( Object JavaDoc representedObject ) {
201             ((BadGuy)representedObject).removePropertyChangeListener( this );
202             detachFrom++;
203         }
204         
205         public int getAttachToCount() {
206             return attachTo;
207         }
208         
209         public int getDetachFromCount() {
210             return detachFrom;
211         }
212         
213         public void propertyChange( PropertyChangeEvent JavaDoc evt ) {
214             fireChange( evt.getSource(), Look.GET_NAME );
215         }
216         
217     }
218
219     public static class BadGuyNodeListener implements NodeListener {
220         
221         private List names = new ArrayList();
222         
223         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
224             names.add( evt.getNewValue() );
225         }
226         
227         public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) {
228         }
229         
230         public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) {
231         }
232         
233         public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) {
234         }
235         
236         public void nodeDestroyed(org.openide.nodes.NodeEvent ev) {
237         }
238         
239     }
240     
241     public static class BadGuyLookListener implements LookListener {
242         
243         private List names = new ArrayList();
244         
245         public void change(org.netbeans.modules.looks.LookEvent evt) {
246             long mask = evt.getMask();
247             
248             if ( ( mask & Look.GET_NAME ) > 0 ) {
249                 names.add( new Object JavaDoc() );
250             }
251         }
252         
253         
254         public void propertyChange(org.netbeans.modules.looks.LookEvent evt) {
255         }
256                 
257     }
258
259 }
260
Popular Tags