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.explorer.view;
043:
044: import java.beans.PropertyVetoException;
045: import javax.swing.ListSelectionModel;
046: import javax.swing.tree.TreeSelectionModel;
047: import junit.framework.Test;
048: import junit.framework.TestSuite;
049:
050: import org.netbeans.junit.NbTestCase;
051: import org.netbeans.junit.NbTestSuite;
052:
053: import org.openide.explorer.ExplorerManager;
054: import org.openide.explorer.ExplorerPanel;
055: import org.openide.explorer.view.*;
056:
057: import org.openide.nodes.AbstractNode;
058: import org.openide.nodes.Children;
059: import org.openide.nodes.Node;
060: import org.openide.nodes.Children.Array;
061:
062: /*
063: * RootContextTest.java tests a change the root context and set selected node
064: * in each root context.
065: *
066: * @author Jiri Rechtacek
067: */
068: public class RootContextTest extends NbTestCase {
069:
070: public RootContextTest(java.lang.String testName) {
071: super (testName);
072: }
073:
074: public static void main(java.lang.String[] args) {
075: junit.textui.TestRunner.run(suite());
076: }
077:
078: public static Test suite() {
079: TestSuite suite = new NbTestSuite(RootContextTest.class);
080: return suite;
081: }
082:
083: // helper variables
084: Node[] arr1, arr2;
085: Node root1, root2;
086: boolean initialized = false;
087:
088: // initialize the test variables
089: public void initTest() {
090:
091: // do init only once
092: if (initialized) {
093: return;
094: } else {
095: initialized = true;
096: }
097:
098: arr1 = new Node[3];
099: arr1[0] = new AbstractNode(Children.LEAF);
100: arr1[0].setName("One");
101:
102: arr1[1] = new AbstractNode(Children.LEAF);
103: arr1[1].setName("Two");
104:
105: arr1[2] = new AbstractNode(Children.LEAF);
106: arr1[2].setName("Three");
107:
108: Array ch1 = new Array();
109: ch1.add(arr1);
110:
111: arr2 = new Node[3];
112: arr2[0] = new AbstractNode(Children.LEAF);
113: arr2[0].setName("Aaa");
114:
115: arr2[1] = new AbstractNode(Children.LEAF);
116: arr2[1].setName("Bbb");
117:
118: arr2[2] = new AbstractNode(Children.LEAF);
119: arr2[2].setName("Ccc");
120:
121: Array ch2 = new Array();
122: ch2.add(arr2);
123:
124: root1 = new AbstractNode(ch1);
125: root1.setName("Root1");
126: root2 = new AbstractNode(ch2);
127: root2.setName("Root2");
128:
129: }
130:
131: /** Run all tests in AWT thread */
132: protected boolean runInEQ() {
133: return true;
134: }
135:
136: // assure the node selections with given manager
137: public void doViewTest(final ExplorerManager mgr) throws Exception {
138: mgr.setRootContext(root1);
139: mgr.setSelectedNodes(new Node[] { arr1[0], arr1[2] });
140:
141: Node[] selNodes = mgr.getSelectedNodes();
142: assertEquals("Root context is ", "Root1", mgr.getRootContext()
143: .getName());
144: assertEquals("Count of the selected node is ", 2,
145: selNodes.length);
146: // pending: an order migth be different
147: //Arrays.sort (selNodes);
148: assertEquals("Selected node is ", "One", selNodes[0].getName());
149: assertEquals("Selected node is ", "Three", selNodes[1]
150: .getName());
151:
152: mgr.setRootContext(root2);
153: mgr.setSelectedNodes(new Node[] { arr2[1] });
154:
155: selNodes = mgr.getSelectedNodes();
156: assertEquals("Root context is ", "Root2", mgr.getRootContext()
157: .getName());
158: assertEquals("Count of the selected node is ", 1,
159: selNodes.length);
160: assertEquals("Selected node is ", "Bbb", selNodes[0].getName());
161:
162: }
163:
164: // test for each type of view
165:
166: public void testBeanTreeView() throws Exception {
167:
168: initTest();
169:
170: TreeView view = new BeanTreeView();
171: view
172: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
173:
174: ExplorerPanel panel = new ExplorerPanel();
175: //#47021
176: panel.setName("preferredID");
177: ExplorerManager mgr = panel.getExplorerManager();
178:
179: panel.add(view);
180: panel.open();
181:
182: doViewTest(mgr);
183:
184: }
185:
186: public void testContextTreeView() throws Exception {
187:
188: initTest();
189:
190: TreeView view = new ContextTreeView();
191: view
192: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
193:
194: ExplorerPanel panel = new ExplorerPanel();
195: //#47021
196: panel.setName("preferredID");
197: ExplorerManager mgr = panel.getExplorerManager();
198:
199: panel.add(view);
200: panel.open();
201:
202: doViewTest(mgr);
203:
204: }
205:
206: public void testTreeTableView() throws Exception {
207:
208: initTest();
209:
210: TreeTableView view = new TreeTableView();
211: view
212: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
213:
214: ExplorerPanel panel = new ExplorerPanel();
215: //#47021
216: panel.setName("preferredID");
217: ExplorerManager mgr = panel.getExplorerManager();
218:
219: panel.add(view);
220: panel.open();
221:
222: doViewTest(mgr);
223:
224: }
225:
226: public void testListView() throws Exception {
227:
228: initTest();
229:
230: ListView view = new ListView();
231: view
232: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
233:
234: ExplorerPanel panel = new ExplorerPanel();
235: //#47021
236: panel.setName("preferredID");
237: ExplorerManager mgr = panel.getExplorerManager();
238:
239: panel.add(view);
240: panel.open();
241:
242: doViewTest(mgr);
243:
244: }
245:
246: public void testListTableView() throws Exception {
247:
248: initTest();
249:
250: ListTableView view = new ListTableView();
251: view
252: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
253:
254: ExplorerPanel panel = new ExplorerPanel();
255: //#47021
256: panel.setName("preferredID");
257: ExplorerManager mgr = panel.getExplorerManager();
258:
259: panel.add(view);
260: panel.open();
261:
262: doViewTest(mgr);
263:
264: }
265:
266: }
|