KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
23 import java.util.TooManyListenersException JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import org.netbeans.api.nodes2looks.Nodes;
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.nodes.Node;
28 import org.openide.util.Enumerations;
29
30 /** Test additional features of ProxyLook.
31  *
32  * @author Petr Hrebejk
33  */

34 public class ProxyLookTest extends NbTestCase {
35
36     // Methods of testCase -----------------------------------------------------
37

38     public ProxyLookTest( String JavaDoc testName ) {
39         super(testName);
40     }
41
42     // Test methods ------------------------------------------------------------
43

44     public void testThrowNoException() {
45         
46         Look composite = Looks.composite( "TC", new Look[] {
47               new ExceptionLook( "sl1", null ),
48               new ExceptionLook( "sl2", null )
49         } );
50         
51         Look filter = Looks.filter( "TF", new ExceptionLook( "fd1", null ), ProxyLook.ALL_METHODS );
52         
53         try {
54             org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null );
55             org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( filter, "RO", null );
56         }
57         catch( Exception JavaDoc e ) {
58             fail( "No exception should be thrown" );
59         }
60         
61     }
62     
63     public void testThrowClassCastException() {
64         
65         Look composite = Looks.composite( "TC", new Look[] {
66               new ExceptionLook( "sl1", null ),
67               new ExceptionLook( "sl2", ClassCastException JavaDoc.class )
68         } );
69         
70         Look filter = Looks.filter( "TF", new ExceptionLook( "fd1", ClassCastException JavaDoc.class ), ProxyLook.ALL_METHODS );
71         
72         try {
73             org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null );
74             org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( filter, "RO", null );
75             fail( "ClassCastException should be thrown, but it was not" );
76         }
77         catch( Exception JavaDoc e ) {
78             assertTrue( "ClassCastException should be thrown. Was : " + e.getClass(), e instanceof ClassCastException JavaDoc );
79         }
80         
81     }
82     
83     public void testThrowIllegalArgumentException() {
84         Look composite = Looks.composite( "TC", new Look[] {
85               new ExceptionLook( "sl1", null ),
86               new ExceptionLook( "sl2", IllegalArgumentException JavaDoc.class )
87         } );
88         
89         Look filter = Looks.filter( "TF", new ExceptionLook( "fd1", IllegalArgumentException JavaDoc.class ), ProxyLook.ALL_METHODS );
90         
91         try {
92             org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null );
93             org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( filter, "RO", null );
94             fail( "IllegalArgumentException should be thrown, but it was not" );
95         }
96         catch( Exception JavaDoc e ) {
97             assertTrue( "IllegalArgumentException should be thrown. Was : " + e.getClass(), e instanceof IllegalArgumentException JavaDoc );
98         }
99         
100     }
101
102     public void testDetachWhenExceptionWasThrown() {
103         
104         ExceptionLook sl1 = new ExceptionLook( "sl1", null );
105         ExceptionLook sl2 = new ExceptionLook( "sl2", null );
106         ExceptionLook sl3 = new ExceptionLook( "sl2", IllegalArgumentException JavaDoc.class );
107         ExceptionLook sl4 = new ExceptionLook( "sl2", ClassCastException JavaDoc.class );
108         
109         Look composite = Looks.composite( "TC", new Look[] { sl1, sl2, sl3, sl4 } );
110         
111         try {
112             org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null );
113         }
114         catch ( IllegalArgumentException JavaDoc e ) {
115             assertEquals( "Sl1 attachTo", 1, sl1.getAttachToCount() );
116             assertEquals( "Sl1 detachFrom", 1, sl1.getDetachFromCount() );
117             assertEquals( "Sl2 attachTo", 1, sl2.getAttachToCount() );
118             assertEquals( "Sl2 detachFrom", 1, sl2.getDetachFromCount() );
119             assertEquals( "Sl3 attachTo", 0, sl3.getAttachToCount() );
120             assertEquals( "Sl3 detachFrom", 0, sl3.getDetachFromCount() );
121             assertEquals( "Sl4 attachTo", 0, sl4.getAttachToCount() );
122             assertEquals( "Sl4 detachFrom", 0, sl4.getDetachFromCount() );
123             return;
124         }
125         
126         fail( "Exception not thrown" );
127     }
128
129     public void testAttachDetachAfterSeletorChange( ) {
130         
131         AttachDetachProvider provider = new AttachDetachProvider();
132         LookSelector selector = Selectors.selector( provider );
133         Look proxy = new AttachDetachProxyLook( "TEST_AD", selector );
134         
135         Node n = Nodes.node( "KAREL", null, Selectors.singleton( proxy ) );
136         
137         assertEquals( "AD_LOOK_1 attachTo", 1, provider.AD_LOOK[0].getAttachCount() );
138         assertEquals( "AD_LOOK_1 detachFrom", 0, provider.AD_LOOK[0].getDetachCount() );
139         assertEquals( "AD_LOOK_2 attachTo", 0, provider.AD_LOOK[1].getAttachCount() );
140         assertEquals( "AD_LOOK_2 detachFrom", 0, provider.AD_LOOK[1].getDetachCount() );
141         
142         provider.switchLook();
143         
144         assertEquals( "AD_LOOK_1 attachTo", 0, provider.AD_LOOK[0].getAttachCount() );
145         assertEquals( "AD_LOOK_1 detachFrom", 1, provider.AD_LOOK[0].getDetachCount() );
146         assertEquals( "AD_LOOK_2 attachTo", 1, provider.AD_LOOK[1].getAttachCount() );
147         assertEquals( "AD_LOOK_2 detachFrom", 0, provider.AD_LOOK[1].getDetachCount() );
148     }
149     
150     // Innerclasses ------------------------------------------------------------
151

