KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.ConcurrentModificationException JavaDoc;
24 import org.netbeans.junit.*;
25 import org.netbeans.modules.looks.LookEvent;
26
27 import org.netbeans.modules.looks.LookListener;
28
29 /** Tests modifications in the Look listeners cache
30  */

31 public class LookListenersTest extends NbTestCase {
32
33     // Methods of testCase -----------------------------------------------------
34

35     public LookListenersTest(java.lang.String JavaDoc testName) {
36         super(testName);
37     }
38
39     public static void main(java.lang.String JavaDoc[] args) {
40         junit.textui.TestRunner.run(suite());
41     }
42     
43     public static NbTest suite() {
44         NbTestSuite suite = new NbTestSuite(LookListenersTest.class);
45         return suite;
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50     }
51     
52     protected void tearDown() throws Exception JavaDoc {
53         super.tearDown();
54     }
55     
56     
57     // Test methods ------------------------------------------------------------
58

59     public void testAddObject() {
60     
61         // Crate test looks
62
TestFiringLook look = new TestFiringLook();
63         TestLookListener listener = new TestLookListener( look );
64         
65         reset();
66         
67         // Add some listeners
68
look.addLookListener( createObject(), listener );
69         look.addLookListener( createObject(), listener );
70         
71         try {
72             // Fire change on all objects which will add new listener
73
look.fire( null, ADD_OBJECT );
74         }
75         catch ( ConcurrentModificationException JavaDoc e ) {
76             fail( "CME thrown " + e );
77         }
78                 
79     }
80     
81     public void testRemoveObject() {
82     
83         // Crate test looks
84
TestFiringLook look = new TestFiringLook();
85         TestLookListener listener = new TestLookListener( look );
86         
87         reset();
88         
89       
90         // Add some listeners
91
look.addLookListener( createObject(), listener );
92         look.addLookListener( createObject(), listener );
93         look.addLookListener( createObject(), listener );
94         
95         try {
96             // Fire change on all objects which will add new listener
97
look.fire( null, REMOVE_OBJECT );
98         }
99         catch ( ConcurrentModificationException JavaDoc e ) {
100             fail( "CME thrown " + e );
101         }
102                 
103     }
104     
105     public void testAddListener() {
106     
107         // Crate test looks
108
TestFiringLook look = new TestFiringLook();
109         
110         reset();
111         createObject();
112         // Add some listeners
113
look.addLookListener( getObject( counter ), new TestLookListener( look ) );
114         look.addLookListener( getObject( counter ), new TestLookListener( look ) );
115         
116         try {
117             // Fire change on all objects which will add new listener
118
look.fire( getObject( counter ), ADD_LISTENER );
119         }
120         catch ( ConcurrentModificationException JavaDoc e ) {
121             fail( "CME thrown " + e );
122         }
123                 
124     }
125     
126     public void testRemoveListener() {
127     
128         // Crate test looks
129
TestFiringLook look = new TestFiringLook();
130         
131         reset();
132         createObject();
133         // Add some listeners
134
look.addLookListener( getObject( counter ), new TestLookListener( look ) );
135         look.addLookListener( getObject( counter ), new TestLookListener( look ) );
136         
137         try {
138             // Fire change on all objects which will add new listener
139
look.fire( getObject( counter ), REMOVE_LISTENER );
140         }
141         catch ( ConcurrentModificationException JavaDoc e ) {
142             fail( "CME thrown " + e );
143         }
144                 
145     }
146     
147     // Private helper classes --------------------------------------------------
148

149     private static int counter = -1;
150     private static ArrayList JavaDoc objects = new ArrayList JavaDoc();
151     
152     private static void reset() {
153         counter = -1;
154         objects = new ArrayList JavaDoc();
155         TestLookListener.listeners = new ArrayList JavaDoc();
156     }
157     
158     private static Integer JavaDoc createObject() {
159         objects.add( new Integer JavaDoc( counter++ ) );
160         return (Integer JavaDoc)objects.get( counter );
161     }
162     
163     private static Integer JavaDoc getObject( int index ) {
164         return (Integer JavaDoc)objects.get( index );
165     }
166     
167     // Abusing event mask for controlling Look's listeners cache
168
private static final long ADD_OBJECT = Look.GET_NAME;
169     private static final long REMOVE_OBJECT = Look.GET_DISPLAY_NAME;
170     private static final long ADD_LISTENER = Look.GET_ICON;
171     private static final long REMOVE_LISTENER = Look.GET_OPENED_ICON;
172     
173     private static class TestFiringLook extends Look {
174         
175         TestFiringLook() {
176             super( "TEST_FIRING_LOOK" );
177         }
178         
179         public void fire( Object JavaDoc object, long mask ) {
180             fireChange( object, mask );
181         }
182         
183     }
184     
185     private static class TestLookListener implements LookListener {
186
187         private static ArrayList JavaDoc listeners = new ArrayList JavaDoc();
188         
189         Look look;
190         
191         TestLookListener( Look look ) {
192             this.look = look;
193             listeners.add( this );
194         }
195                 
196         public void change( LookEvent evt ) {
197             if ( evt.getMask() == ADD_OBJECT ) {
198                 look.addLookListener( createObject(), this );
199             }
200             else if ( evt.getMask() == REMOVE_OBJECT ) {
201                 look.removeLookListener( getObject( counter ), this );
202             }
203             else if ( evt.getMask() == ADD_LISTENER ) {
204                 look.addLookListener( getObject( counter ), this );
205             }
206             else if ( evt.getMask() == REMOVE_LISTENER ) {
207                 look.removeLookListener( getObject( counter ), (LookListener)listeners.get( counter ) );
208             }
209         }
210         
211         public void propertyChange( LookEvent evt ) {
212                 
213         }
214         
215     }
216     
217     
218 }
219
Popular Tags