KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > HierarchyTest


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.api.nodes2looks;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import org.openide.util.Lookup;
24 import org.openide.util.io.NbMarshalledObject;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28 import org.openide.nodes.Node;
29 import org.openide.nodes.Node.Handle;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.spi.looks.Look;
32 import org.netbeans.spi.looks.Looks;
33 import org.netbeans.spi.looks.Selectors;
34
35 public class HierarchyTest extends NbTestCase {
36
37     private Node node;
38
39     public HierarchyTest(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     protected void setUp() throws Exception JavaDoc {
44         
45         ArrayList JavaDoc l1 = new ArrayList JavaDoc ();
46         
47         ArrayList JavaDoc l2 = new ArrayList JavaDoc ();
48         l2.add ("1");
49         l2.add ("2");
50         
51         l1.add (l2);
52         
53         l2 = new ArrayList JavaDoc ();
54         l2.add ("A");
55         l2.add ("B");
56         
57         l1.add (l2);
58         node = createLookNode (l1, null);
59         
60     }
61                  
62     private static Node createLookNode(final Object JavaDoc obj, Handle handle) {
63         if (handle == null) {
64             handle = new TestHandle (obj);
65         }
66         
67         return Nodes.node (obj,
68             Looks.childrenSelectorProvider(
69                 "Root_JLD",
70                 CL.FIRST_CL,
71                 Selectors.array( new Look[] { CL.FIRST_CL, CL.SECOND_CL, CL.FIRST, CL.SECOND } ) ),
72             
73             Selectors.array( new Look[] { CL.FIRST_CL } ),
74             handle
75         );
76                 
77     }
78     
79     protected void tearDown() throws Exception JavaDoc {
80         node = null;
81     }
82     
83     /** Checks the hierarchy of nodes.
84      */

85     public void testHierarchy () {
86         checkLook (node.getChildren().getNodes(), CL.FIRST_CL);
87     }
88     
89     /** Tests the look on clone.
90      */

91     public void testClonedHierarchy () {
92         checkLook (node.cloneNode().getChildren().getNodes(), CL.FIRST_CL);
93     }
94     
95     /** Changes the look and check the result.
96      */

97     public void testChangedLookOnHierarchy () {
98         Node n = node.cloneNode ();
99         
100         TestUtil.setLook (n, CL.SECOND_CL);
101         
102         assertEquals ("Look must change ", CL.SECOND_CL, TestUtil.getLook( n ));
103         // have not changed look selector for children
104
checkLook (n.getChildren ().getNodes (), CL.FIRST_CL);
105         
106         TestUtil.setLook ( n, CL.SECOND );
107         assertEquals ("LookSelector must change ", CL.SECOND, TestUtil.getLook( n ));
108         checkLook (n, CL.SECOND_CL);
109
110     }
111         
112     /** Selects a look on a subnode, waits till the node is garbage collected
113      * and checks whether the look stays there.
114      */

115     public void testLookSelector () throws Exception JavaDoc {
116         
117         Node n = node.cloneNode ();
118         Node[] arr = n.getChildren ().getNodes ();
119                 
120         Node ln = arr[0];
121                 
122         TestUtil.setLook( ln, CL.SECOND );
123         assertEquals( "Look should change. ", CL.SECOND, TestUtil.getLook( ln ) );
124         checkLook ( ln.getChildren().getNodes(), CL.SECOND_CL);
125         
126         WeakReference JavaDoc ref = new WeakReference JavaDoc (ln);
127         
128         ln = null;
129         arr = null;
130         
131         assertGC ("The node must disapear", ref);
132         
133         arr = n.getChildren ().getNodes ();
134                 
135         ln = arr[0];
136         
137         assertEquals ("Look should remain changed", CL.SECOND, TestUtil.getLook( ln ) );
138         checkLook (ln.getChildren().getNodes(), CL.SECOND_CL);
139         
140     }
141     
142     
143     /** Test persistence of change of look descriptor.
144      */

145     public void testLookSelectorSerialization () throws Exception JavaDoc {
146         
147         Node n = node.cloneNode ();
148         Node[] arr = n.getChildren ().getNodes ();
149         
150         
151         Node ln = arr[0];
152                 
153         TestUtil.setLook( ln, CL.SECOND );
154         assertEquals ("Look should change", CL.SECOND, TestUtil.getLook( ln ) );
155         checkLook (ln.getChildren().getNodes(), CL.SECOND_CL);
156         
157         Object JavaDoc oldRO = TestUtil.getRepresentedObject( n );
158         
159         Handle h = n.getHandle ();
160         assertNotNull ("Every node has a handle", h);
161         h = (Handle)new NbMarshalledObject (h).get ();
162         n = h.getNode ();
163         
164         assertEquals ("Represented object remains equal", oldRO, TestUtil.getRepresentedObject ( n ));
165         assertTrue ("but not the same (deserialized)", oldRO != TestUtil.getRepresentedObject ( n ));
166         
167         arr = n.getChildren ().getNodes ();
168         
169         
170         ln = arr[0];
171         
172         assertEquals ("Look should remain changed", CL.SECOND, TestUtil.getLook( ln ) );
173         checkLook (ln.getChildren().getNodes(), CL.SECOND_CL);
174         
175     }
176         
177         
178     /** Checks the look */
179         
180     private static void checkLook (Node from, Look look) {
181         checkLook (from.getChildren ().getNodes (), look);
182     }
183     
184     private static void checkLook (Node[] arr, Look look) {
185         for (int i = 0; i < arr.length; i++) {
186             Node l = arr[i];
187          
188             assertEquals ("Inherits the look", TestUtil.getLook( l ), look);
189             
190             checkLook (l, look);
191         }
192     }
193      
194        
195     /** A look that represents java.util.Collection.
196      */

197    
198     private static final class CL extends Look {
199         
200         // We want two Looks which:
201
// 1. change the look to itself
202
// 2. do this change fro the whole hierarchy
203

204         private static final CL FIRST_CL = new CL ("First_LOOK");
205         private static final CL SECOND_CL = new CL ("Second_LOOK");
206         
207         public static final Look FIRST = Looks.childrenSelectorProvider(
208             "First_JLD",
209             FIRST_CL,
210             Selectors.array( new CL[] { FIRST_CL, SECOND_CL } ) );
211             
212         public static final Look SECOND = Looks.childrenSelectorProvider(
213             "Second_JLD",
214             SECOND_CL,
215             Selectors.array( new CL[] { SECOND_CL, FIRST_CL } ) );
216         
217         
218         private CL (String JavaDoc name) {
219             super (name);
220         }
221         
222         public boolean isLeaf ( Object JavaDoc representedObject, Lookup env ) {
223             return ! (representedObject instanceof List JavaDoc);
224         }
225                 
226         public java.util.List JavaDoc getChildObjects( Object JavaDoc representedObject, Lookup env ) {
227             if ("Empty".equals (getName ())) return null;
228             
229             if (representedObject instanceof Collection JavaDoc) {
230                 Collection JavaDoc c = (Collection JavaDoc)representedObject;
231                 return new ArrayList JavaDoc( c );
232             } else {
233                 return null;
234             }
235         }
236                 
237         public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
238             return representedObject.toString();
239         }
240         
241         public String JavaDoc toString () {
242             return "CL[" + getName () + "]";
243         }
244     }
245     
246     static class TestHandle implements Handle {
247         static final long serialVersionUID = 4503853940L;
248         
249         private Object JavaDoc x;
250
251         public TestHandle (Object JavaDoc x) {
252             this.x = x;
253         }
254
255         public Node getNode () {
256             return createLookNode (x, this);
257         }
258     }
259    
260    
261 }
262
Popular Tags