KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.nodes2looks.Nodes;
29 import org.netbeans.api.nodes2looks.TestUtil;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.nodes.Node;
32 import org.openide.util.Enumerations;
33 import org.openide.util.Lookup;
34
35 /** Various tests for selectors on nodes, including decoration
36  *
37  * @author Petr Hrebejk. Jiri Rechtacek
38  */

39 public class SelectorTest extends NbTestCase {
40     
41     public SelectorTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     /** Tests whether all nodes inherit the selector through the
46      * hirearchy
47      */

48     public void testSimpleSelector() {
49         
50         LookSelector rootSelector = Selectors.selector( new RootProvider( null ) );
51         Node root = Nodes.node ( "ROOT", null, rootSelector);
52         
53         Node[] nodes = root.getChildren ().getNodes ();
54         
55         // children
56
assertEquals("Root has two childner.", 2, nodes.length);
57         assertEquals("Root LookNode has RootSelector.", rootSelector, TestUtil.getLookSelector ( root ));
58         Node children[] = root.getChildren().getNodes();
59                 
60         // looks
61
assertEquals( "Look of child is a instance of LookForRepObj1", Looks.bean().getClass(), TestUtil.getLook( children[0] ).getClass() );
62         assertEquals( "Look of child is a instance of LookForRepObj2", Looks.bean().getClass(), TestUtil.getLook ( children[1] ).getClass() );
63         
64         // selectors
65
assertTrue ("Selector of child 1 ", rootSelector == TestUtil.getLookSelector ( children[0] ) );
66         assertTrue ("Selector of child 2 ", rootSelector == TestUtil.getLookSelector( children[1] ) );
67     }
68     
69     /** Tests that the Selector for children changes when provided in lookup
70      */

71     public void testChangeSimpleSelector() {
72         LookSelector restrictedSelector = Selectors.selector( new RestrictedProvider() );
73         LookSelector rootSelector = Selectors.selector( new RootProvider ( restrictedSelector ) );
74         Node root = Nodes.node ( "ROOT", null, rootSelector);
75         
76         Node[] nodes = root.getChildren ().getNodes ();
77         
78         // children
79
assertEquals("Root has two childner.", 2, nodes.length);
80         assertEquals("Root LookNode has RootSelector.", rootSelector, TestUtil.getLookSelector ( root ));
81         Node children[] = root.getChildren().getNodes();
82         
83         // looks
84
assertEquals( "Look of child is a instance of LookForRepObj1", LookForRepObj1.class, TestUtil.getLook( children[0] ).getClass() );
85         assertEquals( "Look of child is a instance of LookForRepObj2", LookForRepObj2.class, TestUtil.getLook ( children[1] ).getClass() );
86         
87         // selectors
88
assertTrue ("Selector of child 1 ", restrictedSelector == TestUtil.getLookSelector ( children[0] ) );
89         assertTrue ("Selector of child 2 ", restrictedSelector == TestUtil.getLookSelector( children[1] ) );
90     }
91     
92     /*
93     public void testChangeDecoratorSelectorExcludable () {
94         
95         Look rootLook = new RootLook ();
96         // isDecorator = true;
97         // excludable = true;
98         LookSelector rootSelector = new ProxySelector (new RootSelector ());
99         Node root = Looks.node (null, rootLook, rootSelector);
100         Node[] nodes = root.getChildren ().getNodes ();
101         // children
102         assertEquals("Root has two childner.", 2, nodes.length);
103         assertEquals("Root LookNode has RootSelector.", rootSelector, TestUtil.getLookSelector ( root ));
104         Node n1 = root.getChildren ().findChild ("LookForRepObj1");
105         Node n2 = root.getChildren ().findChild ("LookForRepObj2");
106         // look nodes
107         assertTrue ("Child is a instance of LookNode", n1 instanceof org.netbeans.modules.looks.LookNode);
108         assertTrue ("Child is a instance of LookNode", n2 instanceof org.netbeans.modules.looks.LookNode);
109         // looks
110         assertEquals ("Look of child is a instance of LookForRepObj1", "Composite[Decorated[LookForRepObj1", TestUtil.getLook ( n1 ).getName ());
111         assertEquals ("Look of child is a instance of LookForRepObj2", "Composite[Decorated[LookForRepObj2", TestUtil.getLook ( n2 ).getName ());
112         // selectors
113         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+ TestUtil.getLookSelector ( n1 ), TestUtil.getLookSelector ( n1 ) instanceof DecoratorSelector);
114         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+ TestUtil.getLookSelector ( n2 ), TestUtil.getLookSelector ( n2 ) instanceof DecoratorSelector);
115
116         // second level
117         nodes = nodes[0].getChildren ().getNodes ();
118         // children
119         assertEquals("Child has one child.", 1, nodes.length);
120         n1 = root.getChildren ().findChild ("LookForRepObj1");
121         // look nodes
122         assertTrue ("Child is a instance of LookNode", n1 instanceof org.netbeans.modules.looks.LookNode);
123         // looks - one times decorated
124         assertEquals ("Look of child is a instance of LookForRepObj1", "Composite[Decorated[LookForRepObj1", TestUtil.getLook ( n1 ).getName ());
125         // selectors
126         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+ TestUtil.getLookSelector ( n1 ), TestUtil.getLookSelector ( n1 ) instanceof DecoratorSelector);
127         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+ TestUtil.getLookSelector ( n2 ), TestUtil.getLookSelector ( n2 ) instanceof DecoratorSelector);
128         
129     }
130      */

131     
132     /*
133     public void testChangeDecoratorSelectorNonExcludable () {
134         Look rootLook = new RootLook ();
135         isDecorator = true;
136         excludable = false;
137         LookSelector rootSelector = new ProxySelector (new RootSelector ());
138         Node root = Looks.node (null, rootLook, rootSelector);
139         Node[] nodes = root.getChildren ().getNodes ();
140         // children
141         assertEquals("Root has two childner.", 2, nodes.length);
142         assertEquals("Root LookNode has RootSelector.", rootSelector, TestUtil.getLookSelector ( root ));
143         Node n1 = root.getChildren ().findChild ("LookForRepObj1");
144         Node n2 = root.getChildren ().findChild ("LookForRepObj2");
145         // look nodes
146         assertTrue ("Child is a instance of LookNode", n1 instanceof org.netbeans.modules.looks.LookNode);
147         assertTrue ("Child is a instance of LookNode", n2 instanceof org.netbeans.modules.looks.LookNode);
148         // looks - two times decorated
149         assertEquals ("Look of child is a instance of LookForRepObj1", "Composite[Decorated[Composite[Decorated[LookForRepObj1", TestUtil.getLook ( n1 ).getName ());
150         assertEquals ("Look of child is a instance of LookForRepObj2", "Composite[Decorated[Composite[Decorated[LookForRepObj2", TestUtil.getLook ( n2 ).getName ());
151         // selectors
152         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+ TestUtil.getLookSelector ( n1 ), TestUtil.getLookSelector ( n1 ) instanceof DecoratorSelector);
153
154         // second level
155         nodes = nodes[0].getChildren ().getNodes ();
156         // children
157         assertEquals("Child has one child.", 1, nodes.length);
158         n1 = root.getChildren ().findChild ("LookForRepObj1");
159         // look nodes
160         assertTrue ("Child is a instance of LookNode", n1 instanceof org.netbeans.modules.looks.LookNode);
161         // looks
162         assertEquals ("Look of child of child is a instance of LookForRepObj1", "Composite[Decorated[Composite[Decorated[LookForRepObj1", TestUtil.getLook ( n1 ).getName ());
163         // selectors
164         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+ TestUtil.getLookSelector ( n1 ), TestUtil.getLookSelector ( n1 ) instanceof DecoratorSelector);
165     }
166      */

167     
168     /*
169     public void testChangeContextSelectorExcludable () {
170         try {
171         Look rootLook = new RootLook ();
172         isDecorator = false;
173         isStatic = true;
174         excludable = true;
175         LookSelector rootSelector = new ProxySelector (new RootSelector ());
176         LookNode root = new LookNode (null, rootLook, rootSelector);
177         Node[] nodes = root.getChildren ().getNodes ();
178         // children
179         assertEquals("Root has two childner.", 2, nodes.length);
180         assertEquals("Root LookNode has RootSelector.", rootSelector, root.getLookSelector ());
181         Node n1 = root.getChildren ().findChild ("LookForRepObj1");
182         Node n2 = root.getChildren ().findChild ("LookForRepObj2");
183         // look nodes
184         assertTrue ("Child is a instance of LookNode", n1 instanceof LookNode);
185         assertTrue ("Child is a instance of LookNode", n2 instanceof LookNode);
186         // looks
187         assertEquals ("Look of child is a instance of LookForRepObj1", "Composite[Decorated[LookForRepObj1", ((LookNode)n1).getLook ().getName ());
188         assertEquals ("Look of child is a instance of LookForRepObj2", "Composite[Decorated[LookForRepObj2", ((LookNode)n2).getLook ().getName ());
189         // selectors
190         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+((LookNode)n1).getLookSelector ().getName (), ((LookNode)n1).getLookSelector () instanceof DecoratorSelector);
191         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+((LookNode)n2).getLookSelector ().getName (), ((LookNode)n2).getLookSelector () instanceof DecoratorSelector);
192         // second level
193         nodes = nodes[0].getChildren ().getNodes ();
194         // children
195         assertEquals("Root has one child.", 1, nodes.length);
196         assertEquals("Root LookNode has RootSelector.", rootSelector, root.getLookSelector ());
197         n1 = root.getChildren ().findChild ("LookForRepObj1");
198         n2 = root.getChildren ().findChild ("LookForRepObj2");
199         // look nodes
200         assertTrue ("Child is a instance of LookNode", n1 instanceof LookNode);
201         assertTrue ("Child is a instance of LookNode", n2 instanceof LookNode);
202         // looks - one times decorated
203         assertEquals ("Look of child is a instance of LookForRepObj1", "Composite[Decorated[LookForRepObj1", ((LookNode)n1).getLook ().getName ());
204         assertEquals ("Look of child is a instance of LookForRepObj2", "Composite[Decorated[LookForRepObj2", ((LookNode)n2).getLook ().getName ());
205         // selectors
206         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+((LookNode)n1).getLookSelector ().getName (), ((LookNode)n1).getLookSelector () instanceof DecoratorSelector);
207         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+((LookNode)n2).getLookSelector ().getName (), ((LookNode)n2).getLookSelector () instanceof DecoratorSelector);
208         } catch (Exception e) {
209             e.printStackTrace ();
210         }
211     }
212
213     public void testChangeContextSelectorNonExcludable () {
214         try {
215         Look rootLook = new RootLook ();
216         isDecorator = false;
217         isStatic = true;
218         excludable = false;
219         LookSelector rootSelector = new ProxySelector (new RootSelector ());
220         LookNode root = new LookNode (null, rootLook, rootSelector);
221         Node[] nodes = root.getChildren ().getNodes ();
222         // children
223         assertEquals("Root has two childner.", 2, nodes.length);
224         assertEquals("Root LookNode has RootSelector.", rootSelector, root.getLookSelector ());
225         Node n1 = root.getChildren ().findChild ("LookForRepObj1");
226         Node n2 = root.getChildren ().findChild ("LookForRepObj2");
227         // look nodes
228         assertTrue ("Child is a instance of LookNode", n1 instanceof LookNode);
229         assertTrue ("Child is a instance of LookNode", n2 instanceof LookNode);
230         // looks - two times decorated
231         assertEquals ("Look of child is a instance of LookForRepObj1", "Composite[Decorated[Composite[Decorated[LookForRepObj1", ((LookNode)n1).getLook ().getName ());
232         assertEquals ("Look of child is a instance of LookForRepObj2", "Composite[Decorated[Composite[Decorated[LookForRepObj2", ((LookNode)n2).getLook ().getName ());
233         // selectors
234         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+((LookNode)n1).getLookSelector ().getName (), ((LookNode)n1).getLookSelector () instanceof DecoratorSelector);
235
236         // second level
237         nodes = nodes[0].getChildren ().getNodes ();
238         // children
239         assertEquals("Root has one child.", 1, nodes.length);
240         assertEquals("Root LookNode has RootSelector.", rootSelector, root.getLookSelector ());
241         n1 = root.getChildren ().findChild ("LookForRepObj1");
242         // look nodes
243         assertTrue ("Child is a instance of LookNode", n1 instanceof LookNode);
244         // looks
245         assertEquals ("Look of child of child is a instance of LookForRepObj1", "Composite[Decorated[Composite[Decorated[LookForRepObj1", ((LookNode)n1).getLook ().getName ());
246         // selectors
247         assertTrue ("Selector of child is a instance of DecoratorSelector, was "+((LookNode)n1).getLookSelector ().getName (), ((LookNode)n1).getLookSelector () instanceof DecoratorSelector);
248         } catch (Exception e) {
249             e.printStackTrace ();
250         }
251     }
252     
253      */

254     
255     // Selectors for testing ---------------------------------------------------
256

257     public static class RootProvider implements LookProvider {
258     
259         private Look stringLook;
260         private Look r1Look;
261         private Look r2Look;
262         
263         public RootProvider( LookSelector chidrenSelector ) {
264             if ( chidrenSelector == null ) {
265                 stringLook = new RootLook( null );
266                 r1Look = Looks.bean();
267                 r2Look = Looks.bean();
268             }
269             else {
270                 stringLook = new RootLook( chidrenSelector );
271                 r1Look = LookForRepObj1.INSTANCE;
272                 r2Look = LookForRepObj2.INSTANCE;
273             }
274         }
275
276         public Enumeration JavaDoc getLooksForObject(Object JavaDoc representedObject) {
277             if (representedObject instanceof RepObj1) {
278                 return Enumerations.singleton(r1Look);
279             }
280             else if ( representedObject instanceof RepObj2 ) {
281                 return Enumerations.singleton(r2Look);
282             }
283             else if ( representedObject instanceof String JavaDoc ) {
284                 return Enumerations.singleton(stringLook);
285             }
286             else {
287                 return Enumerations.singleton(Looks.bean());
288             }
289         }
290         
291     }
292     
293     public class RestrictedProvider implements LookProvider {
294         
295         public RestrictedProvider() {
296         }
297         
298         public Enumeration JavaDoc getLooksForObject( Object JavaDoc representedObject ) {
299             if ( representedObject instanceof RepObj1 ) {
300                 return Enumerations.singleton(LookForRepObj1.INSTANCE);
301             }
302             else if ( representedObject instanceof RepObj2 ) {
303                 return Enumerations.singleton(LookForRepObj2.INSTANCE);
304             }
305             else
306                 throw new RuntimeException JavaDoc ("Wrong type of represented object, was "+representedObject);
307         }
308                 
309     }
310     
311     // proxy selector
312

313     /*
314     public class ProxySelector extends LookSelector {
315         
316         private LookSelector delegate;
317         
318         public ProxySelector (LookSelector delegate) {
319             this.delegate = delegate;
320         }
321         
322         public Enumeration getLooks(Object representedObject) {
323             if (isDecorator)
324                 return Collections.enumeration (Arrays.asList (new Object[] { Selectors.decorator (delegate, new DecoratorLook () ) }));
325             // else if (isStatic)
326             // return Collections.enumeration (Arrays.asList (new Object[] { Looks.contextSelector (delegate, new DecoratorLook (), true, excludable) }));
327             else
328                 return Collections.enumeration (Arrays.asList (new Object[] { delegate }));
329         }
330         
331     }
332     */

333     
334     
335     // selector for represented object 1
336

337     public class ProviderForRepObj1 implements LookProvider {
338         private String JavaDoc name = "SelectorForRepObj1";
339         
340         public ProviderForRepObj1 () {
341         }
342         
343         public Enumeration JavaDoc getLooksForObject(Object JavaDoc representedObject) {
344             if (representedObject instanceof RepObj1)
345                 return Collections.enumeration (Arrays.asList (new Object JavaDoc[] { new LookForRepObj1 () }));
346             else
347                 throw new RuntimeException JavaDoc ("Wrong type of represented object, was "+representedObject);
348         }
349         
350     }
351     
352     // selector for represented object 2
353

354     public class ProviderForRepObj2 implements LookProvider {
355         private String JavaDoc name = "SelectorForRepObj2";
356         
357         public ProviderForRepObj2 () {
358         }
359         
360         public Enumeration JavaDoc getLooksForObject(Object JavaDoc representedObject) {
361             if (representedObject instanceof RepObj2)
362                 return Collections.enumeration (Arrays.asList (new Object JavaDoc[] { new LookForRepObj2 () }));
363             else
364                 throw new RuntimeException JavaDoc ("Wrong type of represented object, was "+representedObject);
365         }
366                 
367     }
368     
369     // Looks for testing -------------------------------------------------------
370

371     private static class RootLook extends Look {
372                 
373         private LookSelector childrenSelector;
374         
375         public RootLook ( LookSelector childrenSelector ) {
376             super( "RootLook" );
377             this.childrenSelector = childrenSelector;
378         }
379                 
380         public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
381             return getName();
382         }
383
384         public boolean isLeaf ( Object JavaDoc representedObject, Lookup env ) {
385             return false;
386         }
387
388         public List JavaDoc getChildObjects( Object JavaDoc representedObject, Lookup env ) {
389             ArrayList JavaDoc objects = new ArrayList JavaDoc ();
390             objects.add (new RepObj1 ("RepObj1"));
391             objects.add (new RepObj2 ("RepObj2"));
392             return objects;
393         }
394         
395         public Collection JavaDoc getLookupItems( Object JavaDoc representedObject, Lookup env ) {
396             if ( childrenSelector != null ) {
397                 ArrayList JavaDoc items = new ArrayList JavaDoc();
398                 items.add( new GoldenValue.TestLookupItem( childrenSelector ) );
399                 return items;
400             }
401             else {
402                 return null;
403             }
404         }
405         
406     }
407
408     private static class LookForRepObj1 extends Look {
409         
410         public static Look INSTANCE = new LookForRepObj1();
411         
412         public LookForRepObj1 () {
413             super( "LookForRepObj1" );
414         }
415         
416         public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
417             return getName();
418         }
419
420         public boolean isLeaf( Object JavaDoc representedObject, Lookup env ) {
421             return false;
422         }
423         
424         public List JavaDoc getChildObjects( Object JavaDoc representedObject, Lookup env ) {
425             ArrayList JavaDoc objects = new ArrayList JavaDoc ();
426             objects.add (new RepObj1 ("RepObj1"));
427             return objects;
428         }
429     }
430
431     private static class LookForRepObj2 extends Look {
432         
433         public static Look INSTANCE = new LookForRepObj2();
434         
435         public LookForRepObj2 () {
436             super( "LookForRepObj2" );
437         }
438         
439         public String JavaDoc getDisplayName() {
440             return getName();
441         }
442         
443         public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
444             return getName();
445         }
446
447         public boolean isLeaf( Object JavaDoc representedObject, Lookup env ) {
448             return false;
449         }
450         
451         public List JavaDoc getChildObjects( Object JavaDoc representedObject, Lookup env ) {
452             ArrayList JavaDoc objects = new ArrayList JavaDoc ();
453             objects.add (new RepObj2 ("RepObj2"));
454             return objects;
455         }
456     }
457
458     private static class DecoratorLook extends Look {
459         
460         
461         public DecoratorLook () {
462             super( "DecoratorLook" );
463         }
464         
465         
466         public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
467             return getName();
468         }
469         
470         public boolean isLeaf ( Object JavaDoc representedObject, Lookup env ) {
471             return true;
472         }
473     }
474
475     // Represented objects for testing -----------------------------------------
476

477     public static class RepObj1 extends Object JavaDoc {
478         
479         private String JavaDoc name;
480         
481         public RepObj1 (String JavaDoc name) {
482             this.name = name;
483         }
484         
485         public String JavaDoc toString () {
486             return "Represented object "+name;
487         }
488     }
489
490     public static class RepObj2 extends Object JavaDoc {
491         private String JavaDoc name;
492         
493         public RepObj2 (String JavaDoc name) {
494             this.name = name;
495         }
496         
497         public String JavaDoc toString () {
498             return "Represented object "+name;
499         }
500     }
501      
502      
503 }
504
Popular Tags