152
153     public static class ExceptionLook extends Look {
154         
155         private int attachTo;
156         private int detachFrom;
157         
158         private Class JavaDoc exClass;
159                 
160         public ExceptionLook( String JavaDoc name, Class JavaDoc exClass ) {
161             super( name );
162             
163             if ( exClass != null && !( RuntimeException JavaDoc.class.isAssignableFrom( exClass ) ) ) {
164                 fail( "Bad usage of the test " + exClass + " is not runtimeException" );
165             }
166             
167             this.exClass = exClass;
168         }
169         
170         public void attachTo( Object JavaDoc representedObject ) {
171             if ( exClass != null) {
172                 try {
173                     System.err.println("exClass=" + exClass);//XXX
174
throw (RuntimeException JavaDoc)exClass.newInstance();
175                 }
176                 catch ( InstantiationException JavaDoc e ) {
177                     fail( "Bad usage of the test " + e );
178                 }
179                 catch ( IllegalAccessException JavaDoc e ) {
180                     fail( "Bad usage of the test " + e );
181                 }
182             }
183             
184             attachTo++;
185         }
186         
187         public void detachFrom( Object JavaDoc representedObject ) {
188             detachFrom++;
189         }
190         
191         public int getAttachToCount() {
192             return attachTo;
193         }
194         
195         public int getDetachFromCount() {
196             return detachFrom;
197         }
198         
199     }
200
201     private static class AttachDetachProvider implements ChangeableLookProvider {
202         
203         private final AttachDetachLook AD_LOOK[] = new AttachDetachLook[] {
204             new AttachDetachLook( "AD_LOOK_1" ),
205             new AttachDetachLook( "AD_LOOK_2" )
206         };
207         
208         private int current = 0;
209         
210         private ChangeListener JavaDoc listener;
211         
212         public Enumeration JavaDoc getLooksForObject( Object JavaDoc representedObject ) {
213             return Enumerations.singleton(AD_LOOK[current]);
214         }
215                 
216         public void switchLook() {
217             current = current == 0 ? 1 : 0;
218             
219             if ( listener != null ) {
220                 listener.stateChanged( null );
221             }
222         }
223         
224         public synchronized void addChangeListener(ChangeListener JavaDoc listener) throws TooManyListenersException JavaDoc {
225             if ( this.listener != null ) {
226                 throw new TooManyListenersException JavaDoc();
227             }
228             else {
229                 this.listener = listener;
230             }
231         }
232         
233         public Object JavaDoc getKeyForObject(Object JavaDoc representedObject) {
234             return String JavaDoc.class;
235         }
236         
237         public Enumeration JavaDoc getLooksForKey(Object JavaDoc key) {
238             return Enumerations.singleton(AD_LOOK[current]);
239         }
240         
241     }
242     
243     private static class AttachDetachProxyLook extends ProxyLook {
244         
245         public AttachDetachProxyLook( String JavaDoc name, LookSelector selector ) {
246             super( name, selector );
247         }
248         
249         
250     }
251     
252     private static class AttachDetachLook extends Look {
253         
254         private int attachCount;
255         private int detachCount;
256         
257         public AttachDetachLook( String JavaDoc name ) {
258             super( name );
259         }
260
261         protected void attachTo(Object JavaDoc representedObject) {
262             attachCount ++;
263         }
264         
265
266         
267         protected void detachFrom(Object JavaDoc representedObject) {
268             detachCount ++;
269         }
270         
271         
272         public int getAttachCount() {
273             int c = attachCount;
274             attachCount = 0;
275             return c;
276         }
277     
278         public int getDetachCount() {
279             int c = detachCount;
280             detachCount = 0;
281             return c;
282         }
283         
284     }
285     
286     
287 }
288
Popular Tags