KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.*;
23 import junit.textui.TestRunner;
24 import java.beans.*;
25 import java.beans.beancontext.*;
26 import java.util.*;
27 import org.openide.util.Mutex;
28
29 import org.netbeans.junit.*;
30
31 /** Test updating of bean children in proper circumstances, e.g.
32  * deleting nodes or beans.
33  * @author Jesse Glick
34  */

35 public class BeanChildrenTest extends NbTestCase {
36
37     public BeanChildrenTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] args) {
42         TestRunner.run(new NbTestSuite(BeanChildrenTest.class));
43     }
44     
45     @SuppressWarnings JavaDoc("unchecked")
46     private static BeanContext makeContext() {
47         BeanContext bc = new BeanContextSupport();
48         bc.add("one");
49         bc.add("two");
50         bc.add("three");
51         return bc;
52     }
53     
54     private static String JavaDoc[] nodes2Names(Node[] nodes) {
55         String JavaDoc[] names = new String JavaDoc[nodes.length];
56         for (int i = 0; i < nodes.length; i++) {
57             names[i] = nodes[i].getName();
58         }
59         return names;
60     }
61     
62     public void testNodesAreCorrect() throws Exception JavaDoc {
63         BeanContext bc = makeContext();
64         Children c = new BeanChildren(bc, new SimpleFactory());
65         // Note that BeanContextSupport keeps a HashMap of children
66
// so the order is not deterministic.
67
assertEquals("correct subnodes",
68             new HashSet<String JavaDoc>(Arrays.asList(new String JavaDoc[] {"one", "two", "three"})),
69             new HashSet<String JavaDoc>(Arrays.asList(nodes2Names(c.getNodes()))));
70     }
71     
72     public void testRemoveBeanRemovesChild() throws Exception JavaDoc {
73         BeanContext bc = makeContext();
74         final Children c = new BeanChildren(bc, new SimpleFactory());
75         bc.remove("two");
76         assertEquals("correct beans",
77             new HashSet<String JavaDoc>(Arrays.asList(new String JavaDoc[] {"one", "three"})),
78             new HashSet<Object JavaDoc>(Arrays.asList(bc.toArray())));
79         // Make sure we let the children thread run to completion.
80
// Check the result in the reader.
81
// First make sure it is initialized. Otherwise Children.Keys.getNodes
82
// from within the mutex immediately returns no nodes, then when
83
// next asked has them all. Checking outside the mutex seems to block
84
// until the nodes have been initialized.
85
Node[] nodes = c.getNodes(true);
86         nodes = Children.MUTEX.readAccess(new Mutex.Action<Node[]>() {
87             public Node[] run() {
88                 return c.getNodes();
89             }
90         });
91         assertEquals("correct subnodes",
92             new HashSet<String JavaDoc>(Arrays.asList(new String JavaDoc[] {"one", "three"})),
93             new HashSet<String JavaDoc>(Arrays.asList(nodes2Names(nodes))));
94     }
95     
96     // Cf. #7925.
97
public void testDeleteChildRemovesBean() throws Exception JavaDoc {
98         BeanContext bc = makeContext();
99         Children c = new BeanChildren(bc, new SimpleFactory());
100         Node n = c.findChild("two");
101         assertNotNull(n);
102         assertEquals("two", n.getName());
103         n.destroy();
104         // Wait for changes, maybe:
105
Children.MUTEX.readAccess(new Mutex.Action<Void JavaDoc>() {
106             public Void JavaDoc run() {
107                 return null;
108             }
109         });
110         assertEquals("correct beans",
111             new HashSet<String JavaDoc>(Arrays.asList(new String JavaDoc[] {"one", "three"})),
112             new HashSet<Object JavaDoc>(Arrays.asList(bc.toArray())));
113     }
114     
115     private static final class SimpleFactory implements BeanChildren.Factory {
116         public Node createNode(Object JavaDoc bean) throws IntrospectionException {
117             Node n = new AbstractNode(Children.LEAF);
118             n.setName((String JavaDoc)bean);
119             return n;
120         }
121     }
122     
123 }
124
Popular Tags