KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > BeanNodeTest


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.openide.nodes;
21
22 import java.beans.*;
23 import java.util.*;
24
25 import junit.textui.TestRunner;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.junit.NbTestSuite;
28
29 import org.openide.nodes.*;
30 import org.openide.util.lookup.Lookups;
31
32 /** Test updating of bean children in proper circumstances, e.g.
33  * deleting nodes or beans.
34  * @author Jesse Glick
35  */

36 public class BeanNodeTest extends NbTestCase {
37
38     public BeanNodeTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public static void main(String JavaDoc[] args) {
43         TestRunner.run(new NbTestSuite(BeanNodeTest.class));
44     }
45     
46     /** Tests that basic introspected properties of the bean are reflected
47      * as node properties.
48      */

49     public void testBasicBeanProperties() throws Exception JavaDoc {
50         Bean1 b = new Bean1();
51         assertEquals("hello", b.getName());
52         BeanNode n = new BeanNode(b);
53         assertEquals("hello", n.getName());
54         n.setName("hi");
55         assertEquals("hi", b.getName());
56         assertEquals("hi", n.getName());
57         Node.PropertySet[] propsets = n.getPropertySets();
58         assertEquals(1, propsets.length);
59         assertEquals(Sheet.PROPERTIES, propsets[0].getName());
60         Node.Property[] props = propsets[0].getProperties();
61         Node.Property prop = null;
62         // Will have just one prop, 'foo'. OK to also have 'name' but we ignore it if so.
63
for (int i = 0; i < props.length; i++) {
64             if (props[i].getName().equals("foo") && prop == null) {
65                 prop = props[i];
66             } else {
67                 assertEquals("name", props[i].getName());
68             }
69         }
70         assertNotNull(prop);
71         assertEquals("Foo", prop.getDisplayName());
72         assertEquals("The foo.", prop.getShortDescription());
73         assertEquals(new Integer JavaDoc(0), prop.getValue());
74         b.setFoo(1);
75         assertEquals(new Integer JavaDoc(1), prop.getValue());
76         WaitPCL l2 = new WaitPCL("foo");
77         n.addPropertyChangeListener(l2);
78         b.setFoo(2);
79         assertTrue("Calling a bean setter fires a Node.Property value change", l2.changed());
80         assertEquals(new Integer JavaDoc(2), prop.getValue());
81         prop.setValue(new Integer JavaDoc(3));
82         assertEquals(new Integer JavaDoc(3), prop.getValue());
83         assertEquals(new Integer JavaDoc(3), new Integer JavaDoc(b.getFoo()));
84         NL l1 = new NL();
85         n.addNodeListener(l1);
86         b.setName("newname");
87         assertTrue("Calling bean's name setter fires Node.PROPERTY_NAME", l1.changed());
88     }
89     
90     /** Test that beans extending a private superclass are still usable.
91      * No methods defined in the private superclass will be usable, so
92      * they may be skipped if found by the introspector.
93      * @see "#24767"
94      */

95     public void testPrivateInheritance() throws Exception JavaDoc {
96         Bean2 b = new Bean2();
97         BeanNode n = new BeanNode(b);
98         Node.PropertySet[] propsets = n.getPropertySets();
99         assertEquals(1, propsets.length);
100         assertEquals(Sheet.PROPERTIES, propsets[0].getName());
101         Node.Property[] props = propsets[0].getProperties();
102         Node.Property prop = null;
103         for (int i = 0; i < props.length; i++) {
104             if (props[i].getName().equals("yaya") && prop == null) {
105                 prop = props[i];
106             } else if (props[i].getName().equals("class")) {
107                 // OK, useless prop, ignore it
108
} else if (props[i].getName().equals("foo")) {
109                 // OK, not required to be here, but can be
110
// Cf. comment in BeanNode, and code in PropertySupport.Reflection
111
// But we must be able to get the value!
112
if (props[i].canRead()) {
113                     assertEquals("hello", props[i].getValue());
114                 }
115             } else {
116                 assertEquals("name", props[i].getName());
117             }
118         }
119         assertNotNull(prop);
120         assertEquals(new Integer JavaDoc(5), prop.getValue());
121         b.setYaya(3);
122         assertEquals(new Integer JavaDoc(3), prop.getValue());
123         // no foo, no listener
124
}
125     
126     public void testNoFirindOfHiddenProperties () throws Exception JavaDoc {
127         HidenPropertyBean bean = new HidenPropertyBean ();
128         
129         
130         BeanNode node = new BeanNode (bean);
131         
132         Node.PropertySet[] arr = node.getPropertySets ();
133         int cnt = 0;
134         for (int i = 0; i < arr.length; i++) {
135             cnt += arr[i].getProperties ().length;
136         }
137         assertEquals ("Bean node does not show hidden properties", 0, cnt);
138         
139         WaitPCL pcl = new WaitPCL (null);
140         node.addPropertyChangeListener (pcl);
141         
142         bean.setHiddenProperty (11);
143         
144         assertFalse ("No change should be notified", pcl.changed ());
145     }
146     
147     public void testLookupAndBean() throws Exception JavaDoc {
148         class MyClass {
149         }
150
151         MyClass inst = new MyClass();
152
153         Bean1 b = new Bean1();
154         BeanNode n = new BeanNode(b, null, Lookups.singleton(inst));
155
156         // my lookup is propagated
157
MyClass tst = n.getLookup().lookup(MyClass.class);
158         assertSame(inst, tst);
159     }
160
161     public void testCanUseCookieSetWithoutLookup() throws Exception JavaDoc {
162         class MyBeanNode extends BeanNode {
163             MyBeanNode(Object JavaDoc bean) throws IntrospectionException {
164                 super(bean, null, null);
165                 getCookieSet();
166             }
167         };
168
169         new MyBeanNode(new Bean1());
170     }
171     
172     // XXX test synchronizeName
173

174     public static final class Bean1 {
175         
176         private String JavaDoc name = "hello";
177         private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
178         private int foo = 0;
179         
180         public void addPropertyChangeListener(PropertyChangeListener l) {
181             propertyChangeSupport.addPropertyChangeListener(l);
182         }
183         public void removePropertyChangeListener(PropertyChangeListener l) {
184             propertyChangeSupport.removePropertyChangeListener(l);
185         }
186         
187         public String JavaDoc getName() {
188             return this.name;
189         }
190         public void setName(String JavaDoc name) {
191             String JavaDoc oldName = this.name;
192             this.name = name;
193             propertyChangeSupport.firePropertyChange("name", oldName, name);
194         }
195         
196         public int getFoo() {
197             return this.foo;
198         }
199         public void setFoo(int foo) {
200             int oldFoo = this.foo;
201             this.foo = foo;
202             propertyChangeSupport.firePropertyChange("foo", new Integer JavaDoc(oldFoo), new Integer JavaDoc(foo));
203         }
204         
205     }
206     
207     public static final class Bean1BeanInfo extends SimpleBeanInfo {
208         public PropertyDescriptor[] getPropertyDescriptors() {
209             try {
210                 PropertyDescriptor foo = new PropertyDescriptor("foo", Bean1.class);
211                 foo.setDisplayName("Foo");
212                 foo.setShortDescription("The foo.");
213                 return new PropertyDescriptor[] {foo};
214             } catch (IntrospectionException ie) {
215                 ie.printStackTrace();
216                 return null;
217             }
218         }
219     }
220     
221     private static class Bean2S {
222         
223         private String JavaDoc foo = "hello";
224         protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
225         
226         public void addPropertyChangeListener(PropertyChangeListener l) {
227             propertyChangeSupport.addPropertyChangeListener(l);
228         }
229         public void removePropertyChangeListener(PropertyChangeListener l) {
230             propertyChangeSupport.removePropertyChangeListener(l);
231         }
232         
233         public String JavaDoc getFoo() {
234             return this.foo;
235         }
236         public void setFoo(String JavaDoc foo) {
237             String JavaDoc oldFoo = this.foo;
238             this.foo = foo;
239             propertyChangeSupport.firePropertyChange("foo", oldFoo, foo);
240         }
241         
242     }
243     
244     public static final class Bean2 extends Bean2S {
245         
246         private int yaya = 5;
247         
248         public int getYaya() {
249             return this.yaya;
250         }
251         public void setYaya(int yaya) {
252             int oldYaya = this.yaya;
253             this.yaya = yaya;
254             propertyChangeSupport.firePropertyChange("yaya", new Integer JavaDoc(oldYaya), new Integer JavaDoc(yaya));
255         }
256         
257     }
258     
259     /** Prop listener that will tell you if it gets a change.
260      */

261     private static class WaitPCL implements PropertyChangeListener {
262         
263         /** whether a change has been received, and if so count */
264         public int gotit = 0;
265         
266         /** optional property name to filter by (if null, accept any) */
267         private final String JavaDoc prop;
268         
269         public WaitPCL(String JavaDoc p) {
270             prop = p;
271         }
272         
273         public synchronized void propertyChange(PropertyChangeEvent evt) {
274             if (prop == null || prop.equals(evt.getPropertyName())) {
275                 gotit++;
276                 notifyAll();
277             }
278         }
279         
280         public boolean changed() {
281             return changed(1500);
282         }
283         
284         public synchronized boolean changed(int timeout) {
285             if (gotit > 0) {
286                 return true;
287             }
288             try {
289                 wait(timeout);
290             } catch (InterruptedException JavaDoc ie) {
291                 ie.printStackTrace();
292             }
293             return gotit > 0;
294         }
295         
296     }
297     
298     private static class NL extends WaitPCL implements NodeListener {
299         public NL() {
300             super(Node.PROP_NAME);
301         }
302         public void childrenAdded(NodeMemberEvent ev) {}
303         public void childrenRemoved(NodeMemberEvent ev) {}
304         public void childrenReordered(NodeReorderEvent ev) {}
305         public void nodeDestroyed(NodeEvent ev) {}
306     }
307
308     public static class HidenPropertyBean {
309         private int hiddenProperty;
310         private PropertyChangeSupport pcs;
311
312         /** Creates a new instance of Model */
313         public HidenPropertyBean () {
314             pcs = new PropertyChangeSupport(this);
315             hiddenProperty = -1;
316         }
317
318         public void addPropertyChangeListener(PropertyChangeListener l) {
319             pcs.addPropertyChangeListener(l);
320         }
321
322         public void removePropertyChangeListener(PropertyChangeListener l) {
323             pcs.removePropertyChangeListener(l);
324         }
325
326         public int getHiddenProperty() {
327             return hiddenProperty;
328         }
329
330         public void setHiddenProperty(int value) {
331             this.hiddenProperty = value;
332             pcs.firePropertyChange("hiddenProperty", null, null);
333         }
334     }
335     
336     public static class HidenPropertyBeanBeanInfo extends SimpleBeanInfo {
337         public PropertyDescriptor[] getPropertyDescriptors () {
338             PropertyDescriptor[] properties = new PropertyDescriptor[1];
339
340             try {
341                 properties[0] = new PropertyDescriptor ( "hiddenProperty", HidenPropertyBean.class, "getHiddenProperty", "setHiddenProperty" );
342                 properties[0].setHidden ( true );
343             }
344             catch( IntrospectionException e) {}
345
346             // Here you can add code for customizing the properties array.
347

348             return properties;
349         }
350     }
351     
352 }
353
Popular Tags