KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.WeakReference JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import org.netbeans.junit.*;
27 import junit.framework.*;
28
29 import org.netbeans.spi.looks.ProxyLook;
30 import org.netbeans.spi.looks.Look;
31 import org.netbeans.spi.looks.Looks;
32 import org.netbeans.spi.looks.LookSelector;
33 import org.netbeans.api.nodes2looks.Nodes;
34
35 import org.openide.cookies.InstanceCookie;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileSystem;
38 import org.openide.filesystems.XMLFileSystem;
39 import org.openide.util.Lookup;
40
41 public class LooksTest extends NbTestCase {
42
43     private FileSystem fs;
44     private Look delegate;
45     private long mask = Look.ALL_METHODS;
46     private javax.swing.JTextField JavaDoc bean;
47
48     public LooksTest(java.lang.String JavaDoc testName) {
49         super(testName);
50     }
51
52     public static void main(java.lang.String JavaDoc[] args) {
53         junit.textui.TestRunner.run(suite());
54     }
55
56     public static Test suite() {
57         TestSuite suite = new NbTestSuite(LooksTest.class);
58
59         return suite;
60     }
61
62     protected void setUp() throws Exception JavaDoc {
63         super.setUp();
64         URL JavaDoc u = getClass ().getResource ("LooksTest.xml");
65
66         fs = new XMLFileSystem (u);
67         
68         org.netbeans.modules.looks.RegistryBridge.setDefault( fs.getRoot() );
69     }
70
71     protected void tearDown() throws Exception JavaDoc {
72         super.tearDown();
73     }
74
75
76     public void testNODES () throws Exception JavaDoc {
77         // XXX these should be using Registry API instead!
78
checkInstance (Nodes.nodeLook(), "Looks/NODES");
79     }
80
81     public void testBEANS () throws Exception JavaDoc {
82         checkInstance (Looks.bean(), "Looks/BEANS");
83     }
84
85     public void testFILTER_DEFAULT () throws Exception JavaDoc {
86         doFILTER_DEFAULT ("FILTER_DEFAULT");
87     }
88
89     public void testFILTER_DEFAULT2 () throws Exception JavaDoc {
90         doFILTER_DEFAULT ("FILTER_DEFAULT2");
91     }
92
93     private void doFILTER_DEFAULT (String JavaDoc name) throws Exception JavaDoc {
94
95         //System.out.println("testFILTER_DEFAULT");
96
// set delegate look and representive object
97
initFilterLookTest ();
98
99         // reference filter look
100
Look filterRef = Looks.filter ( "TestFilter", delegate, mask);
101         filterRef.attachTo (bean);
102
103         // get look declared in layer
104
Look filter = (Look)checkIfExists ("Looks/" + name);
105         filter.attachTo (bean);
106
107         assertTrue (bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY)));
108         assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY));
109     }
110
111     public void testFILTER_ALL_METHODS () throws Exception JavaDoc {
112         doFILTER_ALL_METHODS ("FILTER_ALL_METHODS");
113     }
114
115     public void testFILTER_ALL_METHODS2 () throws Exception JavaDoc {
116         doFILTER_ALL_METHODS ("FILTER_ALL_METHODS2");
117     }
118
119     private void doFILTER_ALL_METHODS (String JavaDoc name) throws Exception JavaDoc {
120
121         //System.out.println("testFILTER_ALL_METHODS");
122
// set delegate look and representive object
123
initFilterLookTest ();
124
125         // reference filter look
126
mask = Look.ALL_METHODS;
127         Look filterRef = Looks.filter ( "TestFilter", delegate, mask);
128         filterRef.attachTo (bean);
129
130         // get look declared in layer
131
Look filter = (Look)checkIfExists ("Looks/" + name);
132         filter.attachTo (bean);
133
134         assertTrue (bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY)));
135         assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY));
136
137     }
138
139     public void testFILTER_ALL_METHODS_MINUS_GET_NAME () throws Exception JavaDoc {
140         doFILTER_ALL_METHODS_MINUS_GET_NAME ("FILTER_ALL_METHODS_MINUS_GET_NAME");
141     }
142
143     public void testFILTER_ALL_METHODS_MINUS_GET_NAME2 () throws Exception JavaDoc {
144         doFILTER_ALL_METHODS_MINUS_GET_NAME ("FILTER_ALL_METHODS_MINUS_GET_NAME2");
145     }
146
147     private void doFILTER_ALL_METHODS_MINUS_GET_NAME (String JavaDoc name) throws Exception JavaDoc {
148         //System.out.println("testFILTER_ALL_METHODS_MINUS_GET_NAME");
149
// set delegate look and representive object
150
initFilterLookTest ();
151
152         // reference filter look
153
mask = Look.ALL_METHODS & ~Look.GET_NAME;
154         Look filterRef = Looks.filter ("TestFilter", delegate, mask);
155         filterRef.attachTo (bean);
156
157         // get look declared in layer
158
Look filter = (Look)checkIfExists ("Looks/" + name);
159         filter.attachTo (bean);
160
161         assertTrue (!bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY)));
162         assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY));
163
164     }
165
166     public void testFILTER_NO_METHODS () throws Exception JavaDoc {
167         doFILTER_NO_METHODS ("FILTER_NO_METHODS");
168     }
169
170     public void testFILTER_NO_METHODS2 () throws Exception JavaDoc {
171         doFILTER_NO_METHODS ("FILTER_NO_METHODS");
172     }
173
174     private void doFILTER_NO_METHODS (String JavaDoc name) throws Exception JavaDoc {
175         //System.out.println("testFILTER_NO_METHODS");
176
// set delegate look and representive object
177
initFilterLookTest ();
178
179         // reference filter look
180
mask = Look.NO_METHODS;
181         Look filterRef = Looks.filter ("TestFilter", delegate, mask);
182         filterRef.attachTo (bean);
183
184         // get look declared in layer
185
Look filter = (Look)checkIfExists ("Looks/" + name);
186         filter.attachTo (bean);
187
188         assertTrue (!bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY)));
189         assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY));
190     }
191
192     public void testFILTER_NO_METHODS_PLUS_GET_NAME () throws Exception JavaDoc {
193         doFILTER_NO_METHODS_PLUS_GET_NAME ("FILTER_NO_METHODS_PLUS_GET_NAME");
194     }
195
196     public void testFILTER_NO_METHODS_PLUS_GET_NAME2 () throws Exception JavaDoc {
197         doFILTER_NO_METHODS_PLUS_GET_NAME ("FILTER_NO_METHODS_PLUS_GET_NAME2");
198     }
199
200     public void doFILTER_NO_METHODS_PLUS_GET_NAME (String JavaDoc name) throws Exception JavaDoc {
201         //System.out.println("testFILTER_NO_METHODS_PLUS_GET_NAME");
202
// set delegate look and representive object
203
initFilterLookTest ();
204
205         // reference filter look
206
mask = Look.NO_METHODS | Look.GET_NAME;
207         Look filterRef = Looks.filter ("TestFilter", delegate, mask);
208         filterRef.attachTo (bean);
209
210         // get look declared in layer
211
Look filter = (Look)checkIfExists ("Looks/" + name );
212         filter.attachTo (bean);
213
214         assertTrue (bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY)));
215         assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY));
216     }
217
218     public void testNamespaceSelector () throws Exception JavaDoc {
219         // XXX pending: how set initial context for different filesystem
220
LookSelector selectorRef = org.netbeans.spi.looks.Selectors.namespaceTypes ("Looks/Types/Beans/");
221         LookSelector selector = (LookSelector)checkIfExists ("Looks/Selectors/NAMESPACE_SELECTOR");
222
223         String JavaDoc s = new String JavaDoc ();
224         Enumeration JavaDoc enRef = selectorRef.getLooks (s);
225         Enumeration JavaDoc en = selector.getLooks (s);
226         
227         int i = 0;
228         while (enRef.hasMoreElements () && en.hasMoreElements ()) {
229             assertSame ("Found looks are same.", enRef.nextElement (), en.nextElement ());
230             i ++;
231         }
232         
233         assertTrue( "Two looks should be found. Found " + i + ".", i == 2 );
234         assertTrue ("No more items from reference selector.", !enRef.hasMoreElements ());
235         assertTrue ("No more items from declared selector.", !en.hasMoreElements ());
236     }
237     
238     
239     public void testNamespaceSelectorCaching() {
240         LookSelector selector = Selectors.namespaceTypes( "Looks/Types/Simple/" );
241         
242         assertNotNull( "Selector should exist.", selector );
243         
244         
245         // When nobody points to the look it should disapear
246
String JavaDoc ro = "REPRESENTED OBJECT";
247         for( int i = 0; i < 100; i++ ) {
248             
249             if ( i % 10 == 0 ) {
250                 Object JavaDoc k = new Object JavaDoc();
251                 WeakReference JavaDoc wr = new WeakReference JavaDoc( k );
252                 k = null;
253                 System.gc();
254                 assertGC( "Reference should disapear", wr );
255             }
256             
257             Enumeration JavaDoc e = selector.getLooks( ro );
258             assertTrue( "Should find instance counting look.", e.nextElement() instanceof InstanceCountingLook );
259             assertTrue( "It should be the only look.", !e.hasMoreElements() );
260             assertEquals( "There should be one instance of the look.", 1, InstanceCountingLook.getInstanceCount() );
261         }
262         
263     }
264
265     private static Look instanceCountingLook() {
266         return new InstanceCountingLook();
267     }
268     
269     public void testDecoratorSelector () throws Exception JavaDoc {
270         doDecoratorSelector (
271             "DECORATOR_SELECTOR",
272             (Look)checkIfExists ("Looks/Bean/SimpleBeanLook"),
273             namespaceSelectorForLooksTypesBeans ()
274         );
275     }
276
277     public void testDecoratorSelector2 () throws Exception JavaDoc {
278         doDecoratorSelector (
279             "DECORATOR_SELECTOR2",
280             Looks.bean(),
281             (LookSelector)checkIfExists ("Looks/Selectors/NAMESPACE_SELECTOR")
282         );
283     }
284
285     public static LookSelector namespaceSelectorForLooksTypesBeans () {
286         return Selectors.namespaceTypes("Looks/Types/Properties");
287     }
288
289     private void doDecoratorSelector (String JavaDoc name, Look decoratingLook, LookSelector selector) throws Exception JavaDoc {
290
291         //LookSelector decoratorRef = Looks.decorator (selector, decoratingLook, true, true);
292
LookSelector decoratorRef = Selectors.decorator ( selector, decoratingLook );
293
294         LookSelector decorator = (LookSelector)checkIfExists ("Looks/Selectors/" + name);
295
296         String JavaDoc s = new String JavaDoc ();
297         Enumeration JavaDoc enRef = decoratorRef.getLooks (s);
298         Enumeration JavaDoc en = decorator.getLooks (s);
299         Enumeration JavaDoc enSelector = selector.getLooks (s);
300         while (enRef.hasMoreElements () && en.hasMoreElements () && enSelector.hasMoreElements ()) {
301             // pending: check decorated looks
302
en.nextElement ();
303             enRef.nextElement ();
304             enSelector.nextElement ();
305         }
306         assertTrue ("No more items from docorated selector.", !enSelector.hasMoreElements ());
307         assertTrue ("No more items from reference decorator.", !enRef.hasMoreElements ());
308         assertTrue ("No more items from decorator.", !en.hasMoreElements ());
309
310
311     }
312
313     // helper methods ----------------------------------------------------------
314

