1 19 20 package org.openide.explorer; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.VetoableChangeListener ; 25 import java.util.LinkedList ; 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.nodes.AbstractNode; 28 import org.openide.nodes.Children; 29 import org.openide.nodes.Node; 30 31 35 public class ExplorerManagerTest extends NbTestCase 36 implements PropertyChangeListener { 37 private ExplorerManager em; 38 private Keys keys; 39 private Node root; 40 private LinkedList events; 41 42 public ExplorerManagerTest(String testName) { 43 super(testName); 44 } 45 46 48 protected boolean runInEQ() { 49 return true; 50 } 51 52 protected void setUp() throws Exception { 53 em = new ExplorerManager(); 54 keys = new Keys(); 55 root = new AbstractNode(keys); 56 Node[] justAsk = root.getChildren().getNodes(true); 57 em.setRootContext(root); 58 events = new LinkedList (); 59 } 60 61 public void propertyChange(PropertyChangeEvent ev) { 62 assertFalse("No read lock held", Children.MUTEX.isReadAccess()); 63 assertFalse("No write lock held", Children.MUTEX.isWriteAccess()); 64 65 events.add(ev); 66 } 67 68 public void testNormalSelectionChange() throws Exception { 69 final Node a = keys.key("a key"); 70 71 em.addPropertyChangeListener(this); 72 73 em.setSelectedNodes(new Node[] { a }); 74 Node[] arr = em.getSelectedNodes(); 75 assertEquals("One selected", 1, arr.length); 76 assertEquals("A is there", a, arr[0]); 77 78 79 assertEquals("One event", 1, events.size()); 80 PropertyChangeEvent ev = (PropertyChangeEvent )events.removeFirst(); 81 assertEquals("Name is good", ExplorerManager.PROP_SELECTED_NODES, ev.getPropertyName()); 82 83 events.clear(); 84 em.setSelectedNodes(new Node[] { a }); 85 assertEquals("No change: " + events, 0, events.size()); 86 } 87 88 public void testCannotSetNodesNotUnderTheRoot() throws Exception { 89 final Node a = new AbstractNode(Children.LEAF); 90 91 try { 92 em.setSelectedNodes(new Node[] { a }); 93 fail("Should throw IllegalArgumentException as the node is not under root"); 94 } catch (IllegalArgumentException ex) { 95 } 97 } 98 99 100 public void testSetNodesSurviveChangeOfNodes() throws Exception { 101 final Node a = keys.key("toRemove"); 102 103 class ChangeTheSelectionInMiddleOfMethod implements VetoableChangeListener { 104 public int cnt; 105 106 public void vetoableChange(PropertyChangeEvent evt) { 107 cnt++; 108 keys.keys(new String [0]); 109 } 110 } 111 112 ChangeTheSelectionInMiddleOfMethod list = new ChangeTheSelectionInMiddleOfMethod(); 113 em.addVetoableChangeListener(list); 114 115 em.setSelectedNodes(new Node[] { a }); 116 117 assertEquals("Vetoable listener called", 1, list.cnt); 118 assertEquals("Node is dead", null, a.getParentNode()); 119 120 em.waitFinished(); 122 123 Node[] arr = em.getSelectedNodes(); 124 assertEquals("No nodes can be selected", 0, arr.length); 125 } 126 127 public void testCannotVetoSetToEmptySelection() throws Exception { 128 final Node a = keys.key("toRemove"); 129 130 em.setSelectedNodes(new Node[] { a }); 131 132 class NeverCalledVeto implements VetoableChangeListener { 133 public int cnt; 134 135 public void vetoableChange(PropertyChangeEvent evt) { 136 cnt++; 137 keys.keys(new String [0]); 138 } 139 } 140 141 NeverCalledVeto list = new NeverCalledVeto(); 142 em.addVetoableChangeListener(list); 143 144 em.setSelectedNodes(new Node[0]); 145 146 assertEquals("Veto not called", 0, list.cnt); 147 Node[] arr = em.getSelectedNodes(); 148 assertEquals("No nodes can be selected", 0, arr.length); 149 } 150 151 private static final class Keys extends Children.Keys { 152 public Node key(String k) { 153 keys(new String [] { k }); 154 return getNodes()[0]; 155 } 156 public void keys(String [] keys) { 157 super.setKeys(keys); 158 } 159 protected Node[] createNodes(Object o) { 160 AbstractNode an = new AbstractNode(Children.LEAF); 161 an.setName((String )o); 162 return new Node[] { an }; 163 } 164 } 165 } 166 | Popular Tags |