KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > ProxyNodeTest


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.explorer.propertysheet;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.openide.nodes.AbstractNode;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.Node;
29 import org.openide.nodes.NodeEvent;
30 import org.openide.nodes.NodeListener;
31 import org.openide.nodes.NodeMemberEvent;
32 import org.openide.nodes.NodeReorderEvent;
33 import org.openide.nodes.PropertySupport;
34 import org.openide.nodes.Sheet;
35
36 // This test class tests the main functionality of the property sheet
37
public class ProxyNodeTest extends NbTestCase {
38     public ProxyNodeTest(String JavaDoc name) {
39         super(name);
40     }
41     
42     protected boolean runInEQ() {
43         return false;
44     }
45     
46     
47     ProxyNode pn = null;
48     TNode a = null;
49     TNode b = null;
50     TProperty pa = null;
51     TProperty pb = null;
52     ProxyNodeListener nodeL = null;
53     ProxyNodePCL pcl = null;
54     protected void setUp() throws Exception JavaDoc {
55         pa = new TProperty();
56         pb = new TProperty();
57         a = new TNode(pa);
58         b = new TNode(pb);
59         pn = new ProxyNode(new Node[] {a,b});
60         nodeL = new ProxyNodeListener();
61         pcl = new ProxyNodePCL();
62         pn.addNodeListener(nodeL);
63         pn.addPropertyChangeListener(pcl);
64     }
65     
66     private Node.Property findProxyProperty() {
67         return pn.getPropertySets()[0].getProperties()[0];
68     }
69     
70     public void testProxyPropertyCreated() {
71         assertNotNull("Proxied properties each have one property, but couldn't find matching ProxyProperty", findProxyProperty());
72     }
73     
74     public void testProxyPropertyReflectsValueChanges() throws Exception JavaDoc {
75         Object JavaDoc o = findProxyProperty().getValue();
76         String JavaDoc name = findProxyProperty().getName();
77         pa.setValue("Foo");
78         nodeL.assertNoEvent("Setting a property value should not fire to NodeListeners");
79         pcl.assertFired("Property change should have been fired on " + pa.getName(), pa.getName());
80     }
81     
82     public void testProxyPropertyHandlesNodeDeletion() {
83         a.destroyMe();
84         nodeL.assertNotDestroyed();
85         b.destroyMe();
86         nodeL.assertNodeDestroyedFired("Destroying both nodes represented by a proxy node should make it fire NodeDestroyed");
87     }
88     
89     public void testDisplayNameNotFiredToNodeListeners() {
90         System.err.println("First display name " + pn.getDisplayName());
91         a.setDisplayName("New name");
92         System.err.println("Second display name " + pn.getDisplayName());
93         nodeL.assertFired("Setting display name on a proxied node should trigger a property change event", Node.PROP_DISPLAY_NAME);
94         pcl.assertNoEvent("Setting display name should not cause an event on NodeListeners");
95     }
96     
97     public void testCookieChangeGoesToTheCorrectListenerOnly() {
98         a.fireCookies();
99         nodeL.assertFired("One node firing prop cookies should cause an event to node listeners", Node.PROP_COOKIE);
100         pcl.assertNoEvent("No event should have been fired to a property change listener on a cookies change");
101     }
102     
103     
104     public class ProxyNodeListener implements NodeListener {
105         public void childrenAdded(NodeMemberEvent ev) {
106         }
107         
108         public void childrenRemoved(NodeMemberEvent ev) {
109         }
110         
111         public void childrenReordered(NodeReorderEvent ev) {
112         }
113         
114         NodeEvent ev = null;
115         public void nodeDestroyed(NodeEvent ev) {
116             this.ev = ev;
117         }
118         
119         public void assertNotDestroyed() {
120             assertNull(ev);
121         }
122         
123         public void assertNodeDestroyedFired(String JavaDoc msg) {
124             assertNotNull(msg + " - NodeDestroyed not fired", ev);
125         }
126         
127         private PropertyChangeEvent JavaDoc evt = null;
128         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
129             this.evt = evt;
130         }
131         
132         public void assertNoEvent(String JavaDoc msg) {
133             assertNull(msg + "- event was fired to NodeListener but should not have been - " + (evt != null ? evt.getPropertyName() : ""), evt);
134             assertNull(msg + "- no node event should have been generated, but one was " + ev, ev);
135         }
136         
137         public void assertFired(String JavaDoc msg, String JavaDoc propName) {
138             assertTrue(msg + "- event was not fired to NodeListener but should have been", evt != null && propName.equals(evt.getPropertyName()));
139         }
140     }
141     
142     public class ProxyNodePCL implements PropertyChangeListener JavaDoc {
143         private PropertyChangeEvent JavaDoc evt = null;
144         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
145             this.evt = evt;
146         }
147         
148         public void assertNoEvent(String JavaDoc msg) {
149             assertNull(msg + "- event was fired to NodeListener but should not have been", evt);
150         }
151         
152         public void assertFired(String JavaDoc msg, String JavaDoc propName) {
153             assertTrue(msg + "- event was not fired to PropertyChangeListener but should have been", evt != null && propName.equals(evt.getPropertyName()));
154         }
155     }
156     
157     //Node definition
158
public class TNode extends AbstractNode {
159         private TProperty myprop;
160         //create Node
161
public TNode(TProperty myprop) {
162             super(Children.LEAF);
163             setName("TNode");
164             setDisplayName("TNode");
165             this.myprop = myprop;
166         }
167         //clone existing Node
168
public Node cloneNode() {
169             return new TNode(myprop);
170         }
171         
172         // Create a property sheet:
173
protected Sheet createSheet() {
174             Sheet sheet = super.createSheet();
175             // Make sure there is a "Properties" set:
176
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
177             if (props == null) {
178                 props = Sheet.createPropertiesSet();
179                 sheet.put(props);
180             }
181             props.put(myprop);
182             myprop.setFiringNode(this);
183             return sheet;
184         }
185         // Method firing changes
186
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
187             firePropertyChange(s,o1,o2);
188         }
189         
190         public void setDisplayName(String JavaDoc s) {
191             String JavaDoc old = super.getDisplayName();
192             super.setDisplayName(s);
193             fireDisplayNameChange(old, s);
194         }
195         
196         public void destroyMe() {
197             fireNodeDestroyed();
198         }
199         
200         public void fireCookies() {
201             fireCookieChange();
202         }
203     }
204     
205     // Property definition
206
public class TProperty extends PropertySupport {
207         private Object JavaDoc myValue = "Value";
208         // Create new Property
209
public TProperty() {
210             super("TProperty", String JavaDoc.class, "TProperty", "", true, true);
211         }
212         // get property value
213
public Object JavaDoc getValue() {
214             return myValue;
215         }
216         private TNode node = null;
217         void setFiringNode(TNode n) {
218             this.node = n;
219         }
220         
221         // set property value
222
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
223             System.err.println("TProperty setValue: " + value);
224             Object JavaDoc oldVal = myValue;
225             myValue = value;
226             System.err.println("TProperty triggering node property change");
227             if (node != null) {
228                 node.fireMethod(getName(), oldVal, myValue);
229             }
230         }
231     }
232     
233 }
234
Popular Tags