KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > NodeListModelTest


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.view;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List 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
30 /*
31  * Tests for class NodeListModel
32  */

33 public class NodeListModelTest extends NbTestCase {
34
35     private static final int NO_OF_NODES = 20;
36
37     public NodeListModelTest(String JavaDoc name) {
38         super(name);
39     }
40
41     protected boolean runInEQ() {
42         return true;
43     }
44     
45     /*
46      * Tests whether children of the root node are
47      * kept in the memory after the root is passed
48      * to the constructor of NodeListModel.
49      */

50     public void testNodesAreReferenced() {
51         
52         WeakReference JavaDoc[] tn;
53         
54         Node c = new AbstractNode(new CNodeChildren());
55         NodeListModel model = new NodeListModel(c);
56         
57         
58         
59         tn = new WeakReference JavaDoc[model.getSize()];
60         for (int i = 0; i < model.getSize(); i++) {
61             tn[i] = new WeakReference JavaDoc(model.getElementAt(i));
62         }
63         
64         assertTrue("Need to have more than one child", tn.length > 0);
65         
66         boolean fail;
67         try {
68             assertGC("First node should not be gone", tn[0]);
69             fail = true;
70         } catch (Error JavaDoc err) {
71             fail = false;
72         }
73         if (fail) {
74             fail("First node garbage collected!!! " + tn[0].get());
75         }
76         
77         for (int i = 0; i < tn.length; i++) {
78             // else fail
79
assertNotNull("One of the nodes was gone. Index: " + i, tn[i].get());
80         }
81     }
82     
83     /**
84      * Tests proper initialization in constructors.
85      */

86     public void testConstructors() {
87         Node c = new AbstractNode(new CNodeChildren());
88         NodeListModel model = new NodeListModel(c);
89         
90         // the following line used to fail if the
91
// no parameter costructor does not initialize
92
// childrenCount
93
model.getSize();
94     }
95     
96     /*
97      * Children for testNodesAreReferenced.
98      */

99     private static class CNodeChildren extends Children.Keys {
100         public CNodeChildren() {
101             List JavaDoc myKeys = new LinkedList JavaDoc();
102             for (int i = 0; i < NO_OF_NODES; i++) {
103                 myKeys.add(new Integer JavaDoc(i));
104             }
105             
106             setKeys(myKeys);
107         }
108         
109         protected Node[] createNodes(Object JavaDoc key) {
110             AbstractNode an = new AbstractNode(Children.LEAF);
111             an.setName(key.toString());
112             return new Node[] { an };
113         }
114     }
115 }
116
Popular Tags