KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
23 import java.util.*;
24
25 import junit.framework.*;
26 import org.netbeans.api.nodes2looks.Nodes;
27
28 import org.openide.filesystems.*;
29 import org.netbeans.junit.*;
30 import org.openide.util.Lookup;
31 import org.netbeans.api.registry.*;
32 import org.netbeans.modules.looks.SelectorListener;
33 import org.netbeans.spi.registry.*;
34 import org.openide.nodes.Node;
35 import org.openide.nodes.NodeListener;
36
37 /** Tests the behavior of NamespaceSelector when changing
38  * the underlying layers.
39  *
40  * @author Petr Hrebejk
41  */

42 public class NamespaceSelectorEventsTest extends NbTestCase {
43     
44     private FileSystem xfs1, xfs2;
45     private TestMFS mfs;
46     
47     public NamespaceSelectorEventsTest(java.lang.String JavaDoc testName) {
48         super(testName);
49     }
50     
51     public static void main(java.lang.String JavaDoc[] args) {
52         junit.textui.TestRunner.run(suite());
53     }
54
55     public static Test suite() {
56         TestSuite suite = new NbTestSuite(NamespaceSelectorEventsTest.class);
57         return suite;
58     }
59     
60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         
63         URL JavaDoc u1 = getClass ().getResource ("SelectorEventTest1.xml");
64         URL JavaDoc u2 = getClass ().getResource ("SelectorEventTest2.xml");
65                
66         xfs1 = new XMLFileSystem( u1 );
67         xfs2 = new XMLFileSystem( u2 );
68         mfs = new TestMFS( new FileSystem[] { xfs1, xfs2 } );
69         
70         FileObject mfsRoot = mfs.getRoot();
71         org.netbeans.modules.looks.RegistryBridge.setDefault( mfsRoot );
72         
73     }
74     
75     public void testRegistryEvents() throws Exception JavaDoc {
76         
77         Map env = new HashMap();
78         
79         Context ctx = SpiUtils.createContext(
80                 org.netbeans.api.registry.fs.FileSystemContextFactory.createContext( mfs.getRoot() ) );
81         
82         Context sCtx = ctx.getSubcontext( "Selectors/Simple/" );
83         Context jlCtx = sCtx.getSubcontext( "java/lang" );
84         
85         
86         LookSelector selector = Selectors.namespaceTypes( "Selectors/Simple/" );
87         
88         String JavaDoc ro = "REPRESENTED OBJECT";
89         Enumeration e = selector.getLooks( ro );
90         
91         assertEquals( "Should only contain two bindings", 2,
92                       jlCtx.getSubcontextNames().size() + jlCtx.getBindingNames().size() );
93         
94         TestContextListener tcl = new TestContextListener();
95         
96         sCtx.addContextListener( tcl );
97         
98         System.gc();
99         
100         mfs.setDels( new FileSystem[] { xfs1 } );
101         
102         assertEquals( "Should only contain one binding", 1,
103                       jlCtx.getSubcontextNames().size() + jlCtx.getBindingNames().size() );
104         assertEquals( "Should get one event ", 1, tcl.events.size() );
105         
106     }
107     
108     public void testNamespaceSelectorEvents() {
109         
110         LookSelector selector = Selectors.namespaceTypes( "Selectors/Simple/" );
111         TestLookSelectorListener tlsl = new TestLookSelectorListener();
112         assertNotNull( "Selector should exist.", selector );
113         
114         String JavaDoc ro = "REPRESENTED OBJECT";
115         Enumeration e = selector.getLooks( ro );
116                        
117         org.netbeans.modules.looks.Accessor.DEFAULT.addSelectorListener( selector, tlsl );
118            
119         System.gc();
120         
121         mfs.setDels( new FileSystem[] { xfs1 } );
122         
123         assertEquals( "Should get one event ", 1, tlsl.events.size() );
124     }
125     
126     
127     public void testNamespaceSelectorEventsOnNode() throws Exception JavaDoc {
128         
129         LookSelector selector = Selectors.namespaceTypes( "Selectors/Simple/" );
130         assertNotNull( "Selector should exist.", selector );
131         
132         String JavaDoc ro = "REPRESENTED OBJECT";
133         
134         Node node = Nodes.node( ro, null, selector );
135         TestNodeListener tnl = new TestNodeListener();
136         node.addNodeListener( tnl );
137         
138         assertEquals( "Node name should be created by the StringLook.", StringLook.computeName( ro ), node.getName() );
139         assertEquals( "No events should be fired", 0, tnl.events.size() );
140                 
141         mfs.setDels( new FileSystem[] { xfs1 } );
142                
143         assertEquals( "Now it should change to ObjectLook.", ObjectLook.computeName( ro ), node.getName() );
144         assertEquals( "6 events should be fired", 5, tnl.events.size() );
145                 
146         mfs.setDels( new FileSystem[] { xfs1, xfs2 } );
147         
148         assertEquals( "Now it should change back to StringLook.", StringLook.computeName( ro ), node.getName() );
149         assertEquals( "Anodther 6 events should be fired", 11, tnl.events.size() );
150         
151         mfs.setDels( new FileSystem[] { xfs2 } );
152         
153         assertEquals( "Now it should rmain StringLook.", StringLook.computeName( ro ), node.getName() );
154         assertEquals( "No additional events should be fired", 11, tnl.events.size() );
155
156     }
157     
158     public void testProxyLook() {
159         
160         Look look = (Look)org.netbeans.modules.looks.RegistryBridge.getDefault( null ).resolve( "Looks/Composite/TEST_COMPOSITE" );
161         assertNotNull( "Look should exist.", look );
162         
163         String JavaDoc ro1 = "REPRESENTED OBJECT 1";
164         String JavaDoc ro2 = "REPRESENTED OBJECT 2";
165         
166         Node n1 = Nodes.node( ro1, look );
167         Node n2 = Nodes.node( ro2, look );
168         
169         TestNodeListener tnl1 = new TestNodeListener();
170         n1.addNodeListener( tnl1 );
171         TestNodeListener tnl2 = new TestNodeListener();
172         n2.addNodeListener( tnl2 );
173         
174         // Just make sure the proxy does work
175
List children = look.getChildObjects( ro1, Lookup.EMPTY );
176         assertNotNull( "There should be some children", children );
177         assertEquals( "There should be two children", 2, children.size() );
178         
179         assertTrue( "First child should be from ObjectLook", ObjectLook.CHILD == children.get( 0 ) );
180         assertTrue( "Second child should be from StringLook", StringLook.CHILD == children.get( 1 ) );
181         
182         
183         mfs.setDels( new FileSystem[] { xfs1 } );
184         
185         children = look.getChildObjects( ro1, Lookup.EMPTY );
186         assertEquals( "There should be oly one child now", 1, children.size() );
187         assertTrue( "The child should be from ObjectLook", ObjectLook.CHILD == children.get( 0 ) );
188         
189         assertEquals( "7 events should be fired from first node", 7, tnl1.events.size() );
190         assertEquals( "7 events should be fired drom second node", 7, tnl2.events.size() );
191         
192     }
193     
194     
195     private void printList( List list ) {
196         System.err.println("------------------------" + list.size() );
197         for( Iterator it = list.iterator(); it.hasNext(); ) {
198             System.err.println( it.next() );
199         }
200         
201     }
202     
203     // Helper methods ----------------------------------------------------------
204

