001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.nodes;
043:
044: import junit.framework.*;
045: import junit.textui.TestRunner;
046: import java.beans.*;
047: import java.beans.beancontext.*;
048: import java.util.*;
049: import org.openide.util.Mutex;
050:
051: import org.netbeans.junit.*;
052:
053: /** Test updating of bean children in proper circumstances, e.g.
054: * deleting nodes or beans.
055: * @author Jesse Glick
056: */
057: public class BeanChildrenTest extends NbTestCase {
058:
059: public BeanChildrenTest(String name) {
060: super (name);
061: }
062:
063: public static void main(String[] args) {
064: TestRunner.run(new NbTestSuite(BeanChildrenTest.class));
065: }
066:
067: @SuppressWarnings("unchecked")
068: private static BeanContext makeContext() {
069: BeanContext bc = new BeanContextSupport();
070: bc.add("one");
071: bc.add("two");
072: bc.add("three");
073: return bc;
074: }
075:
076: private static String[] nodes2Names(Node[] nodes) {
077: String[] names = new String[nodes.length];
078: for (int i = 0; i < nodes.length; i++) {
079: names[i] = nodes[i].getName();
080: }
081: return names;
082: }
083:
084: public void testNodesAreCorrect() throws Exception {
085: BeanContext bc = makeContext();
086: Children c = new BeanChildren(bc, new SimpleFactory());
087: // Note that BeanContextSupport keeps a HashMap of children
088: // so the order is not deterministic.
089: assertEquals("correct subnodes", new HashSet<String>(Arrays
090: .asList(new String[] { "one", "two", "three" })),
091: new HashSet<String>(Arrays.asList(nodes2Names(c
092: .getNodes()))));
093: }
094:
095: public void testRemoveBeanRemovesChild() throws Exception {
096: BeanContext bc = makeContext();
097: final Children c = new BeanChildren(bc, new SimpleFactory());
098: bc.remove("two");
099: assertEquals("correct beans", new HashSet<String>(Arrays
100: .asList(new String[] { "one", "three" })),
101: new HashSet<Object>(Arrays.asList(bc.toArray())));
102: // Make sure we let the children thread run to completion.
103: // Check the result in the reader.
104: // First make sure it is initialized. Otherwise Children.Keys.getNodes
105: // from within the mutex immediately returns no nodes, then when
106: // next asked has them all. Checking outside the mutex seems to block
107: // until the nodes have been initialized.
108: Node[] nodes = c.getNodes(true);
109: nodes = Children.MUTEX.readAccess(new Mutex.Action<Node[]>() {
110: public Node[] run() {
111: return c.getNodes();
112: }
113: });
114: assertEquals("correct subnodes", new HashSet<String>(Arrays
115: .asList(new String[] { "one", "three" })),
116: new HashSet<String>(Arrays.asList(nodes2Names(nodes))));
117: }
118:
119: // Cf. #7925.
120: public void testDeleteChildRemovesBean() throws Exception {
121: BeanContext bc = makeContext();
122: Children c = new BeanChildren(bc, new SimpleFactory());
123: Node n = c.findChild("two");
124: assertNotNull(n);
125: assertEquals("two", n.getName());
126: n.destroy();
127: // Wait for changes, maybe:
128: Children.MUTEX.readAccess(new Mutex.Action<Void>() {
129: public Void run() {
130: return null;
131: }
132: });
133: assertEquals("correct beans", new HashSet<String>(Arrays
134: .asList(new String[] { "one", "three" })),
135: new HashSet<Object>(Arrays.asList(bc.toArray())));
136: }
137:
138: private static final class SimpleFactory implements
139: BeanChildren.Factory {
140: public Node createNode(Object bean)
141: throws IntrospectionException {
142: Node n = new AbstractNode(Children.LEAF);
143: n.setName((String) bean);
144: return n;
145: }
146: }
147:
148: }
|