1 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 35 public class BeanChildrenTest extends NbTestCase { 36 37 public BeanChildrenTest(String name) { 38 super(name); 39 } 40 41 public static void main(String [] args) { 42 TestRunner.run(new NbTestSuite(BeanChildrenTest.class)); 43 } 44 45 @SuppressWarnings ("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 [] nodes2Names(Node[] nodes) { 55 String [] names = new String [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 { 63 BeanContext bc = makeContext(); 64 Children c = new BeanChildren(bc, new SimpleFactory()); 65 assertEquals("correct subnodes", 68 new HashSet<String >(Arrays.asList(new String [] {"one", "two", "three"})), 69 new HashSet<String >(Arrays.asList(nodes2Names(c.getNodes())))); 70 } 71 72 public void testRemoveBeanRemovesChild() throws Exception { 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 >(Arrays.asList(new String [] {"one", "three"})), 78 new HashSet<Object >(Arrays.asList(bc.toArray()))); 79 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 >(Arrays.asList(new String [] {"one", "three"})), 93 new HashSet<String >(Arrays.asList(nodes2Names(nodes)))); 94 } 95 96 public void testDeleteChildRemovesBean() throws Exception { 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 Children.MUTEX.readAccess(new Mutex.Action<Void >() { 106 public Void run() { 107 return null; 108 } 109 }); 110 assertEquals("correct beans", 111 new HashSet<String >(Arrays.asList(new String [] {"one", "three"})), 112 new HashSet<Object >(Arrays.asList(bc.toArray()))); 113 } 114 115 private static final class SimpleFactory implements BeanChildren.Factory { 116 public Node createNode(Object bean) throws IntrospectionException { 117 Node n = new AbstractNode(Children.LEAF); 118 n.setName((String )bean); 119 return n; 120 } 121 } 122 123 } 124 | Popular Tags |