205     private static Look objectLook() {
206         return ObjectLook.INSTANCE;
207     }
208     
209     private static Look stringLook() {
210         return StringLook.INSTANCE;
211     }
212     
213     // Helper classes ----------------------------------------------------------
214

215     /** Used from other SelectorEventsTests
216      */

217     public static class TestLookSelectorListener implements SelectorListener {
218         
219         List events = new ArrayList();
220         
221         public void contentsChanged(org.netbeans.modules.looks.SelectorEvent event) {
222             events.add( event );
223         }
224         
225     }
226     
227     private static class TestContextListener implements ContextListener {
228             
229         private List events = new ArrayList();
230         
231         public void attributeChanged(AttributeEvent evt) {
232             events.add( evt );
233         }
234         
235         public void bindingChanged(BindingEvent evt) {
236             events.add( evt );
237         }
238         
239         public void subcontextChanged(SubcontextEvent evt) {
240             events.add( evt );
241         }
242         
243     }
244     
245          
246     private static class TestNodeListener implements NodeListener {
247         
248         List events = new ArrayList();
249         
250         public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) {
251             events.add( ev );
252         }
253         
254         public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) {
255             events.add( ev );
256         }
257         
258         public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) {
259             events.add( ev );
260         }
261         
262         public void nodeDestroyed(org.openide.nodes.NodeEvent ev) {
263             events.add( ev );
264         }
265         
266         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc ev ) {
267             events.add( ev );
268         }
269         
270     }
271     
272     private static class TestMFS extends MultiFileSystem {
273         
274         public TestMFS() {
275             super();
276         }
277         
278         
279         public TestMFS( FileSystem[] delegates ) {
280             super( delegates );
281         }
282         
283         public void setDels( FileSystem[] fss ) {
284             setDelegates( fss );
285         }
286         
287     }
288     
289     private static class ObjectLook extends Look {
290         
291         public static final String JavaDoc CHILD = "OBJECT_CHILD";
292         
293         public static final Look INSTANCE = new ObjectLook();
294         
295         public ObjectLook() {
296             super( "TestObjectLook" );
297         }
298         
299         public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
300             return computeName( representedObject );
301         }
302         
303         public List getChildObjects( Object JavaDoc representedObject, Lookup env ) {
304             List ch = new ArrayList();
305             ch.add( CHILD );
306             return ch;
307         }
308         
309         public static String JavaDoc computeName( Object JavaDoc representedObject ) {
310             return representedObject.getClass().getName() + " : " + representedObject.hashCode();
311         }
312         
313     }
314     
315     
316     private static class StringLook extends Look {
317         
318         public static final Look INSTANCE = new StringLook();
319         
320         public static final String JavaDoc CHILD = "STRING_CHILD";
321         
322         public StringLook() {
323             super( "TestStringLook" );
324         }
325         
326         public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
327             return computeName( representedObject );
328         }
329         
330         public List getChildObjects( Object JavaDoc representedObject, Lookup env ) {
331             List ch = new ArrayList();
332             ch.add( CHILD );
333             return ch;
334         }
335         
336         public static String JavaDoc computeName( Object JavaDoc representedObject ) {
337             return (String JavaDoc)representedObject;
338         }
339         
340     }
341     
342     private static class TestProxyLook extends ProxyLook {
343         
344         TestProxyLook( String JavaDoc name, LookSelector selector ) {
345             super( name, selector );
346         }
347     }
348         
349 }
350
Popular Tags