KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > viewmodel > BasicTest


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.modules.viewmodel;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30 import javax.swing.AbstractAction JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.modules.viewmodel.TreeModelNode;
34 import org.netbeans.modules.viewmodel.TreeModelRoot;
35 import org.netbeans.modules.viewmodel.TreeTable;
36 import org.netbeans.spi.viewmodel.*;
37 import org.openide.nodes.Node;
38 import org.openide.util.RequestProcessor;
39
40
41
42 /**
43  * Tests the JPDABreakpointEvent.resume() functionality.
44  *
45  * @author Maros Sandor, Jan Jancura
46  */

47 public class BasicTest extends NbTestCase {
48
49     private String JavaDoc helpID = "A test help ID"; // NOI18N
50

51     public BasicTest (String JavaDoc s) {
52         super (s);
53     }
54
55     public void testBasic () throws Exception JavaDoc {
56         ArrayList JavaDoc l = new ArrayList JavaDoc ();
57         CompoundModel cm = new CompoundModel ();
58         l.add (cm);
59         Models.CompoundModel mcm = Models.createCompoundModel(l, helpID);
60         TreeTable tt = (TreeTable) Models.createView(mcm);
61         waitFinished ();
62         Node n = tt.getExplorerManager ().
63             getRootContext ();
64         checkNode (n, "");
65         if (cm.exception != null)
66             cm.exception.printStackTrace ();
67         assertNull ("Threading problem", cm.exception);
68         // TODO: Expansion test does not work - probably written in a bad way...
69
//assertEquals ("nodeExpanded notification number", 3, cm.expandedTest.size ());
70
//assertEquals ("nodeExpanded ", cm.toBeExpandedTest, cm.expandedTest);
71
assertEquals(n.getValue("propertiesHelpID"), helpID);
72     }
73     
74     private void checkNode (Node n, String JavaDoc name) {
75         // init
76
//assertEquals (null, n.getShortDescription ());
77
Node[] ns = n.getChildren ().getNodes ();
78         waitFinished ();
79         
80         ns = n.getChildren ().getNodes ();
81         if (name.length () < 4) {
82             assertEquals (name, 3, ns.length);
83             checkNode (ns [0], name + "a");
84             checkNode (ns [1], name + "b");
85             checkNode (ns [2], name + "c");
86         } else
87             assertEquals (ns.length, 0);
88         
89         if (name.length () > 0) {
90             //assertEquals (name, n.getName ());
91
n.getDisplayName ();
92             String JavaDoc sd = n.getShortDescription ();
93             n.getActions (false);
94             waitFinished ();
95             assertEquals (name, n.getDisplayName ());
96             assertEquals (name + "WWW", sd);
97             assertEquals (1, n.getActions (false).length);
98         }
99     }
100
101     static void waitFinished () {
102         TreeModelNode.getRequestProcessor ().post (new Runnable JavaDoc () {
103             public void run () {}
104         }).waitFinished ();
105     }
106     
107     
108     public void testMnemonics() throws Exception JavaDoc {
109         ArrayList JavaDoc l = new ArrayList JavaDoc ();
110         CompoundModel cm = new CompoundModel ();
111         l.add (cm);
112         TestColumnModel tcm = new TestColumnModel();
113         l.add(tcm);
114         Models.CompoundModel mcm = Models.createCompoundModel(l);
115         TreeTable tt = (TreeTable) Models.createView(mcm);
116         Node.Property[] columns = tt.columns;
117         assertEquals(2, columns.length);
118         assertEquals(new Character JavaDoc('e'), columns[1].getValue("ColumnMnemonicCharTTV"));
119     }
120     
121     public static class CompoundModel implements TreeModel,
122     NodeModel, NodeActionsProvider, TableModel, TreeExpansionModel {
123
124     
125         private Vector JavaDoc listeners = new Vector JavaDoc ();
126         
127         private Throwable JavaDoc exception;
128
129         private Map JavaDoc callNumbers = new HashMap JavaDoc ();
130         protected synchronized void addCall (String JavaDoc methodName, Object JavaDoc node) {
131             Map JavaDoc m = (Map JavaDoc) callNumbers.get (methodName);
132             if (m == null)
133                 callNumbers.put (methodName, m = new HashMap JavaDoc ());
134             if (m.containsKey (node)) {
135                 Object JavaDoc info = m.get(node);
136                 if (info instanceof Exception JavaDoc) {
137                     System.err.println ("Second call of " + methodName + " method for the same node " + node);
138                     System.err.println("First was at:");
139                     ((Exception JavaDoc) info).printStackTrace();
140                     System.err.println("Second is:");
141                     Thread.dumpStack();
142                     m.put (node, new Integer JavaDoc(2));
143                 } else {
144                     int numCalls = ((Integer JavaDoc) info).intValue() + 1;
145                     System.err.println (numCalls+". call of " + methodName + " method for the same node " + node);
146                     Thread.dumpStack();
147                     m.put (node, new Integer JavaDoc(numCalls));
148                 }
149             } else {
150                 m.put (node, new Exception JavaDoc());
151             }
152         }
153
154         void checkThread () {
155             try {
156                 assertTrue ("The right thread", TreeModelNode.getRequestProcessor ().isRequestProcessorThread ());
157             } catch (Throwable JavaDoc t) {
158                 exception = t;
159             }
160             /*;
161             Thread t = Thread.currentThread ();
162             if ( t.getClass ().getName ().startsWith
163                     (RequestProcessor.class.getName ())
164             ) exception = new Exception ();
165              */

166         }
167
168         // TreeModel ...............................................................
169

170         /**
171          * Returns the root node of the tree or null, if the tree is empty.
172          *
173          * @return the root node of the tree or null
174          */

175         public Object JavaDoc getRoot () {
176             addCall ("getRoot", null);
177             return ROOT;
178         }
179
180         /**
181          * Returns children for given parent on given indexes.
182          *
183          * @param parent a parent of returned nodes
184          * @throws UnknownTypeException if this TreeModel implementation is not
185          * able to resolve dchildren for given node type
186          *
187          * @return children for given parent on given indexes
188          */

189         public Object JavaDoc[] getChildren (Object JavaDoc parent, int from, int to)
190         throws UnknownTypeException {
191             addCall ("getChildren", parent);
192             if (parent == ROOT)
193                 return new Object JavaDoc[] {"a", "b", "c"};
194             if (parent instanceof String JavaDoc)
195                 return new Object JavaDoc[] {parent + "a", parent + "b", parent + "c"};
196             throw new UnknownTypeException (parent);
197         }
198
199         /**
200          * Returns number of children for given node.
201          *
202          * @param node the parent node
203          * @throws UnknownTypeException if this TreeModel implementation is not
204          * able to resolve children for given node type
205          *
206          * @return true if node is leaf
207          */

208         public int getChildrenCount (Object JavaDoc node) throws UnknownTypeException {
209             addCall ("getChildrenCount", node);
210             if (node == ROOT)
211                 return 3;
212             if (node instanceof String JavaDoc)
213                 return 3;
214             throw new UnknownTypeException (node);
215         }
216
217         /**
218          * Returns true if node is leaf.
219          *
220          * @throws UnknownTypeException if this TreeModel implementation is not
221          * able to resolve dchildren for given node type
222          * @return true if node is leaf
223          */

224         public boolean isLeaf (Object JavaDoc node) throws UnknownTypeException {
225             addCall ("isLeaf", node);
226             if (node == ROOT)
227                 return false;
228             if (node instanceof String JavaDoc)
229                 return ((String JavaDoc) node).length () > 3;
230             throw new UnknownTypeException (node);
231         }
232
233
234         // NodeModel ...........................................................
235

236         /**
237          * Returns display name for given node.
238          *
239          * @throws UnknownTypeException if this NodeModel implementation is not
240          * able to resolve display name for given node type
241          * @return display name for given node
242          */

243         public String JavaDoc getDisplayName (Object JavaDoc node) throws UnknownTypeException {
244             addCall ("getDisplayName", node);
245             //checkThread ();
246
if (node instanceof String JavaDoc)
247                 return (String JavaDoc) node;
248             throw new UnknownTypeException (node);
249         }
250
251         /**
252          * Returns tooltip for given node.
253          *
254          * @throws UnknownTypeException if this NodeModel implementation is not
255          * able to resolve tooltip for given node type
256          * @return tooltip for given node
257          */

258         public String JavaDoc getShortDescription (Object JavaDoc node)
259         throws UnknownTypeException {
260             addCall ("getShortDescription", node);
261             //checkThread (); Short description is called on AWT! How else we could display a tooltip?
262
if (node == ROOT)
263                 return "";
264             if (node instanceof String JavaDoc)
265                 return node + "WWW";
266             throw new UnknownTypeException (node);
267         }
268
269         /**
270          * Returns icon for given node.
271          *
272          * @throws UnknownTypeException if this NodeModel implementation is not
273          * able to resolve icon for given node type
274          * @return icon for given node
275          */

276         public String JavaDoc getIconBase (Object JavaDoc node)
277         throws UnknownTypeException {
278             addCall ("getIconBase", node);
279             //checkThread ();
280
if (node instanceof String JavaDoc)
281                 return node + "XXX";
282             throw new UnknownTypeException (node);
283         }
284
285
286         // NodeActionsProvider .....................................................
287

288         /**
289          * Performs default action for given node.
290          *
291          * @throws UnknownTypeException if this NodeActionsProvider implementation
292          * is not able to resolve actions for given node type
293          * @return display name for given node
294          */

295         public void performDefaultAction (Object JavaDoc node) throws UnknownTypeException {
296         }
297
298         /**
299          * Returns set of actions for given node.
300          *
301          * @throws UnknownTypeException if this NodeActionsProvider implementation
302          * is not able to resolve actions for given node type
303          * @return display name for given node
304          */

305         public Action JavaDoc[] getActions (Object JavaDoc node) throws UnknownTypeException {
306             //checkThread ();
307
if (node == ROOT)
308                 return new Action JavaDoc [0];
309             if (node instanceof String JavaDoc)
310                 return new Action JavaDoc[] {
311                     new AbstractAction JavaDoc ((String JavaDoc) node) {
312                         public void actionPerformed (ActionEvent JavaDoc ev) {
313                             
314                         }
315                     },
316                 };
317             throw new UnknownTypeException (node);
318         }
319
320
321         // ColumnsModel ............................................................
322

323         /**
324          * Returns sorted array of
325          * {@link org.netbeans.spi.viewmodel.ColumnModel}s.
326          *
327          * @return sorted array of ColumnModels
328          */

329         public ColumnModel[] getColumns () {
330             return new ColumnModel [0];
331         }
332
333
334         // TableModel ..............................................................
335

336         public Object JavaDoc getValueAt (Object JavaDoc node, String JavaDoc columnID) throws
337         UnknownTypeException {
338             addCall ("getValueAt", node);
339             checkThread ();
340             if (node instanceof String JavaDoc) {
341                 if (columnID.equals ("1"))
342                     return node + "1";
343                 if (columnID.equals ("2"))
344                     return node + "2";
345             }
346             throw new UnknownTypeException (node);
347         }
348
349         public boolean isReadOnly (Object JavaDoc node, String JavaDoc columnID) throws
350         UnknownTypeException {
351             addCall ("isReadOnly", node);
352             checkThread ();
353             if (node instanceof String JavaDoc) {
354                 if (columnID.equals ("1"))
355                     return true;
356                 if (columnID.equals ("2"))
357                     return true;
358             }
359             throw new UnknownTypeException (node);
360         }
361
362         public void setValueAt (Object JavaDoc node, String JavaDoc columnID, Object JavaDoc value) throws
363         UnknownTypeException {
364             throw new UnknownTypeException (node);
365         }
366
367
368         // TreeExpansionModel ......................................................
369

370         private Set JavaDoc toBeExpandedTest = new HashSet JavaDoc ();
371         private Set JavaDoc expandedTest = new HashSet JavaDoc ();
372         {
373             toBeExpandedTest.add ("a");
374             toBeExpandedTest.add ("ab");
375             toBeExpandedTest.add ("abc");
376         }
377         
378         /**
379          * Defines default state (collapsed, expanded) of given node.
380          *
381          * @param node a node
382          * @return default state (collapsed, expanded) of given node
383          */

384         public boolean isExpanded (Object JavaDoc node) throws UnknownTypeException {
385             if (node instanceof String JavaDoc)
386                 return toBeExpandedTest.contains (node);
387             throw new UnknownTypeException (node);
388         }
389
390         /**
391          * Called when given node is expanded.
392          *
393          * @param node a expanded node
394          */

395         public void nodeExpanded (Object JavaDoc node) {
396             if (!toBeExpandedTest.contains (node)) {
397                 System.err.println("This node should not be expanded: " + node);
398                 Thread.dumpStack();
399             }
400             expandedTest.add (node);
401         }
402
403         /**
404          * Called when given node is collapsed.
405          *
406          * @param node a collapsed node
407          */

408         public void nodeCollapsed (Object JavaDoc node) {
409             System.err.println("nodeCollapsed " + node);
410             Thread.dumpStack();
411         }
412
413
414         // listeners ...............................................................
415

416         /**
417          * Registers given listener.
418          *
419          * @param l the listener to add
420          */

421         public void addModelListener (ModelListener l) {
422             listeners.add (l);
423         }
424
425         /**
426          * Unregisters given listener.
427          *
428          * @param l the listener to remove
429          */

430         public void removeModelListener (ModelListener l) {
431             listeners.remove (l);
432         }
433         
434         public void fire () {
435             Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
436             int i, k = v.size ();
437             for (i = 0; i < k; i++)
438                 ((ModelListener) v.get (i)).modelChanged (null);
439         }
440         
441         public void fire (ModelEvent event) {
442             Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
443             int i, k = v.size ();
444             for (i = 0; i < k; i++) {
445                 ((ModelListener) v.get (i)).modelChanged (event);
446             }
447         }
448     }
449     
450     private static class TestColumnModel extends ColumnModel {
451         public Class JavaDoc getType() {
452             return String JavaDoc.class;
453         }
454
455         public String JavaDoc getDisplayName() {
456             return "Test";
457         }
458
459         public Character JavaDoc getDisplayedMnemonic() {
460             return new Character JavaDoc('e');
461         }
462
463         public String JavaDoc getID() {
464             return "xx";
465         }
466
467     }
468 }
469
Popular Tags