315     private Object JavaDoc checkInstance (Object JavaDoc inst, String JavaDoc name) throws Exception JavaDoc {
316         Object JavaDoc instRef = checkIfExists( name );
317         assertEquals ("Instance is the same", inst, instRef);
318         return instRef;
319     }
320
321
322     private Object JavaDoc checkIfExists (String JavaDoc name) throws Exception JavaDoc {
323         
324         org.netbeans.modules.looks.RegistryBridge registryBridge = org.netbeans.modules.looks.RegistryBridge.getDefault( null );
325         Object JavaDoc o = registryBridge.resolve( name );
326         assertNotNull( "Name " + name + " not found.", o );
327         return o;
328         
329     }
330
331     private void initFilterLookTest () {
332         if (bean != null)
333             return ;
334         try {
335             delegate = (Look)checkInstance (Looks.bean(), "Looks/Bean/SimpleBeanLook"); // NOI18N
336
assertNotNull (delegate);
337             bean = new javax.swing.JTextField JavaDoc ();
338             bean.setName ("dummy"); // NOI18N
339
} catch (Exception JavaDoc e) {
340             fail (e.getMessage ());
341         }
342     }
343     
344     // test classes ------------------------------------------------------------
345

346     private static class InstanceCountingLook extends Look {
347         
348         private static int count = 0;
349         
350         public InstanceCountingLook() {
351             super( "ICL " + count++ );
352         }
353         
354         
355         public static int getInstanceCount() {
356             return count;
357         }
358         
359     }
360
361 }
362
363
364
365
Popular Tags