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: /*
043: *
044: */
045:
046: package org.openide.explorer.view;
047:
048: import java.lang.ref.*;
049: import java.awt.event.*;
050: import java.awt.*;
051: import java.beans.PropertyVetoException;
052: import java.lang.reflect.InvocationTargetException;
053: import java.lang.reflect.Method;
054: import java.util.*;
055: import javax.swing.JList;
056: import javax.swing.SwingUtilities;
057: import javax.swing.tree.TreeSelectionModel;
058:
059: import org.openide.*;
060: import org.openide.explorer.*;
061: import org.openide.nodes.*;
062: import org.openide.util.*;
063: import junit.framework.*;
064: import junit.textui.TestRunner;
065: import org.netbeans.junit.*;
066:
067: /**
068: * Tests for control selection mode on TreeView (test on BeanTreeView).
069: * Note: here are used TreeView's method which hasn't been introduced yet,
070: * will be introduced when the enh 11928 will be implemented.
071: * Run test with attached patch in issue 11928.
072: *
073: * @author Jiri Rechtacek
074: * @see "#11928"
075: */
076: public class SelectionModeTest extends NbTestCase {
077:
078: ExplorerManager mgr;
079: TreeView tree;
080: Node[] singleSelection, contiguousSelection,
081: discontiguousSelection;
082:
083: public SelectionModeTest(String name) {
084: super (name);
085: }
086:
087: public static void main(String args[]) {
088: TestRunner.run(new NbTestSuite(SelectionModeTest.class));
089: System.exit(0);
090: }
091:
092: protected boolean runInEQ() {
093: return true;
094: }
095:
096: /** Create tree and a selection of nodes for test.
097: */
098: protected void setUp() {
099: // disable any lookup, to isolate the test from other registered
100: // subsystems like core/windows
101: System.setProperty("org.openide.util.Lookup", "-");
102:
103: // create tree:
104: // root +--- parent_one +--- one1
105: // |--- one2
106: // |--- parent_two +--- two1
107: // |--- leaf
108:
109: final Children parents = new Children.Array();
110: Node root = new AbstractNode(parents);
111: root.setName("root");
112:
113: tree = new BeanTreeView();
114: //tree = new ContextTreeView ();
115:
116: final ExplorerPanel p = new ExplorerPanel();
117: p.setName("SelectionModeTest");
118:
119: p.add(tree, BorderLayout.CENTER);
120: p.getExplorerManager().setRootContext(root);
121: p.open();
122:
123: final Children ones = new Children.Array();
124: Node parent_one = new AbstractNode(ones);
125: parent_one.setName("parent_one");
126: final Children twos = new Children.Array();
127: Node parent_two = new AbstractNode(twos);
128: parent_two.setName("parent_two");
129:
130: final Node one1 = new AbstractNode(Children.LEAF);
131: one1.setName("one1");
132:
133: final Node one2 = new AbstractNode(Children.LEAF);
134: one2.setName("one2");
135:
136: ones.add(new Node[] { one1, one2 });
137:
138: final Node two1 = new AbstractNode(Children.LEAF);
139: two1.setName("two1");
140:
141: twos.add(new Node[] { two1 });
142:
143: parents.add(new Node[] { parent_one, parent_two });
144:
145: // the test selections
146: singleSelection = new Node[] { parent_two };
147: contiguousSelection = new Node[] { one1, one2 };
148: discontiguousSelection = new Node[] { one2, two1 };
149:
150: mgr = p.getExplorerManager();
151: }
152:
153: /** Test set all nodes selections if the mode SINGLE_TREE_SELECTION is set.
154: * @throws Exception */
155: public void testSingleSelectionMode() throws Exception {
156: // try setSelectionMode; if not present then fail
157: setSelectionMode(tree, TreeSelectionModel.SINGLE_TREE_SELECTION);
158: PropertyVetoException exp = null;
159:
160: // single
161: try {
162: // have to be equal
163: assertTrue(
164: "[MODE: SINGLE_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
165: trySelection(mgr, singleSelection));
166: } catch (PropertyVetoException e) {
167: // no exp should be thrown
168: fail("[MODE: SINGLE_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); // NO18N
169: }
170:
171: // contiguous
172: try {
173: exp = null;
174: // cont' be equal
175: assertTrue(
176: "[MODE: SINGLE_TREE_SELECTION][NODES: two nodes contiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
177: !trySelection(mgr, contiguousSelection));
178: } catch (PropertyVetoException e) {
179: // exp should be thrown
180: exp = e;
181: } finally {
182: if (exp == null)
183: fail("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException was NOT thrown."); // NO18N
184: }
185:
186: // discontiguous
187: try {
188: exp = null;
189: assertTrue(
190: "[MODE: SINGLE_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
191: trySelection(mgr, discontiguousSelection));
192: } catch (PropertyVetoException e) {
193: // exp should be thrown
194: exp = e;
195: } finally {
196: if (exp == null)
197: fail("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); // NO18N
198: }
199:
200: }
201:
202: /** Test set all nodes selections if the mode CONTIGUOUS_TREE_SELECTION is set.
203: * @throws Exception */
204: public void testContigousSelection() throws Exception {
205: // try setSelectionMode; if not present then fail
206: setSelectionMode(tree,
207: TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
208: PropertyVetoException exp = null;
209:
210: // single
211: try {
212: // have to be equal
213: assertTrue(
214: "[MODE: CONTIGUOUS_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
215: trySelection(mgr, singleSelection));
216: } catch (PropertyVetoException e) {
217: // no exp should be thrown
218: fail("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); // NO18N
219: }
220:
221: // contiguous
222: try {
223: exp = null;
224: // have to be equal
225: assertTrue(
226: "[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
227: trySelection(mgr, contiguousSelection));
228: } catch (PropertyVetoException e) {
229: // no exp should be thrown
230: fail("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException can't be thrown."); // NO18N
231: }
232:
233: // discontiguous
234: try {
235: // cont' be equal
236: exp = null;
237: assertTrue(
238: "[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
239: trySelection(mgr, discontiguousSelection));
240: } catch (PropertyVetoException e) {
241: // exp should be thrown
242: exp = e;
243: } finally {
244: if (exp == null)
245: fail("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); // NO18N
246: }
247:
248: }
249:
250: /** Test set all nodes selections if the mode DISCONTIGUOUS_TREE_SELECTION is set.
251: * @throws Exception */
252: public void testDiscontigousSelection() throws Exception {
253: // try setSelectionMode; if not present then fail
254: setSelectionMode(tree,
255: TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
256: PropertyVetoException exp = null;
257:
258: // single
259: try {
260: // have to be equal
261: assertTrue(
262: "[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
263: trySelection(mgr, singleSelection));
264: } catch (PropertyVetoException e) {
265: // no exp should be thrown
266: fail("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); // NO18N
267: }
268:
269: // contiguous
270: try {
271: // have to be equal
272: assertTrue(
273: "[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] getSelectedNodes is NOT equal setSelectedNodes.", // NO18N
274: trySelection(mgr, contiguousSelection));
275: } catch (PropertyVetoException e) {
276: // no exp should be thrown
277: fail("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException can't be thrown."); // NO18N
278: }
279:
280: // discontiguous
281: try {
282: // have to be equal
283: assertTrue(
284: "[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", // NO18N
285: trySelection(mgr, discontiguousSelection));
286: } catch (PropertyVetoException e) {
287: // no exp should be thrown
288: fail("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); // NO18N
289: }
290:
291: }
292:
293: /** Try set array of nodes and check a array which is get back.
294: * @param mgr Explorer manager
295: * @param selected arrar of nodes which will be set
296: * @throws PropertyVetoException may be thrown from setSelecedNodes
297: * @return true if Explorer Manager returned same array as was set.*/
298: private boolean trySelection(ExplorerManager mgr, Node[] selected)
299: throws PropertyVetoException {
300: mgr.setSelectedNodes(selected);
301: if (selected != null) {
302: return Arrays.equals(selected, mgr.getSelectedNodes());
303: }
304: return true;
305: }
306:
307: /** Set selection on TreeView if the method is present. If not then the test failed.
308: * @param TreeView tree instance TreeView
309: * @param int mode selection mode */
310: private void setSelectionMode(TreeView tree, int mode) {
311: try {
312: Class c = tree.getClass();
313: Method m = c.getMethod("setSelectionMode",
314: new Class[] { Integer.TYPE });
315: m.invoke(tree, new Object[] { new Integer(mode) });
316: } catch (NoSuchMethodException nsme) {
317: fail("The method setSelectionMode can't be called on this object. See enh #11928.");
318: } catch (IllegalAccessException iae) {
319: fail("IllegalAccessException thrown from setSelectionMode.");
320: } catch (InvocationTargetException ite) {
321: fail("InvocationTargetException thrown from setSelectionMode.");
322: }
323: }
324:
325: }
|