PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/netbeans-platform-7.0.1/openide.nodes/test/unit/src/org/openide/nodes/BeanNodeTest.java

#
Java | 377 lines | 249 code | 59 blank | 69 comment | 20 complexity | fd329cee1ff108061faa19d01c5deee2 MD5 | raw file
Possible License(s): MIT, Apache-2.0, LGPL-2.1, GPL-2.0, CPL-1.0, BSD-3-Clause
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.openide.nodes;
  45. import java.beans.*;
  46. import java.util.*;
  47. import junit.textui.TestRunner;
  48. import org.netbeans.junit.NbTestCase;
  49. import org.netbeans.junit.NbTestSuite;
  50. import org.openide.nodes.*;
  51. import org.openide.util.lookup.Lookups;
  52. /** Test updating of bean children in proper circumstances, e.g.
  53. * deleting nodes or beans.
  54. * @author Jesse Glick
  55. */
  56. public class BeanNodeTest extends NbTestCase {
  57. public BeanNodeTest(String name) {
  58. super(name);
  59. }
  60. public static void main(String[] args) {
  61. TestRunner.run(new NbTestSuite(BeanNodeTest.class));
  62. }
  63. /** Tests that basic introspected properties of the bean are reflected
  64. * as node properties.
  65. */
  66. public void testBasicBeanProperties() throws Exception {
  67. Bean1 b = new Bean1();
  68. assertEquals("hello", b.getName());
  69. BeanNode n = new BeanNode(b);
  70. assertEquals("hello", n.getName());
  71. n.setName("hi");
  72. assertEquals("hi", b.getName());
  73. assertEquals("hi", n.getName());
  74. Node.PropertySet[] propsets = n.getPropertySets();
  75. assertEquals(1, propsets.length);
  76. assertEquals(Sheet.PROPERTIES, propsets[0].getName());
  77. Node.Property[] props = propsets[0].getProperties();
  78. Node.Property prop = null;
  79. // Will have just one prop, 'foo'. OK to also have 'name' but we ignore it if so.
  80. for (int i = 0; i < props.length; i++) {
  81. if (props[i].getName().equals("foo") && prop == null) {
  82. prop = props[i];
  83. } else {
  84. assertEquals("name", props[i].getName());
  85. }
  86. }
  87. assertNotNull(prop);
  88. assertEquals("Foo", prop.getDisplayName());
  89. assertEquals("The foo.", prop.getShortDescription());
  90. assertEquals(new Integer(0), prop.getValue());
  91. b.setFoo(1);
  92. assertEquals(new Integer(1), prop.getValue());
  93. WaitPCL l2 = new WaitPCL("foo");
  94. n.addPropertyChangeListener(l2);
  95. b.setFoo(2);
  96. assertTrue("Calling a bean setter fires a Node.Property value change", l2.changed());
  97. assertEquals(new Integer(2), prop.getValue());
  98. prop.setValue(new Integer(3));
  99. assertEquals(new Integer(3), prop.getValue());
  100. assertEquals(new Integer(3), new Integer(b.getFoo()));
  101. NL l1 = new NL();
  102. n.addNodeListener(l1);
  103. b.setName("newname");
  104. assertTrue("Calling bean's name setter fires Node.PROPERTY_NAME", l1.changed());
  105. }
  106. /** Test that beans extending a private superclass are still usable.
  107. * No methods defined in the private superclass will be usable, so
  108. * they may be skipped if found by the introspector.
  109. * @see "#24767"
  110. */
  111. public void testPrivateInheritance() throws Exception {
  112. Bean2 b = new Bean2();
  113. BeanNode n = new BeanNode(b);
  114. Node.PropertySet[] propsets = n.getPropertySets();
  115. assertEquals(1, propsets.length);
  116. assertEquals(Sheet.PROPERTIES, propsets[0].getName());
  117. Node.Property[] props = propsets[0].getProperties();
  118. Node.Property prop = null;
  119. for (int i = 0; i < props.length; i++) {
  120. if (props[i].getName().equals("yaya") && prop == null) {
  121. prop = props[i];
  122. } else if (props[i].getName().equals("class")) {
  123. // OK, useless prop, ignore it
  124. } else if (props[i].getName().equals("foo")) {
  125. // OK, not required to be here, but can be
  126. // Cf. comment in BeanNode, and code in PropertySupport.Reflection
  127. // But we must be able to get the value!
  128. if (props[i].canRead()) {
  129. assertEquals("hello", props[i].getValue());
  130. }
  131. } else {
  132. assertEquals("name", props[i].getName());
  133. }
  134. }
  135. assertNotNull(prop);
  136. assertEquals(new Integer(5), prop.getValue());
  137. b.setYaya(3);
  138. assertEquals(new Integer(3), prop.getValue());
  139. // no foo, no listener
  140. }
  141. public void testNoFirindOfHiddenProperties () throws Exception {
  142. HidenPropertyBean bean = new HidenPropertyBean ();
  143. BeanNode node = new BeanNode (bean);
  144. Node.PropertySet[] arr = node.getPropertySets ();
  145. int cnt = 0;
  146. for (int i = 0; i < arr.length; i++) {
  147. cnt += arr[i].getProperties ().length;
  148. }
  149. assertEquals ("Bean node does not show hidden properties", 0, cnt);
  150. WaitPCL pcl = new WaitPCL (null);
  151. node.addPropertyChangeListener (pcl);
  152. bean.setHiddenProperty (11);
  153. assertFalse ("No change should be notified", pcl.changed ());
  154. }
  155. public void testLookupAndBean() throws Exception {
  156. class MyClass {
  157. }
  158. MyClass inst = new MyClass();
  159. Bean1 b = new Bean1();
  160. BeanNode n = new BeanNode(b, null, Lookups.singleton(inst));
  161. // my lookup is propagated
  162. MyClass tst = n.getLookup().lookup(MyClass.class);
  163. assertSame(inst, tst);
  164. }
  165. public void testCanUseCookieSetWithoutLookup() throws Exception {
  166. class MyBeanNode extends BeanNode {
  167. MyBeanNode(Object bean) throws IntrospectionException {
  168. super(bean, null, null);
  169. getCookieSet();
  170. }
  171. };
  172. new MyBeanNode(new Bean1());
  173. }
  174. // XXX test synchronizeName
  175. public static final class Bean1 {
  176. private String name = "hello";
  177. private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
  178. private int foo = 0;
  179. public void addPropertyChangeListener(PropertyChangeListener l) {
  180. propertyChangeSupport.addPropertyChangeListener(l);
  181. }
  182. public void removePropertyChangeListener(PropertyChangeListener l) {
  183. propertyChangeSupport.removePropertyChangeListener(l);
  184. }
  185. public String getName() {
  186. return this.name;
  187. }
  188. public void setName(String name) {
  189. String oldName = this.name;
  190. this.name = name;
  191. propertyChangeSupport.firePropertyChange("name", oldName, name);
  192. }
  193. public int getFoo() {
  194. return this.foo;
  195. }
  196. public void setFoo(int foo) {
  197. int oldFoo = this.foo;
  198. this.foo = foo;
  199. propertyChangeSupport.firePropertyChange("foo", new Integer(oldFoo), new Integer(foo));
  200. }
  201. }
  202. public static final class Bean1BeanInfo extends SimpleBeanInfo {
  203. public PropertyDescriptor[] getPropertyDescriptors() {
  204. try {
  205. PropertyDescriptor foo = new PropertyDescriptor("foo", Bean1.class);
  206. foo.setDisplayName("Foo");
  207. foo.setShortDescription("The foo.");
  208. return new PropertyDescriptor[] {foo};
  209. } catch (IntrospectionException ie) {
  210. ie.printStackTrace();
  211. return null;
  212. }
  213. }
  214. }
  215. private static class Bean2S {
  216. private String foo = "hello";
  217. protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
  218. public void addPropertyChangeListener(PropertyChangeListener l) {
  219. propertyChangeSupport.addPropertyChangeListener(l);
  220. }
  221. public void removePropertyChangeListener(PropertyChangeListener l) {
  222. propertyChangeSupport.removePropertyChangeListener(l);
  223. }
  224. public String getFoo() {
  225. return this.foo;
  226. }
  227. public void setFoo(String foo) {
  228. String oldFoo = this.foo;
  229. this.foo = foo;
  230. propertyChangeSupport.firePropertyChange("foo", oldFoo, foo);
  231. }
  232. }
  233. public static final class Bean2 extends Bean2S {
  234. private int yaya = 5;
  235. public int getYaya() {
  236. return this.yaya;
  237. }
  238. public void setYaya(int yaya) {
  239. int oldYaya = this.yaya;
  240. this.yaya = yaya;
  241. propertyChangeSupport.firePropertyChange("yaya", new Integer(oldYaya), new Integer(yaya));
  242. }
  243. }
  244. /** Prop listener that will tell you if it gets a change.
  245. */
  246. private static class WaitPCL implements PropertyChangeListener {
  247. /** whether a change has been received, and if so count */
  248. public int gotit = 0;
  249. /** optional property name to filter by (if null, accept any) */
  250. private final String prop;
  251. public WaitPCL(String p) {
  252. prop = p;
  253. }
  254. public synchronized void propertyChange(PropertyChangeEvent evt) {
  255. if (prop == null || prop.equals(evt.getPropertyName())) {
  256. gotit++;
  257. notifyAll();
  258. }
  259. }
  260. public boolean changed() {
  261. return changed(1500);
  262. }
  263. public synchronized boolean changed(int timeout) {
  264. if (gotit > 0) {
  265. return true;
  266. }
  267. try {
  268. wait(timeout);
  269. } catch (InterruptedException ie) {
  270. ie.printStackTrace();
  271. }
  272. return gotit > 0;
  273. }
  274. }
  275. private static class NL extends WaitPCL implements NodeListener {
  276. public NL() {
  277. super(Node.PROP_NAME);
  278. }
  279. public void childrenAdded(NodeMemberEvent ev) {}
  280. public void childrenRemoved(NodeMemberEvent ev) {}
  281. public void childrenReordered(NodeReorderEvent ev) {}
  282. public void nodeDestroyed(NodeEvent ev) {}
  283. }
  284. public static class HidenPropertyBean {
  285. private int hiddenProperty;
  286. private PropertyChangeSupport pcs;
  287. /** Creates a new instance of Model */
  288. public HidenPropertyBean () {
  289. pcs = new PropertyChangeSupport(this);
  290. hiddenProperty = -1;
  291. }
  292. public void addPropertyChangeListener(PropertyChangeListener l) {
  293. pcs.addPropertyChangeListener(l);
  294. }
  295. public void removePropertyChangeListener(PropertyChangeListener l) {
  296. pcs.removePropertyChangeListener(l);
  297. }
  298. public int getHiddenProperty() {
  299. return hiddenProperty;
  300. }
  301. public void setHiddenProperty(int value) {
  302. this.hiddenProperty = value;
  303. pcs.firePropertyChange("hiddenProperty", null, null);
  304. }
  305. }
  306. public static class HidenPropertyBeanBeanInfo extends SimpleBeanInfo {
  307. public PropertyDescriptor[] getPropertyDescriptors () {
  308. PropertyDescriptor[] properties = new PropertyDescriptor[1];
  309. try {
  310. properties[0] = new PropertyDescriptor ( "hiddenProperty", HidenPropertyBean.class, "getHiddenProperty", "setHiddenProperty" );
  311. properties[0].setHidden ( true );
  312. }
  313. catch( IntrospectionException e) {}
  314. // Here you can add code for customizing the properties array.
  315. return properties;
  316. }
  317. }
  318. }