KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.*;
25
26 import junit.textui.TestRunner;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29
30
31
32 /**
33  * @author Some Czech
34  */

35 public class SheetTest extends NbTestCase {
36
37     public SheetTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] args) {
42         TestRunner.run(new NbTestSuite(SheetTest.class));
43     }
44
45     public void testSheetEvents() {
46
47         AbstractNode node = new AbstractNode( Children.LEAF );
48         
49         Sheet sheet = node.getSheet();
50         
51         SheetListener sl = new SheetListener();
52         TestNodeListener tnl = new TestNodeListener();
53         
54         node.addNodeListener( tnl );
55         node.addPropertyChangeListener( sl );
56                 
57         Sheet.Set ss = new Sheet.Set();
58         ss.setName("Karel");
59         sheet.put( ss );
60         
61         tnl.assertEvents( "NodePropertySets change", new PropertyChangeEvent JavaDoc[] {
62             new PropertyChangeEvent JavaDoc( node, Node.PROP_PROPERTY_SETS, null, null )
63         } );
64         
65         sl.assertEvents( "No events", new PropertyChangeEvent JavaDoc[] {} );
66         
67         PropertySupport.Name prop = new PropertySupport.Name (node);
68         ss.put (prop);
69         
70         tnl.assertEvents( "NodePropertySets change again", new PropertyChangeEvent JavaDoc[] {
71             new PropertyChangeEvent JavaDoc( node, Node.PROP_PROPERTY_SETS, null, null )
72         } );
73         
74         sl.assertEvents( "No events fired", new PropertyChangeEvent JavaDoc[] {} );
75         
76         sheet.remove(ss.getName());
77         
78         tnl.assertEvents( "NodePropertySets change", new PropertyChangeEvent JavaDoc[] {
79             new PropertyChangeEvent JavaDoc( node, Node.PROP_PROPERTY_SETS, null, null )
80         } );
81         
82         sl.assertEvents( "No events", new PropertyChangeEvent JavaDoc[] {} );
83         
84         ss.remove (prop.getName());
85         
86         tnl.assertEvents( "No change in Node, as the set is removed", new PropertyChangeEvent JavaDoc[] {} );
87         sl.assertEvents( "No events fired", new PropertyChangeEvent JavaDoc[] {} );
88     }
89     
90     
91     private static class SheetListener implements PropertyChangeListener JavaDoc {
92         
93         List events = new ArrayList();
94         
95         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
96             events.add( evt );
97         }
98         
99         public void assertEvents( String JavaDoc message, PropertyChangeEvent JavaDoc[] pevents ) {
100             
101             if ( events.size() != pevents.length ) {
102                 fail( message );
103             }
104             
105             int i = 0;
106             for( Iterator it = events.iterator(); it.hasNext(); i++ ) {
107                 PropertyChangeEvent JavaDoc pche = (PropertyChangeEvent JavaDoc)it.next();
108                 assertEquals( message + " [" + i + "] ", pevents[i].getSource(), pche.getSource ());
109                 assertEquals( message + " [" + i + "] ", pevents[i].getPropertyName(), pche.getPropertyName());
110                 assertEquals( message + " [" + i + "] ", pevents[i].getOldValue (), pche.getOldValue());
111                 assertEquals( message + " [" + i + "] ", pevents[i].getNewValue(), pche.getNewValue ());
112                 assertEquals( message + " [" + i + "] ", pevents[i].getPropagationId(), pche.getPropagationId());
113             }
114             
115             events.clear();
116         }
117         
118     }
119     
120     private static class TestNodeListener extends SheetListener implements NodeListener {
121         
122         public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) {
123         }
124         
125         public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) {
126         }
127         
128         public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) {
129         }
130         
131         public void nodeDestroyed(org.openide.nodes.NodeEvent ev) {
132         }
133         
134     }
135
136 }
137
Popular Tags