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.windows;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyVetoException;
047: import java.util.Collection;
048: import java.util.Iterator;
049: import javax.swing.SwingUtilities;
050:
051: import junit.framework.Test;
052: import junit.framework.TestSuite;
053: import junit.textui.TestRunner;
054:
055: import org.netbeans.junit.NbTestCase;
056: import org.netbeans.junit.NbTestSuite;
057:
058: import org.openide.explorer.ExplorerManager;
059: import org.openide.explorer.ExplorerPanel;
060: import org.openide.explorer.view.BeanTreeView;
061: import org.openide.explorer.view.TreeView;
062: import org.openide.nodes.AbstractNode;
063: import org.openide.nodes.Children;
064: import org.openide.nodes.Node;
065: import org.openide.util.Lookup;
066:
067: /** Check the synchronization TC.getActivatedNodes and ExplorerManager.getSelectedNodes.
068: * Test should assure the fix of issue 31244.
069: *
070: * @author Jiri Rechtacek
071: */
072: public class TopComponentActivatedNodesTest extends NbTestCase {
073: /** top component we work on */
074: private TopComponent top;
075:
076: public TopComponentActivatedNodesTest(java.lang.String testName) {
077: super (testName);
078: }
079:
080: public static void main(java.lang.String[] args) {
081: TestRunner.run(suite());
082: }
083:
084: public static Test suite() {
085: TestSuite suite = new NbTestSuite(
086: TopComponentActivatedNodesTest.class);
087:
088: return suite;
089: }
090:
091: protected boolean runInEQ() {
092: return true;
093: }
094:
095: private ExplorerPanel p;
096: private ExplorerManager em;
097: private Node[] nodes;
098: private PropertyChangeListener listenerEM, listenerTC;
099:
100: protected void setUp() {
101: System.setProperty("org.openide.util.Lookup", "-"); // no lookup
102:
103: p = new ExplorerPanel();
104: em = p.getExplorerManager();
105:
106: TreeView tv = new BeanTreeView();
107: p.add(tv);
108: Children ch = new Children.Array();
109: nodes = new Node[10];
110: for (int i = 0; i < 10; i++) {
111: nodes[i] = new AbstractNode(Children.LEAF);
112: nodes[i].setName("Node" + i);
113: }
114: ch.add(nodes);
115: Node root = new AbstractNode(ch);
116: em.setRootContext(root);
117:
118: // check synchronixzation before
119: assertArrays(
120: "INIT: getSelectedNodes equals getActivatedNodes.", em
121: .getSelectedNodes(), p.getActivatedNodes());
122: }
123:
124: private void initListeners() {
125: listenerTC = new PropertyChangeListener() {
126: public void propertyChange(PropertyChangeEvent ev) {
127: System.out.println("TC: PROP_ACTIVATED_NODES change!");
128: // try {
129: // Thread.sleep (1000);
130: // } catch (Exception e) {
131: // }
132: assertArrays(
133: "FIRED TC CHANGE: getSelectedNodes equals PROP_ACTIVATED_NODES",
134: em.getSelectedNodes(), p.getActivatedNodes());
135: }
136: };
137:
138: p.addPropertyChangeListener(
139: TopComponent.Registry.PROP_ACTIVATED_NODES, listenerTC);
140:
141: listenerEM = new PropertyChangeListener() {
142: public void propertyChange(PropertyChangeEvent ev) {
143: if (ExplorerManager.PROP_SELECTED_NODES.equals(ev
144: .getPropertyName())) {
145: System.out
146: .println("EM: PROP_SELECTED_NODES change!");
147: assertArrays(
148: "FIRED EM CHANGE: PROP_SELECTED_NODES equals getActivatedNodes",
149: ((Object[]) ev.getNewValue()), p
150: .getActivatedNodes());
151: }
152: }
153: };
154:
155: em.addPropertyChangeListener(listenerEM);
156:
157: }
158:
159: private void removeListeners() {
160: em.removePropertyChangeListener(listenerEM);
161: p.removePropertyChangeListener(
162: TopComponent.Registry.PROP_ACTIVATED_NODES, listenerTC);
163: }
164:
165: public void testOnceChange() {
166: SwingUtilities.invokeLater(new Runnable() {
167: public void run() {
168:
169: initListeners();
170:
171: // select a node
172: try {
173: em
174: .setSelectedNodes(new Node[] { nodes[3],
175: nodes[5] });
176:
177: Node[] activatedNodes = p.getActivatedNodes();
178:
179: // check synchronixzation after
180: assertArrays(
181: "ONCE CHANGE: getSelectedNodes equals getActivatedNodes.",
182: em.getSelectedNodes(), p
183: .getActivatedNodes());
184:
185: // lookup
186: Lookup.Result result = p.getLookup().lookup(
187: new Lookup.Template(Node.class));
188: Collection col = result.allInstances();
189: Iterator it = col.iterator();
190: Node[] lookupNodes = new Node[col.size()];
191: int i = 0;
192: while (it.hasNext()) {
193: lookupNodes[i] = (Node) it.next();
194: i++;
195: }
196:
197: // check nodes in lookup with acivated nodes
198: assertArrays(
199: "LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
200: lookupNodes, activatedNodes);
201:
202: } catch (PropertyVetoException pve) {
203: fail("Caught PropertyVetoException. msg:"
204: + pve.getMessage());
205: } finally {
206: removeListeners();
207: }
208:
209: }
210: });
211: }
212:
213: public void testIntensiveChange() {
214: SwingUtilities.invokeLater(new Runnable() {
215: public void run() {
216:
217: initListeners();
218:
219: // select a node
220: try {
221: for (int i = 3; i < 8; i++)
222: em.setSelectedNodes(new Node[] { nodes[i] });
223:
224: Node[] activatedNodes = p.getActivatedNodes();
225:
226: // check synchronixzation after
227: assertArrays(
228: "INTENSIVE CHANGES: getSelectedNodes equals getActivatedNodes.",
229: em.getSelectedNodes(), activatedNodes);
230:
231: // lookup
232: Lookup.Result result = p.getLookup().lookup(
233: new Lookup.Template(Node.class));
234: Collection col = result.allInstances();
235: Iterator it = col.iterator();
236: Node[] lookupNodes = new Node[col.size()];
237: int i = 0;
238: while (it.hasNext()) {
239: lookupNodes[i] = (Node) it.next();
240: i++;
241: }
242:
243: // check nodes in lookup with acivated nodes
244: assertArrays(
245: "LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
246: lookupNodes, em.getSelectedNodes());
247:
248: } catch (PropertyVetoException pve) {
249: fail("Caught PropertyVetoException. msg:"
250: + pve.getMessage());
251: } finally {
252: removeListeners();
253: }
254: }
255: });
256: }
257:
258: public void testIntensiveChangeWithLookup() {
259: SwingUtilities.invokeLater(new Runnable() {
260: public void run() {
261:
262: initListeners();
263: // select a node
264: try {
265: for (int i = 3; i < 8; i++)
266: em.setSelectedNodes(new Node[] { nodes[i] });
267: } catch (PropertyVetoException pve) {
268: fail("Caught PropertyVetoException. msg:"
269: + pve.getMessage());
270: } finally {
271: removeListeners();
272: }
273:
274: // get nodes from lookup
275: Lookup.Result result = p.getLookup().lookup(
276: new Lookup.Template(Node.class));
277: Collection col = result.allInstances();
278: Iterator it = col.iterator();
279: Node[] lookupNodes = new Node[col.size()];
280: int i = 0;
281: while (it.hasNext()) {
282: lookupNodes[i] = (Node) it.next();
283: i++;
284: }
285:
286: // check nodes in lookup with acivated nodes
287: assertArrays(
288: "LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
289: lookupNodes, p.getActivatedNodes());
290:
291: // check nodes in lookup with selected nodes
292: assertArrays(
293: "LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
294: lookupNodes, em.getSelectedNodes());
295: }
296: });
297: }
298:
299: public void testInteroperabilityWithTopComponentRegistry()
300: throws Exception {
301: final TopComponent tc = new TopComponent();
302: final Lookup.Result res = tc.getLookup().lookup(
303: new Lookup.Template(Node.class));
304:
305: assertNull("Empty arrays", tc.getActivatedNodes());
306: assertEquals("Empty list of nodes", 0, res.allInstances()
307: .size());
308:
309: class L implements PropertyChangeListener,
310: org.openide.util.LookupListener {
311: public Object[] expectedArray;
312: public java.util.ArrayList events = new java.util.ArrayList();
313:
314: public void resultChanged(org.openide.util.LookupEvent ev) {
315: events.add(ev);
316: }
317:
318: public void propertyChange(PropertyChangeEvent ev) {
319: if (TopComponent.Registry.PROP_CURRENT_NODES.equals(ev
320: .getPropertyName())) {
321: assertArrays("Should be the same", tc
322: .getActivatedNodes(), expectedArray);
323: assertArrays("Also in lookup. ", res.allInstances()
324: .toArray(), expectedArray);
325: }
326: events.add(ev);
327: }
328: }
329: L l = new L();
330: res.addLookupListener(l);
331:
332: tc.requestActive();
333: assertEquals("Really activated", tc, TopComponent.getRegistry()
334: .getActivated());
335: try {
336: TopComponent.getRegistry().addPropertyChangeListener(l);
337:
338: Node[] arr = { Node.EMPTY };
339: l.expectedArray = arr;
340: tc.setActivatedNodes(arr);
341:
342: Object[] ev = l.events.toArray();
343: assertEquals("Three events", 3, ev.length);
344: assertEquals("First is lookup change",
345: org.openide.util.LookupEvent.class, ev[0]
346: .getClass());
347: assertEquals("Second is prop change",
348: PropertyChangeEvent.class, ev[1].getClass());
349: assertEquals("Third is prop change",
350: PropertyChangeEvent.class, ev[2].getClass());
351:
352: assertEquals(TopComponent.Registry.PROP_ACTIVATED_NODES,
353: ((PropertyChangeEvent) ev[1]).getPropertyName());
354: assertEquals(TopComponent.Registry.PROP_CURRENT_NODES,
355: ((PropertyChangeEvent) ev[2]).getPropertyName());
356: } finally {
357: TopComponent.getRegistry().removePropertyChangeListener(l);
358: }
359: }
360:
361: private void assertArrays(String msg, Object[] arr1, Object[] arr2) {
362: // DEBUG MSG log content of arrays
363: // System.out.println("do ["+msg+"]: ");
364: // if (arr1 != null) for (int i = 0; i < arr1.length; i++) System.out.println("Arr1: " + i + ". " + arr1[i]);
365: // if (arr2 != null) for (int i = 0; i < arr2.length; i++) System.out.println("Arr2: " + i + ". " + arr2[i]);
366: // System.out.println("done!");
367: // END OF DEBUG MSG
368: if (arr1 == null && arr2 == null)
369: return;
370: if (arr1 == null) {
371: if (arr2.length == 0) {
372: return;
373: } else {
374: fail(msg + " BUT: Array1 was null Array2 was "
375: + java.util.Arrays.asList(arr2));
376: }
377: }
378: if (arr2 == null) {
379: if (arr1.length == 0) {
380: return;
381: } else {
382: fail(msg + " BUT: Array2 was null Array1 was "
383: + java.util.Arrays.asList(arr1));
384: }
385: }
386: if (arr1.length != arr2.length)
387: fail(msg + "Arrays have a diferent size. First: "
388: + java.util.Arrays.asList(arr1) + " second: "
389: + java.util.Arrays.asList(arr2));
390: //Arrays.sort (arr1);
391: //Arrays.sort (arr2);
392: for (int i = 0; i < arr1.length; i++) {
393: if (!arr1[i].equals(arr2[i])) {
394: fail(msg + " BUT: excepted: " + arr1[i] + ", was: "
395: + arr2[i]);
396: }
397: }
398: }
399:
400: }
|