KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > ExplorerManagerTest


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;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.VetoableChangeListener JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.nodes.AbstractNode;
28 import org.openide.nodes.Children;
29 import org.openide.nodes.Node;
30
31 /**
32  *
33  * @author Jaroslav Tulach
34  */

35 public class ExplorerManagerTest extends NbTestCase
36         implements PropertyChangeListener JavaDoc {
37     private ExplorerManager em;
38     private Keys keys;
39     private Node root;
40     private LinkedList JavaDoc events;
41     
42     public ExplorerManagerTest(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     /** This code is supposed to run in AWT test.
47      */

48     protected boolean runInEQ() {
49         return true;
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         em = new ExplorerManager();
54         keys = new Keys();
55         root = new AbstractNode(keys);
56         Node[] justAsk = root.getChildren().getNodes(true);
57         em.setRootContext(root);
58         events = new LinkedList JavaDoc();
59     }
60     
61     public void propertyChange(PropertyChangeEvent JavaDoc ev) {
62         assertFalse("No read lock held", Children.MUTEX.isReadAccess());
63         assertFalse("No write lock held", Children.MUTEX.isWriteAccess());
64         
65         events.add(ev);
66     }
67     
68     public void testNormalSelectionChange() throws Exception JavaDoc {
69         final Node a = keys.key("a key");
70         
71         em.addPropertyChangeListener(this);
72         
73         em.setSelectedNodes(new Node[] { a });
74         Node[] arr = em.getSelectedNodes();
75         assertEquals("One selected", 1, arr.length);
76         assertEquals("A is there", a, arr[0]);
77         
78         
79         assertEquals("One event", 1, events.size());
80         PropertyChangeEvent JavaDoc ev = (PropertyChangeEvent JavaDoc)events.removeFirst();
81         assertEquals("Name is good", ExplorerManager.PROP_SELECTED_NODES, ev.getPropertyName());
82
83         events.clear();
84         em.setSelectedNodes(new Node[] { a });
85         assertEquals("No change: " + events, 0, events.size());
86     }
87     
88     public void testCannotSetNodesNotUnderTheRoot() throws Exception JavaDoc {
89         final Node a = new AbstractNode(Children.LEAF);
90         
91         try {
92             em.setSelectedNodes(new Node[] { a });
93             fail("Should throw IllegalArgumentException as the node is not under root");
94         } catch (IllegalArgumentException JavaDoc ex) {
95             // ok, a is not under root
96
}
97     }
98     
99     
100     public void testSetNodesSurviveChangeOfNodes() throws Exception JavaDoc {
101         final Node a = keys.key("toRemove");
102         
103         class ChangeTheSelectionInMiddleOfMethod implements VetoableChangeListener JavaDoc {
104             public int cnt;
105             
106             public void vetoableChange(PropertyChangeEvent JavaDoc evt) {
107                 cnt++;
108                 keys.keys(new String JavaDoc[0]);
109             }
110         }
111         
112         ChangeTheSelectionInMiddleOfMethod list = new ChangeTheSelectionInMiddleOfMethod();
113         em.addVetoableChangeListener(list);
114         
115         em.setSelectedNodes(new Node[] { a });
116         
117         assertEquals("Vetoable listener called", 1, list.cnt);
118         assertEquals("Node is dead", null, a.getParentNode());
119         
120         // handling of removed nodes is done asynchronously
121
em.waitFinished();
122         
123         Node[] arr = em.getSelectedNodes();
124         assertEquals("No nodes can be selected", 0, arr.length);
125     }
126     
127     public void testCannotVetoSetToEmptySelection() throws Exception JavaDoc {
128         final Node a = keys.key("toRemove");
129         
130         em.setSelectedNodes(new Node[] { a });
131         
132         class NeverCalledVeto implements VetoableChangeListener JavaDoc {
133             public int cnt;
134             
135             public void vetoableChange(PropertyChangeEvent JavaDoc evt) {
136                 cnt++;
137                 keys.keys(new String JavaDoc[0]);
138             }
139         }
140         
141         NeverCalledVeto list = new NeverCalledVeto();
142         em.addVetoableChangeListener(list);
143         
144         em.setSelectedNodes(new Node[0]);
145         
146         assertEquals("Veto not called", 0, list.cnt);
147         Node[] arr = em.getSelectedNodes();
148         assertEquals("No nodes can be selected", 0, arr.length);
149     }
150     
151     private static final class Keys extends Children.Keys {
152         public Node key(String JavaDoc k) {
153             keys(new String JavaDoc[] { k });
154             return getNodes()[0];
155         }
156         public void keys(String JavaDoc[] keys) {
157             super.setKeys(keys);
158         }
159         protected Node[] createNodes(Object JavaDoc o) {
160             AbstractNode an = new AbstractNode(Children.LEAF);
161             an.setName((String JavaDoc)o);
162             return new Node[] { an };
163         }
164     }
165 }
166
Popular Tags