1 19 20 package org.openide.nodes; 21 22 import java.beans.*; 23 import java.util.*; 24 25 import junit.textui.TestRunner; 26 import org.netbeans.junit.NbTestCase; 27 import org.netbeans.junit.NbTestSuite; 28 29 import org.openide.nodes.*; 30 import org.openide.util.lookup.Lookups; 31 32 36 public class BeanNodeTest extends NbTestCase { 37 38 public BeanNodeTest(String name) { 39 super(name); 40 } 41 42 public static void main(String [] args) { 43 TestRunner.run(new NbTestSuite(BeanNodeTest.class)); 44 } 45 46 49 public void testBasicBeanProperties() throws Exception { 50 Bean1 b = new Bean1(); 51 assertEquals("hello", b.getName()); 52 BeanNode n = new BeanNode(b); 53 assertEquals("hello", n.getName()); 54 n.setName("hi"); 55 assertEquals("hi", b.getName()); 56 assertEquals("hi", n.getName()); 57 Node.PropertySet[] propsets = n.getPropertySets(); 58 assertEquals(1, propsets.length); 59 assertEquals(Sheet.PROPERTIES, propsets[0].getName()); 60 Node.Property[] props = propsets[0].getProperties(); 61 Node.Property prop = null; 62 for (int i = 0; i < props.length; i++) { 64 if (props[i].getName().equals("foo") && prop == null) { 65 prop = props[i]; 66 } else { 67 assertEquals("name", props[i].getName()); 68 } 69 } 70 assertNotNull(prop); 71 assertEquals("Foo", prop.getDisplayName()); 72 assertEquals("The foo.", prop.getShortDescription()); 73 assertEquals(new Integer (0), prop.getValue()); 74 b.setFoo(1); 75 assertEquals(new Integer (1), prop.getValue()); 76 WaitPCL l2 = new WaitPCL("foo"); 77 n.addPropertyChangeListener(l2); 78 b.setFoo(2); 79 assertTrue("Calling a bean setter fires a Node.Property value change", l2.changed()); 80 assertEquals(new Integer (2), prop.getValue()); 81 prop.setValue(new Integer (3)); 82 assertEquals(new Integer (3), prop.getValue()); 83 assertEquals(new Integer (3), new Integer (b.getFoo())); 84 NL l1 = new NL(); 85 n.addNodeListener(l1); 86 b.setName("newname"); 87 assertTrue("Calling bean's name setter fires Node.PROPERTY_NAME", l1.changed()); 88 } 89 90 95 public void testPrivateInheritance() throws Exception { 96 Bean2 b = new Bean2(); 97 BeanNode n = new BeanNode(b); 98 Node.PropertySet[] propsets = n.getPropertySets(); 99 assertEquals(1, propsets.length); 100 assertEquals(Sheet.PROPERTIES, propsets[0].getName()); 101 Node.Property[] props = propsets[0].getProperties(); 102 Node.Property prop = null; 103 for (int i = 0; i < props.length; i++) { 104 if (props[i].getName().equals("yaya") && prop == null) { 105 prop = props[i]; 106 } else if (props[i].getName().equals("class")) { 107 } else if (props[i].getName().equals("foo")) { 109 if (props[i].canRead()) { 113 assertEquals("hello", props[i].getValue()); 114 } 115 } else { 116 assertEquals("name", props[i].getName()); 117 } 118 } 119 assertNotNull(prop); 120 assertEquals(new Integer (5), prop.getValue()); 121 b.setYaya(3); 122 assertEquals(new Integer (3), prop.getValue()); 123 } 125 126 public void testNoFirindOfHiddenProperties () throws Exception { 127 HidenPropertyBean bean = new HidenPropertyBean (); 128 129 130 BeanNode node = new BeanNode (bean); 131 132 Node.PropertySet[] arr = node.getPropertySets (); 133 int cnt = 0; 134 for (int i = 0; i < arr.length; i++) { 135 cnt += arr[i].getProperties ().length; 136 } 137 assertEquals ("Bean node does not show hidden properties", 0, cnt); 138 139 WaitPCL pcl = new WaitPCL (null); 140 node.addPropertyChangeListener (pcl); 141 142 bean.setHiddenProperty (11); 143 144 assertFalse ("No change should be notified", pcl.changed ()); 145 } 146 147 public void testLookupAndBean() throws Exception { 148 class MyClass { 149 } 150 151 MyClass inst = new MyClass(); 152 153 Bean1 b = new Bean1(); 154 BeanNode n = new BeanNode(b, null, Lookups.singleton(inst)); 155 156 MyClass tst = n.getLookup().lookup(MyClass.class); 158 assertSame(inst, tst); 159 } 160 161 public void testCanUseCookieSetWithoutLookup() throws Exception { 162 class MyBeanNode extends BeanNode { 163 MyBeanNode(Object bean) throws IntrospectionException { 164 super(bean, null, null); 165 getCookieSet(); 166 } 167 }; 168 169 new MyBeanNode(new Bean1()); 170 } 171 172 174 public static final class Bean1 { 175 176 private String name = "hello"; 177 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); 178 private int foo = 0; 179 180 public void addPropertyChangeListener(PropertyChangeListener l) { 181 propertyChangeSupport.addPropertyChangeListener(l); 182 } 183 public void removePropertyChangeListener(PropertyChangeListener l) { 184 propertyChangeSupport.removePropertyChangeListener(l); 185 } 186 187 public String getName() { 188 return this.name; 189 } 190 public void setName(String name) { 191 String oldName = this.name; 192 this.name = name; 193 propertyChangeSupport.firePropertyChange("name", oldName, name); 194 } 195 196 public int getFoo() { 197 return this.foo; 198 } 199 public void setFoo(int foo) { 200 int oldFoo = this.foo; 201 this.foo = foo; 202 propertyChangeSupport.firePropertyChange("foo", new Integer (oldFoo), new Integer (foo)); 203 } 204 205 } 206 207 public static final class Bean1BeanInfo extends SimpleBeanInfo { 208 public PropertyDescriptor[] getPropertyDescriptors() { 209 try { 210 PropertyDescriptor foo = new PropertyDescriptor("foo", Bean1.class); 211 foo.setDisplayName("Foo"); 212 foo.setShortDescription("The foo."); 213 return new PropertyDescriptor[] {foo}; 214 } catch (IntrospectionException ie) { 215 ie.printStackTrace(); 216 return null; 217 } 218 } 219 } 220 221 private static class Bean2S { 222 223 private String foo = "hello"; 224 protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); 225 226 public void addPropertyChangeListener(PropertyChangeListener l) { 227 propertyChangeSupport.addPropertyChangeListener(l); 228 } 229 public void removePropertyChangeListener(PropertyChangeListener l) { 230 propertyChangeSupport.removePropertyChangeListener(l); 231 } 232 233 public String getFoo() { 234 return this.foo; 235 } 236 public void setFoo(String foo) { 237 String oldFoo = this.foo; 238 this.foo = foo; 239 propertyChangeSupport.firePropertyChange("foo", oldFoo, foo); 240 } 241 242 } 243 244 public static final class Bean2 extends Bean2S { 245 246 private int yaya = 5; 247 248 public int getYaya() { 249 return this.yaya; 250 } 251 public void setYaya(int yaya) { 252 int oldYaya = this.yaya; 253 this.yaya = yaya; 254 propertyChangeSupport.firePropertyChange("yaya", new Integer (oldYaya), new Integer (yaya)); 255 } 256 257 } 258 259 261 private static class WaitPCL implements PropertyChangeListener { 262 263 264 public int gotit = 0; 265 266 267 private final String prop; 268 269 public WaitPCL(String p) { 270 prop = p; 271 } 272 273 public synchronized void propertyChange(PropertyChangeEvent evt) { 274 if (prop == null || prop.equals(evt.getPropertyName())) { 275 gotit++; 276 notifyAll(); 277 } 278 } 279 280 public boolean changed() { 281 return changed(1500); 282 } 283 284 public synchronized boolean changed(int timeout) { 285 if (gotit > 0) { 286 return true; 287 } 288 try { 289 wait(timeout); 290 } catch (InterruptedException ie) { 291 ie.printStackTrace(); 292 } 293 return gotit > 0; 294 } 295 296 } 297 298 private static class NL extends WaitPCL implements NodeListener { 299 public NL() { 300 super(Node.PROP_NAME); 301 } 302 public void childrenAdded(NodeMemberEvent ev) {} 303 public void childrenRemoved(NodeMemberEvent ev) {} 304 public void childrenReordered(NodeReorderEvent ev) {} 305 public void nodeDestroyed(NodeEvent ev) {} 306 } 307 308 public static class HidenPropertyBean { 309 private int hiddenProperty; 310 private PropertyChangeSupport pcs; 311 312 313 public HidenPropertyBean () { 314 pcs = new PropertyChangeSupport(this); 315 hiddenProperty = -1; 316 } 317 318 public void addPropertyChangeListener(PropertyChangeListener l) { 319 pcs.addPropertyChangeListener(l); 320 } 321 322 public void removePropertyChangeListener(PropertyChangeListener l) { 323 pcs.removePropertyChangeListener(l); 324 } 325 326 public int getHiddenProperty() { 327 return hiddenProperty; 328 } 329 330 public void setHiddenProperty(int value) { 331 this.hiddenProperty = value; 332 pcs.firePropertyChange("hiddenProperty", null, null); 333 } 334 } 335 336 public static class HidenPropertyBeanBeanInfo extends SimpleBeanInfo { 337 public PropertyDescriptor[] getPropertyDescriptors () { 338 PropertyDescriptor[] properties = new PropertyDescriptor[1]; 339 340 try { 341 properties[0] = new PropertyDescriptor ( "hiddenProperty", HidenPropertyBean.class, "getHiddenProperty", "setHiddenProperty" ); 342 properties[0].setHidden ( true ); 343 } 344 catch( IntrospectionException e) {} 345 346 348 return properties; 349 } 350 } 351 352 } 353 | Popular Tags |