0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Alexander T. Simbirtsev
0019: * @version $Revision$
0020: */package javax.swing.tree;
0021:
0022: import java.beans.PropertyChangeListener;
0023: import java.util.EventListener;
0024: import java.util.Vector;
0025: import javax.swing.SwingTestCase;
0026: import javax.swing.event.TreeSelectionEvent;
0027: import javax.swing.event.TreeSelectionListener;
0028: import javax.swing.event.SwingPropertyChangeSupportTest.FindableListener;
0029:
0030: public class DefaultTreeSelectionModelTest extends SwingTestCase {
0031: class ConcreteTreeSelectionListener extends FindableListener
0032: implements TreeSelectionListener {
0033: public TreeSelectionEvent event = null;
0034:
0035: public boolean fired = false;
0036:
0037: private final boolean debugOutput;
0038:
0039: ConcreteTreeSelectionListener() {
0040: super ();
0041: debugOutput = false;
0042: }
0043:
0044: ConcreteTreeSelectionListener(final boolean debugOutput) {
0045: super ();
0046: this .debugOutput = debugOutput;
0047: }
0048:
0049: public void valueChanged(TreeSelectionEvent e) {
0050: event = e;
0051: fired = true;
0052: if (debugOutput) {
0053: System.out.println(e);
0054: }
0055: }
0056:
0057: @Override
0058: public void reset() {
0059: event = null;
0060: fired = false;
0061: }
0062: }
0063:
0064: protected DefaultTreeSelectionModel model;
0065:
0066: @Override
0067: protected void setUp() throws Exception {
0068: super .setUp();
0069: model = new DefaultTreeSelectionModel();
0070: }
0071:
0072: @Override
0073: protected void tearDown() throws Exception {
0074: model = null;
0075: super .tearDown();
0076: }
0077:
0078: /*
0079: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.DefaultTreeSelectionModel()'
0080: */
0081: public void testDefaultTreeSelectionModel() {
0082: assertNotNull(model.listSelectionModel);
0083: assertNull(model.rowMapper);
0084: assertNull(model.selection);
0085: assertNull(model.changeSupport);
0086: assertNotNull(model.listenerList);
0087: assertEquals(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION,
0088: model.selectionMode);
0089: assertNull(model.leadPath);
0090: assertEquals(-1, model.leadIndex);
0091: assertEquals(-1, model.leadRow);
0092: }
0093:
0094: /*
0095: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.addPropertyChangeListener(PropertyChangeListener)'
0096: */
0097: public void testAddPropertyChangeListener() {
0098: PropertyChangeController changeListener1 = new PropertyChangeController();
0099: PropertyChangeController changeListener2 = new PropertyChangeController();
0100: PropertyChangeListener[] listenersArray = null;
0101: model.addPropertyChangeListener(null);
0102: listenersArray = model.getPropertyChangeListeners();
0103: assertEquals(0, listenersArray.length);
0104: model.addPropertyChangeListener(changeListener1);
0105: listenersArray = model.getPropertyChangeListeners();
0106: assertEquals(1, changeListener1.findMe(listenersArray));
0107: assertEquals(0, model.listenerList
0108: .getListeners(PropertyChangeListener.class).length);
0109: model.addPropertyChangeListener(changeListener2);
0110: listenersArray = model.getPropertyChangeListeners();
0111: assertEquals(1, changeListener1.findMe(listenersArray));
0112: assertEquals(1, changeListener2.findMe(listenersArray));
0113: model.addPropertyChangeListener(changeListener2);
0114: listenersArray = model.getPropertyChangeListeners();
0115: assertEquals(1, changeListener1.findMe(listenersArray));
0116: assertEquals(2, changeListener2.findMe(listenersArray));
0117: }
0118:
0119: /*
0120: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.removePropertyChangeListener(PropertyChangeListener)'
0121: */
0122: public void testRemovePropertyChangeListener() {
0123: PropertyChangeController changeListener1 = new PropertyChangeController();
0124: PropertyChangeController changeListener2 = new PropertyChangeController();
0125: PropertyChangeListener[] listenersArray = null;
0126: model.addPropertyChangeListener(changeListener1);
0127: model.addPropertyChangeListener(changeListener1);
0128: model.addPropertyChangeListener(changeListener2);
0129: listenersArray = model.getPropertyChangeListeners();
0130: assertEquals(3, listenersArray.length);
0131: model.removePropertyChangeListener(changeListener1);
0132: listenersArray = model.getPropertyChangeListeners();
0133: assertEquals(2, listenersArray.length);
0134: model.removePropertyChangeListener(changeListener1);
0135: listenersArray = model.getPropertyChangeListeners();
0136: assertEquals(1, listenersArray.length);
0137: model.removePropertyChangeListener(changeListener2);
0138: listenersArray = model.getPropertyChangeListeners();
0139: assertEquals(0, listenersArray.length);
0140: model.removePropertyChangeListener(null);
0141: listenersArray = model.getPropertyChangeListeners();
0142: assertEquals(0, listenersArray.length);
0143: }
0144:
0145: /*
0146: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getPropertyChangeListeners()'
0147: */
0148: public void testGetPropertyChangeListeners() {
0149: PropertyChangeController changeListener1 = new PropertyChangeController();
0150: PropertyChangeController changeListener2 = new PropertyChangeController();
0151: PropertyChangeListener[] listenersArray = null;
0152: listenersArray = model.getPropertyChangeListeners();
0153: assertEquals(0, listenersArray.length);
0154: model.addPropertyChangeListener(changeListener1);
0155: listenersArray = model.getPropertyChangeListeners();
0156: assertEquals(1, listenersArray.length);
0157: assertEquals(1, changeListener1.findMe(listenersArray));
0158: model.addPropertyChangeListener(changeListener2);
0159: listenersArray = model.getPropertyChangeListeners();
0160: assertEquals(2, listenersArray.length);
0161: assertEquals(1, changeListener1.findMe(listenersArray));
0162: assertEquals(1, changeListener2.findMe(listenersArray));
0163: }
0164:
0165: /*
0166: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.addTreeSelectionListener(TreeSelectionListener)'
0167: */
0168: public void testAddTreeSelectionListener() {
0169: ConcreteTreeSelectionListener listener1 = new ConcreteTreeSelectionListener();
0170: ConcreteTreeSelectionListener listener2 = new ConcreteTreeSelectionListener();
0171: TreeSelectionListener[] listenersArray = null;
0172: model.addTreeSelectionListener(listener1);
0173: listenersArray = model.getTreeSelectionListeners();
0174: assertTrue(listenersArray.length == 1);
0175: assertEquals(1, model.listenerList
0176: .getListeners(TreeSelectionListener.class).length);
0177: assertEquals(1,
0178: model.getListeners(TreeSelectionListener.class).length);
0179: assertTrue(listener1.findMe(listenersArray) > 0);
0180: model.addTreeSelectionListener(listener2);
0181: listenersArray = model.getTreeSelectionListeners();
0182: assertEquals(2, listenersArray.length);
0183: assertTrue(listener1.findMe(listenersArray) > 0);
0184: assertTrue(listener2.findMe(listenersArray) > 0);
0185: model.addTreeSelectionListener(listener2);
0186: listenersArray = model.getTreeSelectionListeners();
0187: assertEquals(3, listenersArray.length);
0188: }
0189:
0190: /*
0191: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.removeTreeSelectionListener(TreeSelectionListener)'
0192: */
0193: public void testRemoveTreeSelectionListener() {
0194: ConcreteTreeSelectionListener changeListener1 = new ConcreteTreeSelectionListener();
0195: ConcreteTreeSelectionListener changeListener2 = new ConcreteTreeSelectionListener();
0196: ConcreteTreeSelectionListener changeListener3 = new ConcreteTreeSelectionListener();
0197: TreeSelectionListener[] listenersArray = null;
0198: model.addTreeSelectionListener(changeListener1);
0199: model.addTreeSelectionListener(changeListener2);
0200: model.addTreeSelectionListener(changeListener3);
0201: listenersArray = model.getTreeSelectionListeners();
0202: assertEquals(3, listenersArray.length);
0203: assertEquals(1, changeListener1.findMe(listenersArray));
0204: assertEquals(1, changeListener2.findMe(listenersArray));
0205: assertEquals(1, changeListener3.findMe(listenersArray));
0206: model.removeTreeSelectionListener(changeListener2);
0207: listenersArray = model.getTreeSelectionListeners();
0208: assertEquals(2, listenersArray.length);
0209: assertEquals(1, changeListener1.findMe(listenersArray));
0210: assertEquals(0, changeListener2.findMe(listenersArray));
0211: assertEquals(1, changeListener3.findMe(listenersArray));
0212: model.removeTreeSelectionListener(changeListener1);
0213: listenersArray = model.getTreeSelectionListeners();
0214: assertEquals(1, listenersArray.length);
0215: assertEquals(1, changeListener3.findMe(listenersArray));
0216: model.removeTreeSelectionListener(changeListener3);
0217: listenersArray = model.getTreeSelectionListeners();
0218: assertEquals(0, listenersArray.length);
0219: }
0220:
0221: /*
0222: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getTreeSelectionListeners()'
0223: */
0224: public void testGetTreeSelectionListeners() {
0225: ConcreteTreeSelectionListener changeListener1 = new ConcreteTreeSelectionListener();
0226: ConcreteTreeSelectionListener changeListener2 = new ConcreteTreeSelectionListener();
0227: ConcreteTreeSelectionListener changeListener3 = new ConcreteTreeSelectionListener();
0228: TreeSelectionListener[] listenersArray = null;
0229: listenersArray = model.getTreeSelectionListeners();
0230: assertTrue(listenersArray != null && listenersArray.length == 0);
0231: model.addTreeSelectionListener(changeListener1);
0232: model.addTreeSelectionListener(changeListener2);
0233: model.addTreeSelectionListener(changeListener3);
0234: model.addTreeSelectionListener(changeListener2);
0235: listenersArray = model.getTreeSelectionListeners();
0236: assertTrue(listenersArray.length == 4);
0237: assertTrue(changeListener1.findMe(listenersArray) == 1);
0238: assertTrue(changeListener2.findMe(listenersArray) == 2);
0239: assertTrue(changeListener3.findMe(listenersArray) == 1);
0240: }
0241:
0242: public void testFireValueChange() {
0243: ConcreteTreeSelectionListener listener = new ConcreteTreeSelectionListener();
0244: TreeSelectionListener[] listenersArray = null;
0245: listenersArray = model.getTreeSelectionListeners();
0246: assertTrue(listenersArray != null && listenersArray.length == 0);
0247: model.addTreeSelectionListener(listener);
0248: TreePath path = new TreePath("asd");
0249: TreePath oldSelection = new TreePath("old");
0250: TreePath newSelection = new TreePath("new");
0251: Object source = new Object();
0252: boolean isNew = true;
0253: TreeSelectionEvent event = new TreeSelectionEvent(source, path,
0254: isNew, oldSelection, newSelection);
0255: model.fireValueChanged(event);
0256: assertSame(event, listener.event);
0257: listener.reset();
0258: model.fireValueChanged(null);
0259: assertTrue(listener.fired);
0260: assertNull(listener.event);
0261: }
0262:
0263: /*
0264: * Class under test for EventListener[] getListeners(Class)
0265: */
0266: public void testGetListenersClass() {
0267: PropertyChangeController changeListener1 = new PropertyChangeController();
0268: PropertyChangeController changeListener2 = new PropertyChangeController();
0269: TreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener();
0270: EventListener[] listenersArray = null;
0271: listenersArray = model
0272: .getListeners(TreeSelectionListener.class);
0273: assertEquals(0, listenersArray.length);
0274: listenersArray = model
0275: .getListeners(PropertyChangeListener.class);
0276: assertEquals(0, listenersArray.length);
0277: model.addPropertyChangeListener(changeListener1);
0278: model.addTreeSelectionListener(treeSelectionListener);
0279: model.addPropertyChangeListener(changeListener2);
0280: model.addPropertyChangeListener(changeListener2);
0281: listenersArray = model
0282: .getListeners(PropertyChangeListener.class);
0283: assertEquals(0, listenersArray.length);
0284: listenersArray = model
0285: .getListeners(TreeSelectionListener.class);
0286: assertEquals(1, listenersArray.length);
0287: model.removeTreeSelectionListener(treeSelectionListener);
0288: listenersArray = model
0289: .getListeners(TreeSelectionListener.class);
0290: assertEquals(0, listenersArray.length);
0291: }
0292:
0293: /*
0294: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.addSelectionPath(TreePath)'
0295: */
0296: public void testAddSelectionPath() {
0297: TreePath path1 = new TreePath("1");
0298: TreePath path2 = new TreePath("2");
0299: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
0300: false);
0301: model.addTreeSelectionListener(treeSelectionListener);
0302: model.addSelectionPath(path1);
0303: assertEquals(path1, treeSelectionListener.event.getPaths()[0]);
0304: assertTrue(treeSelectionListener.event.isAddedPath(path1));
0305: treeSelectionListener.reset();
0306: model.addSelectionPath(path1);
0307: assertNull(treeSelectionListener.event);
0308: model.addSelectionPath(path2);
0309: assertEquals(1, treeSelectionListener.event.getPaths().length);
0310: assertEquals(path2, treeSelectionListener.event.getPaths()[0]);
0311: assertTrue(treeSelectionListener.event.isAddedPath(path2));
0312: assertNotNull(treeSelectionListener.event
0313: .getOldLeadSelectionPath());
0314: assertNotNull(treeSelectionListener.event
0315: .getNewLeadSelectionPath());
0316: treeSelectionListener.reset();
0317: model.addSelectionPath(null);
0318: assertNull(treeSelectionListener.event);
0319: }
0320:
0321: /*
0322: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getSelectionPath()'
0323: */
0324: public void testGetSelectionPath() {
0325: TreePath path1 = new TreePath("1");
0326: TreePath path2 = new TreePath("2");
0327: assertNull(model.getSelectionPath());
0328: model.addSelectionPath(path1);
0329: assertEquals(path1, model.getSelectionPath());
0330: model.addSelectionPath(path2);
0331: assertEquals(path1, model.getSelectionPath());
0332: }
0333:
0334: /*
0335: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.isPathSelected(TreePath)'
0336: */
0337: public void testIsPathSelected() {
0338: TreePath path1 = new TreePath("1");
0339: TreePath path2 = new TreePath("2");
0340: TreePath path3 = new TreePath("3");
0341: TreePath path4 = new TreePath("4");
0342: assertFalse(model.isPathSelected(path1));
0343: assertFalse(model.isPathSelected(path2));
0344: assertFalse(model.isPathSelected(path3));
0345: assertFalse(model.isPathSelected(path4));
0346: model.addSelectionPath(path1);
0347: assertTrue(model.isPathSelected(path1));
0348: assertFalse(model.isPathSelected(path2));
0349: assertFalse(model.isPathSelected(path3));
0350: assertFalse(model.isPathSelected(path4));
0351: model.addSelectionPath(path2);
0352: assertTrue(model.isPathSelected(path1));
0353: assertTrue(model.isPathSelected(path2));
0354: assertFalse(model.isPathSelected(path3));
0355: assertFalse(model.isPathSelected(path4));
0356: model.addSelectionPaths(new TreePath[] { path3, path4 });
0357: assertTrue(model.isPathSelected(path1));
0358: assertTrue(model.isPathSelected(path2));
0359: assertTrue(model.isPathSelected(path3));
0360: assertTrue(model.isPathSelected(path4));
0361: assertFalse(model.isPathSelected(null));
0362: }
0363:
0364: /*
0365: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.addSelectionPaths(TreePath[])'
0366: */
0367: public void testAddSelectionPaths() {
0368: TreePath path1 = new TreePath("1");
0369: TreePath path2 = new TreePath("2");
0370: TreePath path3 = new TreePath("3");
0371: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
0372: false);
0373: model.addTreeSelectionListener(treeSelectionListener);
0374: model.addSelectionPaths(new TreePath[] { path1 });
0375: assertEquals(path1, treeSelectionListener.event.getPaths()[0]);
0376: assertTrue(treeSelectionListener.event.isAddedPath(path1));
0377: treeSelectionListener.reset();
0378: model.addSelectionPaths(new TreePath[] { path1 });
0379: assertNull(treeSelectionListener.event);
0380: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
0381: assertEquals(2, treeSelectionListener.event.getPaths().length);
0382: assertEquals(path2, treeSelectionListener.event.getPaths()[0]);
0383: assertEquals(path3, treeSelectionListener.event.getPaths()[1]);
0384: assertTrue(treeSelectionListener.event.isAddedPath(path2));
0385: assertTrue(treeSelectionListener.event.isAddedPath(path3));
0386: assertNotNull(treeSelectionListener.event
0387: .getOldLeadSelectionPath());
0388: assertNotNull(treeSelectionListener.event
0389: .getNewLeadSelectionPath());
0390: }
0391:
0392: public void testAddSelectionPaths_SelectionModes() {
0393: TreePath path1 = new TreePath("1");
0394: TreePath path2 = new TreePath("2");
0395: TreePath path3 = new TreePath("3");
0396: TreePath path4 = new TreePath("4");
0397: TreePath path5 = new TreePath("5");
0398: TreePath path6 = new TreePath("6");
0399: model.clearSelection();
0400: model.setRowMapper(new MyRowMapper());
0401: model
0402: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
0403: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
0404: assertEquals(1, model.getSelectionCount());
0405: model.clearSelection();
0406: model
0407: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0408: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
0409: assertEquals(3, model.getSelectionCount());
0410: model.clearSelection();
0411: model
0412: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
0413: model.addSelectionPaths(new TreePath[] { path1, path2, path4 });
0414: assertEquals(2, model.getSelectionCount());
0415: assertEquals(path1, model.getSelectionPaths()[0]);
0416: assertEquals(path2, model.getSelectionPaths()[1]);
0417: model.addSelectionPaths(new TreePath[] { path3 });
0418: assertEquals(3, model.getSelectionCount());
0419: model.addSelectionPaths(new TreePath[] { path5, path6 });
0420: assertEquals(2, model.getSelectionCount());
0421: assertEquals(path5, model.getSelectionPaths()[0]);
0422: assertEquals(path6, model.getSelectionPaths()[1]);
0423: model.addSelectionPaths(new TreePath[] { path1, path2, path3,
0424: path4 });
0425: assertEquals(6, model.getSelectionCount());
0426: }
0427:
0428: /*
0429: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getSelectionPaths()'
0430: */
0431: public void testGetSelectionPaths() {
0432: TreePath path1 = new TreePath("1");
0433: TreePath path2 = new TreePath("2");
0434: TreePath path3 = new TreePath("3");
0435: TreePath path4 = new TreePath("4");
0436: assertNull(model.getSelectionPaths());
0437: model.addSelectionPaths(new TreePath[] { path1 });
0438: assertEquals(1, model.getSelectionPaths().length);
0439: assertEquals(path1, model.getSelectionPaths()[0]);
0440: model.addSelectionPath(path4);
0441: assertEquals(2, model.getSelectionPaths().length);
0442: assertEquals(path1, model.getSelectionPaths()[0]);
0443: assertEquals(path4, model.getSelectionPaths()[1]);
0444: model.addSelectionPaths(new TreePath[] { path4, path2, path3 });
0445: assertEquals(4, model.getSelectionPaths().length);
0446: TreePath[] paths = model.getSelectionPaths();
0447: assertTrue(find(paths, path1));
0448: assertTrue(find(paths, path2));
0449: assertTrue(find(paths, path3));
0450: assertTrue(find(paths, path4));
0451: }
0452:
0453: private boolean find(final Object[] array, final Object o) {
0454: if (array == null) {
0455: return false;
0456: }
0457: for (int i = 0; i < array.length; i++) {
0458: if (o == null && array[i] == null || o.equals(array[i])) {
0459: return true;
0460: }
0461: }
0462: return false;
0463: }
0464:
0465: /*
0466: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getSelectionCount()'
0467: */
0468: public void testGetSelectionCount() {
0469: TreePath path1 = new TreePath("1");
0470: TreePath path2 = new TreePath("2");
0471: TreePath path3 = new TreePath("3");
0472: assertEquals(0, model.getSelectionCount());
0473: model.addSelectionPaths(new TreePath[] { path1 });
0474: assertEquals(1, model.getSelectionCount());
0475: model.addSelectionPaths(new TreePath[] { path1 });
0476: assertEquals(1, model.getSelectionCount());
0477: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
0478: assertEquals(3, model.getSelectionCount());
0479: }
0480:
0481: /*
0482: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.clearSelection()'
0483: */
0484: public void testClearSelection() {
0485: TreePath path1 = new TreePath("1");
0486: TreePath path2 = new TreePath("2");
0487: TreePath path3 = new TreePath("3");
0488: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
0489: false);
0490: model.addTreeSelectionListener(treeSelectionListener);
0491: assertEquals(0, model.getSelectionCount());
0492: model.clearSelection();
0493: assertNull(treeSelectionListener.event);
0494: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
0495: assertEquals(3, model.getSelectionCount());
0496: treeSelectionListener.reset();
0497: model.clearSelection();
0498: assertEquals(0, model.getSelectionCount());
0499: assertNull(treeSelectionListener.event
0500: .getNewLeadSelectionPath());
0501: assertEquals(3, treeSelectionListener.event.getPaths().length);
0502: assertFalse(treeSelectionListener.event.isAddedPath(0));
0503: assertFalse(treeSelectionListener.event.isAddedPath(1));
0504: assertFalse(treeSelectionListener.event.isAddedPath(2));
0505: }
0506:
0507: /*
0508: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getLeadSelectionPath()'
0509: */
0510: public void testGetLeadSelectionPath() {
0511: TreePath path1 = new TreePath("1");
0512: assertNull(model.getLeadSelectionPath());
0513: model.leadPath = path1;
0514: assertEquals(path1, model.getLeadSelectionPath());
0515: model.leadPath = null;
0516: assertNull(model.getLeadSelectionPath());
0517: }
0518:
0519: /*
0520: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getLeadSelectionRow()'
0521: */
0522: public void testGetLeadSelectionRow() {
0523: TreePath path1 = new TreePath("1");
0524: assertNull(model.getLeadSelectionPath());
0525: model.leadPath = path1;
0526: assertEquals(-1, model.getLeadSelectionRow());
0527: model.leadPath = null;
0528: assertEquals(-1, model.getLeadSelectionRow());
0529: model.setRowMapper(new MyRowMapper());
0530: assertEquals(-1, model.getLeadSelectionRow());
0531: model.leadPath = path1;
0532: assertEquals(-1, model.getLeadSelectionRow());
0533: model.selection = new TreePath[] { path1 };
0534: model.resetRowSelection();
0535: assertEquals(1, model.getLeadSelectionRow());
0536: model.leadPath = null;
0537: model.resetRowSelection();
0538: assertEquals(-1, model.getLeadSelectionRow());
0539: model.leadPath = path1;
0540: model.leadRow = 100;
0541: assertEquals(100, model.getLeadSelectionRow());
0542: model.resetRowSelection();
0543: assertEquals(1, model.getLeadSelectionRow());
0544: assertEquals(1, model.leadRow);
0545: }
0546:
0547: /*
0548: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getMaxSelectionRow()'
0549: */
0550: public void testGetMaxSelectionRow() {
0551: TreePath path1 = new TreePath("1");
0552: TreePath path2 = new TreePath("2");
0553: TreePath path3 = new TreePath("3");
0554: TreePath path4 = new TreePath("4");
0555: TreePath path5 = new TreePath("5");
0556: TreePath path6 = new TreePath("a");
0557: TreePath[] paths1 = new TreePath[] { path1, path2, path3,
0558: path4, path5 };
0559: TreePath[] paths3 = new TreePath[] { path2, path4, path1 };
0560: TreePath[] paths4 = new TreePath[] { path2, path1, path3, path5 };
0561: TreePath[] paths5 = new TreePath[] { path6, path4, path2 };
0562: TreePath[] paths6 = new TreePath[] { path6 };
0563: assertEquals(-1, model.getMaxSelectionRow());
0564: model.setSelectionPaths(paths1);
0565: assertEquals(-1, model.getMaxSelectionRow());
0566: model.setRowMapper(new MyRowMapper());
0567: assertEquals(5, model.getMaxSelectionRow());
0568: model.setSelectionPaths(paths3);
0569: assertEquals(4, model.getMaxSelectionRow());
0570: model.setSelectionPaths(paths4);
0571: assertEquals(5, model.getMaxSelectionRow());
0572: model.setSelectionPaths(paths5);
0573: assertEquals(4, model.getMaxSelectionRow());
0574: model.setSelectionPaths(paths6);
0575: assertEquals(-1, model.getMaxSelectionRow());
0576: model.setSelectionPaths(new TreePath[0]);
0577: assertEquals(-1, model.getMaxSelectionRow());
0578: }
0579:
0580: /*
0581: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getMinSelectionRow()'
0582: */
0583: public void testGetMinSelectionRow() {
0584: TreePath path1 = new TreePath("1");
0585: TreePath path2 = new TreePath("2");
0586: TreePath path3 = new TreePath("3");
0587: TreePath path4 = new TreePath("4");
0588: TreePath path5 = new TreePath("5");
0589: TreePath path6 = new TreePath("a");
0590: TreePath[] paths1 = new TreePath[] { path1, path2, path3,
0591: path4, path5 };
0592: TreePath[] paths3 = new TreePath[] { path2, path4, path5 };
0593: TreePath[] paths4 = new TreePath[] { path2, path1, path3, path5 };
0594: TreePath[] paths5 = new TreePath[] { path6, path5, path2 };
0595: TreePath[] paths6 = new TreePath[] { path6 };
0596: assertEquals(-1, model.getMinSelectionRow());
0597: model.setSelectionPaths(paths1);
0598: assertEquals(-1, model.getMinSelectionRow());
0599: model.setRowMapper(new MyRowMapper());
0600: assertEquals(1, model.getMinSelectionRow());
0601: model.setSelectionPaths(paths3);
0602: assertEquals(2, model.getMinSelectionRow());
0603: model.setSelectionPaths(paths4);
0604: assertEquals(1, model.getMinSelectionRow());
0605: model.setSelectionPaths(paths5);
0606: assertEquals(2, model.getMinSelectionRow());
0607: model.setSelectionPaths(paths6);
0608: assertEquals(-1, model.getMinSelectionRow());
0609: model.setSelectionPaths(new TreePath[0]);
0610: assertEquals(-1, model.getMinSelectionRow());
0611: }
0612:
0613: /*
0614: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getSelectionMode()'
0615: */
0616: public void testGetSelectionMode() {
0617: assertEquals(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION,
0618: model.getSelectionMode());
0619: }
0620:
0621: /*
0622: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.setSelectionMode(int)'
0623: */
0624: public void testSetSelectionMode() {
0625: int mode1 = TreeSelectionModel.SINGLE_TREE_SELECTION;
0626: int mode2 = TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION;
0627: int mode3 = TreeSelectionModel.CONTIGUOUS_TREE_SELECTION;
0628: int mode4 = 100;
0629: PropertyChangeController listener = new PropertyChangeController();
0630: model.addPropertyChangeListener(listener);
0631: model.setSelectionMode(mode1);
0632: listener
0633: .checkLastPropertyFired(
0634: model,
0635: "selectionMode",
0636: new Integer(
0637: TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION),
0638: new Integer(mode1));
0639: model.setSelectionMode(mode2);
0640: listener.checkLastPropertyFired(model, "selectionMode",
0641: new Integer(mode1), new Integer(mode2));
0642: model.setSelectionMode(mode3);
0643: listener.checkLastPropertyFired(model, "selectionMode",
0644: new Integer(mode2), new Integer(mode3));
0645: model.setSelectionMode(mode4);
0646: listener
0647: .checkLastPropertyFired(
0648: model,
0649: "selectionMode",
0650: new Integer(mode3),
0651: new Integer(
0652: TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION));
0653: TreePath path1 = new TreePath("1");
0654: // TreePath path2 = new TreePath("2");
0655: TreePath path3 = new TreePath("3");
0656: TreePath path4 = new TreePath("4");
0657: TreePath path5 = new TreePath("5");
0658: model
0659: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0660: model.addSelectionPaths(new TreePath[] { path1, path3, path4 });
0661: model
0662: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
0663: if (!isHarmony()) {
0664: model.insureRowContinuity();
0665: }
0666: assertEquals(1, model.getSelectionCount());
0667: assertEquals(path1, model.getSelectionPath());
0668: model
0669: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0670: model.addSelectionPaths(new TreePath[] { path1, path3, path5 });
0671: model
0672: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
0673: assertEquals(3, model.getSelectionCount());
0674: model
0675: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0676: model.setRowMapper(new MyRowMapper());
0677: model
0678: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
0679: if (!isHarmony()) {
0680: model.insureRowContinuity();
0681: }
0682: assertEquals(1, model.getSelectionCount());
0683: model
0684: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0685: model.setSelectionPaths(new TreePath[] { path1, path3, path4 });
0686: model
0687: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
0688: if (!isHarmony()) {
0689: model.insureRowContinuity();
0690: }
0691: assertEquals(1, model.getSelectionCount());
0692: }
0693:
0694: /*
0695: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getSelectionRows()'
0696: */
0697: public void testGetSelectionRows() {
0698: TreePath path1 = new TreePath("1");
0699: TreePath path2 = new TreePath("2");
0700: TreePath path3 = new TreePath("3");
0701: TreePath path4 = new TreePath("4");
0702: TreePath path5 = new TreePath("5");
0703: TreePath path6 = new TreePath("a");
0704: TreePath[] paths1 = new TreePath[] { path1, path2, path3,
0705: path4, path5 };
0706: TreePath[] paths3 = new TreePath[] { path2, path4, path1 };
0707: TreePath[] paths4 = new TreePath[] { path2, path1, path3, path5 };
0708: TreePath[] paths5 = new TreePath[] { path6, path4, path2 };
0709: TreePath[] paths6 = new TreePath[] { path6 };
0710: assertNull(model.getSelectionRows());
0711: model.setSelectionPaths(paths1);
0712: assertNull(model.getSelectionRows());
0713: model.setRowMapper(new MyRowMapper());
0714: assertEquals(5, model.getSelectionRows().length);
0715: model.setSelectionPaths(paths3);
0716: assertEquals(3, model.getSelectionRows().length);
0717: model.setSelectionPaths(paths4);
0718: assertEquals(4, model.getSelectionRows().length);
0719: model.setSelectionPaths(paths5);
0720: assertEquals(2, model.getSelectionRows().length);
0721: model.setSelectionPaths(paths6);
0722: assertNull(model.getSelectionRows());
0723: model.setSelectionPaths(new TreePath[0]);
0724: assertNull(model.getSelectionRows());
0725: }
0726:
0727: /*
0728: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.isRowSelected(int)'
0729: */
0730: public void testIsRowSelected() {
0731: TreePath path1 = new TreePath("1");
0732: TreePath path2 = new TreePath("2");
0733: TreePath path3 = new TreePath("3");
0734: TreePath path4 = new TreePath("4");
0735: TreePath path5 = new TreePath("5");
0736: TreePath path6 = new TreePath("a");
0737: TreePath[] paths1 = new TreePath[] { path1, path2, path3,
0738: path4, path5 };
0739: TreePath[] paths3 = new TreePath[] { path2, path4, path5 };
0740: TreePath[] paths6 = new TreePath[] { path6 };
0741: assertFalse(model.isRowSelected(-1));
0742: model.setSelectionPaths(paths1);
0743: assertFalse(model.isRowSelected(0));
0744: assertFalse(model.isRowSelected(1));
0745: model.setRowMapper(new MyRowMapper());
0746: assertFalse(model.isRowSelected(0));
0747: assertTrue(model.isRowSelected(1));
0748: model.setSelectionPaths(paths3);
0749: assertFalse(model.isRowSelected(1));
0750: assertTrue(model.isRowSelected(2));
0751: assertFalse(model.isRowSelected(3));
0752: assertTrue(model.isRowSelected(4));
0753: assertTrue(model.isRowSelected(5));
0754: model.setSelectionPaths(paths6);
0755: assertFalse(model.isRowSelected(-1));
0756: assertFalse(model.isRowSelected(0));
0757: }
0758:
0759: /*
0760: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.isSelectionEmpty()'
0761: */
0762: public void testIsSelectionEmpty() {
0763: TreePath path1 = new TreePath("1");
0764: assertTrue(model.isSelectionEmpty());
0765: model.addSelectionPath(path1);
0766: assertFalse(model.isSelectionEmpty());
0767: model.removeSelectionPath(path1);
0768: assertTrue(model.isSelectionEmpty());
0769: }
0770:
0771: /*
0772: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPath(TreePath)'
0773: */
0774: public void testRemoveSelectionPath() {
0775: TreePath path1 = new TreePath("1");
0776: TreePath path2 = new TreePath("2");
0777: TreePath path3 = new TreePath("3");
0778: TreePath path4 = new TreePath("4");
0779: assertEquals(0, model.getSelectionCount());
0780: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
0781: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
0782: false);
0783: model.addTreeSelectionListener(treeSelectionListener);
0784: model.removeSelectionPath(path4);
0785: assertNull(treeSelectionListener.event);
0786: model.removeSelectionPath(path2);
0787: assertEquals(2, model.getSelectionCount());
0788: assertNotNull(treeSelectionListener.event
0789: .getNewLeadSelectionPath());
0790: assertEquals(1, treeSelectionListener.event.getPaths().length);
0791: assertFalse(treeSelectionListener.event.isAddedPath(0));
0792: treeSelectionListener.reset();
0793: model.removeSelectionPath(path1);
0794: assertEquals(1, model.getSelectionCount());
0795: assertNotNull(treeSelectionListener.event
0796: .getNewLeadSelectionPath());
0797: assertEquals(1, treeSelectionListener.event.getPaths().length);
0798: assertFalse(treeSelectionListener.event.isAddedPath(0));
0799: treeSelectionListener.reset();
0800: model.removeSelectionPath(path3);
0801: assertEquals(0, model.getSelectionCount());
0802: assertNull(treeSelectionListener.event
0803: .getNewLeadSelectionPath());
0804: assertEquals(1, treeSelectionListener.event.getPaths().length);
0805: assertFalse(treeSelectionListener.event.isAddedPath(0));
0806: treeSelectionListener.reset();
0807: }
0808:
0809: /*
0810: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPaths(TreePath[])'
0811: */
0812: public void testRemoveSelectionPaths() {
0813: TreePath path1 = new TreePath("1");
0814: TreePath path2 = new TreePath("2");
0815: TreePath path3 = new TreePath("3");
0816: TreePath path4 = new TreePath("4");
0817: TreePath path5 = new TreePath("5");
0818: assertEquals(0, model.getSelectionCount());
0819: model.addSelectionPaths(new TreePath[] { path1, path2, path3,
0820: path4 });
0821: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
0822: false);
0823: model.addTreeSelectionListener(treeSelectionListener);
0824: model.removeSelectionPaths(new TreePath[] { path5, path5 });
0825: assertEquals(4, model.getSelectionCount());
0826: assertNull(treeSelectionListener.event);
0827: model
0828: .removeSelectionPaths(new TreePath[] { path1, path3,
0829: path5 });
0830: assertEquals(2, model.getSelectionCount());
0831: assertNotNull(treeSelectionListener.event
0832: .getNewLeadSelectionPath());
0833: assertEquals(2, treeSelectionListener.event.getPaths().length);
0834: assertFalse(treeSelectionListener.event.isAddedPath(0));
0835: assertFalse(treeSelectionListener.event.isAddedPath(1));
0836: treeSelectionListener.reset();
0837: model.removeSelectionPaths(null);
0838: assertEquals(2, model.getSelectionCount());
0839: assertNull(treeSelectionListener.event);
0840: model.removeSelectionPaths(new TreePath[] { path2, path4,
0841: path1, path5 });
0842: assertEquals(0, model.getSelectionCount());
0843: assertNull(treeSelectionListener.event
0844: .getNewLeadSelectionPath());
0845: assertEquals(2, treeSelectionListener.event.getPaths().length);
0846: assertFalse(treeSelectionListener.event.isAddedPath(0));
0847: assertFalse(treeSelectionListener.event.isAddedPath(1));
0848: treeSelectionListener.reset();
0849: }
0850:
0851: public void testRemoveSelectionPaths_SelectionModes() {
0852: TreePath path1 = new TreePath("1");
0853: TreePath path2 = new TreePath("2");
0854: TreePath path3 = new TreePath("3");
0855: TreePath path4 = new TreePath("4");
0856: assertEquals(0, model.getSelectionCount());
0857: model
0858: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0859: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
0860: assertEquals(3, model.getSelectionCount());
0861: model.removeSelectionPaths(new TreePath[] { path2 });
0862: assertEquals(2, model.getSelectionCount());
0863: model.setSelectionPaths(new TreePath[] { path1, path2, path4 });
0864: assertEquals(3, model.getSelectionCount());
0865: model.removeSelectionPaths(new TreePath[] { path2 });
0866: assertEquals(2, model.getSelectionCount());
0867: model
0868: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
0869: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
0870: assertEquals(1, model.getSelectionCount());
0871: assertEquals(path1, model.getSelectionPath());
0872: model.removeSelectionPaths(new TreePath[] { path1 });
0873: assertEquals(0, model.getSelectionCount());
0874: model
0875: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
0876: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
0877: assertEquals(3, model.getSelectionCount());
0878: model.removeSelectionPaths(new TreePath[] { path2 });
0879: assertEquals(2, model.getSelectionCount());
0880: model.setRowMapper(new MyRowMapper());
0881: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
0882: model.removeSelectionPaths(new TreePath[] { path1 });
0883: assertEquals(2, model.getSelectionCount());
0884: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
0885: model.removeSelectionPaths(new TreePath[] { path2 });
0886: assertEquals(0, model.getSelectionCount());
0887: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
0888: model.removeSelectionPaths(new TreePath[] { path3 });
0889: assertEquals(2, model.getSelectionCount());
0890: model.setSelectionPaths(new TreePath[] { path1, path2, path3,
0891: path4 });
0892: model.removeSelectionPaths(new TreePath[] { path1 });
0893: assertEquals(3, model.getSelectionCount());
0894: model.setSelectionPaths(new TreePath[] { path1, path2, path3,
0895: path4 });
0896: model.removeSelectionPaths(new TreePath[] { path2 });
0897: assertEquals(0, model.getSelectionCount());
0898: }
0899:
0900: /*
0901: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.resetRowSelection()'
0902: */
0903: public void testResetRowSelection() {
0904: TreePath path1 = new TreePath("1");
0905: TreePath path2 = new TreePath("2");
0906: TreePath path3 = new TreePath("3");
0907: model.setRowMapper(new MyRowMapper());
0908: model.leadPath = path1;
0909: model.selection = new TreePath[] { path1, path2, path3 };
0910: assertEquals(-1, model.getLeadSelectionRow());
0911: assertEquals(-1, model.getMaxSelectionRow());
0912: assertEquals(-1, model.getMinSelectionRow());
0913: model.resetRowSelection();
0914: assertEquals(1, model.getLeadSelectionRow());
0915: assertEquals(3, model.getMaxSelectionRow());
0916: assertEquals(1, model.getMinSelectionRow());
0917: model.leadPath = null;
0918: model.resetRowSelection();
0919: assertEquals(-1, model.getLeadSelectionRow());
0920: assertEquals(3, model.getMaxSelectionRow());
0921: assertEquals(1, model.getMinSelectionRow());
0922: model.leadPath = path1;
0923: model.leadRow = 100;
0924: assertEquals(100, model.getLeadSelectionRow());
0925: model.resetRowSelection();
0926: assertEquals(1, model.getLeadSelectionRow());
0927: assertEquals(1, model.leadRow);
0928: }
0929:
0930: /*
0931: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.setRowMapper(RowMapper)'
0932: */
0933: public void testSetRowMapper() {
0934: RowMapper mapper1 = new MyRowMapper();
0935: RowMapper mapper2 = new MyRowMapper();
0936: model.setRowMapper(mapper1);
0937: assertEquals(mapper1, model.getRowMapper());
0938: model.setRowMapper(mapper2);
0939: assertEquals(mapper2, model.getRowMapper());
0940: }
0941:
0942: /*
0943: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.getRowMapper()'
0944: */
0945: public void testGetRowMapper() {
0946: assertNull(model.getRowMapper());
0947: }
0948:
0949: /*
0950: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(TreePath[])'
0951: */
0952: public void testSetSelectionPaths() {
0953: TreePath path1 = new TreePath("1");
0954: TreePath path2 = new TreePath("2");
0955: TreePath path3 = new TreePath("3");
0956: TreePath path4 = new TreePath("4");
0957: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
0958: false);
0959: assertEquals(0, model.getSelectionCount());
0960: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
0961: assertEquals(3, model.getSelectionCount());
0962: model.addTreeSelectionListener(treeSelectionListener);
0963: model.setSelectionPaths(new TreePath[] { path3, path4 });
0964: assertEquals(2, model.getSelectionCount());
0965: assertNotNull(treeSelectionListener.event
0966: .getNewLeadSelectionPath());
0967: assertEquals(3, treeSelectionListener.event.getPaths().length);
0968: assertTrue(treeSelectionListener.event.isAddedPath(0));
0969: assertFalse(treeSelectionListener.event.isAddedPath(1));
0970: assertFalse(treeSelectionListener.event.isAddedPath(2));
0971: treeSelectionListener.reset();
0972: model.setSelectionPaths(new TreePath[] { path3, path4 });
0973: assertNull(treeSelectionListener.event);
0974: model.setSelectionPaths(null);
0975: assertEquals(0, model.getSelectionCount());
0976: assertNull(treeSelectionListener.event
0977: .getNewLeadSelectionPath());
0978: assertEquals(2, treeSelectionListener.event.getPaths().length);
0979: assertFalse(treeSelectionListener.event.isAddedPath(0));
0980: assertFalse(treeSelectionListener.event.isAddedPath(1));
0981: treeSelectionListener.reset();
0982: model.setSelectionPaths(new TreePath[] { path4, path4 });
0983: assertEquals(1, model.getSelectionCount());
0984: assertEquals(path4, treeSelectionListener.event
0985: .getNewLeadSelectionPath());
0986: assertEquals(1, treeSelectionListener.event.getPaths().length);
0987: assertTrue(treeSelectionListener.event.isAddedPath(0));
0988: treeSelectionListener.reset();
0989: }
0990:
0991: public void testSetSelectionPaths_SelectionModes() {
0992: TreePath path1 = new TreePath("1");
0993: TreePath path2 = new TreePath("2");
0994: TreePath path3 = new TreePath("3");
0995: TreePath path4 = new TreePath("4");
0996: assertEquals(0, model.getSelectionCount());
0997: model
0998: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0999: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
1000: assertEquals(3, model.getSelectionCount());
1001: model.setSelectionPaths(new TreePath[] { path1, path2, path4 });
1002: assertEquals(3, model.getSelectionCount());
1003: model
1004: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
1005: model.setSelectionPaths(new TreePath[] { path1, path3, path4 });
1006: assertEquals(1, model.getSelectionCount());
1007: assertEquals(path1, model.getSelectionPath());
1008: model.setSelectionPaths(new TreePath[] { path1, path2, path4 });
1009: assertEquals(1, model.getSelectionCount());
1010: assertEquals(path1, model.getSelectionPath());
1011: model.setSelectionPaths(new TreePath[] {});
1012: assertNull(model.getSelectionPath());
1013: model
1014: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
1015: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
1016: assertEquals(3, model.getSelectionCount());
1017: model.setRowMapper(new MyRowMapper());
1018: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
1019: assertEquals(3, model.getSelectionCount());
1020: model.setSelectionPaths(new TreePath[] { path1, path3, path4 });
1021: assertEquals(1, model.getSelectionCount());
1022: assertEquals(path1, model.getSelectionPath());
1023: model.setSelectionPaths(new TreePath[] { path1, path2, path4 });
1024: assertEquals(1, model.getSelectionCount());
1025: assertEquals(path1, model.getSelectionPaths()[0]);
1026: model.setSelectionPaths(new TreePath[] {});
1027: assertEquals(0, model.getSelectionCount());
1028: }
1029:
1030: /*
1031: * Test method for 'javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(TreePath)'
1032: */
1033: public void testSetSelectionPath() {
1034: TreePath path1 = new TreePath("1");
1035: TreePath path2 = new TreePath("2");
1036: TreePath path3 = new TreePath("3");
1037: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
1038: false);
1039: assertEquals(0, model.getSelectionCount());
1040: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
1041: assertEquals(3, model.getSelectionCount());
1042: model.addTreeSelectionListener(treeSelectionListener);
1043: model.setSelectionPath(path1);
1044: assertEquals(1, model.getSelectionCount());
1045: assertEquals(path1, treeSelectionListener.event
1046: .getNewLeadSelectionPath());
1047: assertEquals(2, treeSelectionListener.event.getPaths().length);
1048: assertFalse(treeSelectionListener.event.isAddedPath(0));
1049: assertFalse(treeSelectionListener.event.isAddedPath(1));
1050: treeSelectionListener.reset();
1051: model.setSelectionPath(path1);
1052: assertNull(treeSelectionListener.event);
1053: model.setSelectionPath(null);
1054: assertEquals(0, model.getSelectionCount());
1055: assertNull(treeSelectionListener.event
1056: .getNewLeadSelectionPath());
1057: assertEquals(1, treeSelectionListener.event.getPaths().length);
1058: assertFalse(treeSelectionListener.event.isAddedPath(0));
1059: treeSelectionListener.reset();
1060: }
1061:
1062: public void testCanPathsBeAdded() {
1063: TreePath path1 = new TreePath("1");
1064: TreePath path2 = new TreePath("2");
1065: TreePath path3 = new TreePath("3");
1066: TreePath path4 = new TreePath("4");
1067: assertTrue(model.canPathsBeAdded(null));
1068: model.clearSelection();
1069: model.setRowMapper(new MyRowMapper());
1070: model
1071: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
1072: assertTrue(model.canPathsBeAdded(new TreePath[] { path1, path2,
1073: path3 }));
1074: assertTrue(model
1075: .canPathsBeAdded(new TreePath[] { path1, path3 }));
1076: assertTrue(model.canPathsBeAdded(new TreePath[] {}));
1077: model.addSelectionPath(path4);
1078: assertTrue(model.canPathsBeAdded(new TreePath[] { path1, path2,
1079: path3 }));
1080: assertFalse(model
1081: .canPathsBeAdded(new TreePath[] { path1, path3 }));
1082: assertTrue(model.canPathsBeAdded(new TreePath[] { path4 }));
1083: assertTrue(model.canPathsBeAdded(new TreePath[] {}));
1084: model.clearSelection();
1085: model
1086: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
1087: assertTrue(model.canPathsBeAdded(new TreePath[] { path1, path2,
1088: path3 }));
1089: assertTrue(model
1090: .canPathsBeAdded(new TreePath[] { path1, path3 }));
1091: assertTrue(model.canPathsBeAdded(new TreePath[] { path4 }));
1092: assertTrue(model.canPathsBeAdded(new TreePath[] {}));
1093: model.addSelectionPath(path4);
1094: assertTrue(model.canPathsBeAdded(new TreePath[] { path1, path2,
1095: path3 }));
1096: assertTrue(model
1097: .canPathsBeAdded(new TreePath[] { path1, path3 }));
1098: assertTrue(model.canPathsBeAdded(new TreePath[] { path4 }));
1099: assertTrue(model.canPathsBeAdded(new TreePath[] {}));
1100: model
1101: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
1102: model.clearSelection();
1103: assertTrue(model.canPathsBeAdded(new TreePath[] { path1, path2,
1104: path3 }));
1105: assertTrue(model
1106: .canPathsBeAdded(new TreePath[] { path1, path3 }));
1107: assertTrue(model.canPathsBeAdded(new TreePath[] { path4 }));
1108: assertTrue(model.canPathsBeAdded(new TreePath[] {}));
1109: model.addSelectionPath(path4);
1110: assertTrue(model.canPathsBeAdded(new TreePath[] { path1, path2,
1111: path3 }));
1112: assertFalse(model
1113: .canPathsBeAdded(new TreePath[] { path1, path3 }));
1114: assertTrue(model.canPathsBeAdded(new TreePath[] { path4 }));
1115: assertTrue(model.canPathsBeAdded(new TreePath[] {}));
1116: }
1117:
1118: public void testCanPathsBeRemoved() {
1119: TreePath path1 = new TreePath("1");
1120: TreePath path2 = new TreePath("2");
1121: TreePath path3 = new TreePath("3");
1122: TreePath path4 = new TreePath("4");
1123: assertEquals(0, model.getSelectionCount());
1124: model
1125: .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
1126: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
1127: assertTrue(model.canPathsBeRemoved(new TreePath[] { path2 }));
1128: assertTrue(model.canPathsBeRemoved(new TreePath[] { path4 }));
1129: assertTrue(model.canPathsBeRemoved(new TreePath[] {}));
1130: model.setSelectionPaths(new TreePath[] { path1, path2, path4 });
1131: assertTrue(model.canPathsBeRemoved(new TreePath[] { path2 }));
1132: model
1133: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
1134: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
1135: assertTrue(model.canPathsBeRemoved(new TreePath[] { path1 }));
1136: model
1137: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
1138: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
1139: assertTrue(model.canPathsBeRemoved(new TreePath[] { path2 }));
1140: model.setRowMapper(new MyRowMapper());
1141: model.setSelectionPaths(new TreePath[] { path1, path2, path3 });
1142: assertTrue(model.canPathsBeRemoved(new TreePath[] { path1 }));
1143: assertFalse(model.canPathsBeRemoved(new TreePath[] { path2 }));
1144: assertTrue(model.canPathsBeRemoved(new TreePath[] { path3 }));
1145: model.setSelectionPaths(new TreePath[] { path1, path2, path3,
1146: path4 });
1147: assertTrue(model.canPathsBeRemoved(new TreePath[] { path1 }));
1148: assertFalse(model.canPathsBeRemoved(new TreePath[] { path2 }));
1149: assertFalse(model.canPathsBeRemoved(new TreePath[] { path3 }));
1150: assertTrue(model.canPathsBeRemoved(new TreePath[] { path4 }));
1151: }
1152:
1153: @SuppressWarnings("unchecked")
1154: public void testNotifyPathChange() {
1155: TreePath path1 = new TreePath("1");
1156: TreePath path2 = new TreePath("2");
1157: TreePath path3 = new TreePath("3");
1158: TreePath path4 = new TreePath("4");
1159: Vector holders = new Vector();
1160: holders.add(new PathPlaceHolder(path1, true));
1161: holders.add(new PathPlaceHolder(path2, true));
1162: holders.add(new PathPlaceHolder(path3, false));
1163: ConcreteTreeSelectionListener treeSelectionListener = new ConcreteTreeSelectionListener(
1164: false);
1165: assertEquals(0, model.getSelectionCount());
1166: model.addTreeSelectionListener(treeSelectionListener);
1167: model.notifyPathChange(holders, path4);
1168: assertEquals(0, model.getSelectionCount());
1169: assertNotNull(treeSelectionListener.event);
1170: assertEquals(path4, treeSelectionListener.event
1171: .getOldLeadSelectionPath());
1172: assertNull(treeSelectionListener.event
1173: .getNewLeadSelectionPath());
1174: assertEquals(3, treeSelectionListener.event.getPaths().length);
1175: assertTrue(treeSelectionListener.event.isAddedPath(0));
1176: assertTrue(treeSelectionListener.event.isAddedPath(1));
1177: assertTrue(treeSelectionListener.event.isAddedPath(1));
1178: treeSelectionListener.reset();
1179: }
1180:
1181: public void testUpdateLeadIndex() {
1182: TreePath path1 = new TreePath("1");
1183: TreePath path2 = new TreePath("2");
1184: TreePath path3 = new TreePath("3");
1185: TreePath path4 = new TreePath("4");
1186: TreePath path5 = new TreePath("5");
1187: model.addSelectionPaths(new TreePath[] { path1, path2, path3,
1188: path4 });
1189: assertEquals(3, model.leadIndex);
1190: model.leadIndex = 1000;
1191: model.updateLeadIndex();
1192: assertEquals(3, model.leadIndex);
1193: model.leadPath = null;
1194: model.updateLeadIndex();
1195: assertEquals(-1, model.leadIndex);
1196: model.leadPath = path1;
1197: model.updateLeadIndex();
1198: assertEquals(0, model.leadIndex);
1199: model.leadPath = path5;
1200: model.updateLeadIndex();
1201: assertEquals(-1, model.leadIndex);
1202: }
1203:
1204: class MyRowMapper implements RowMapper {
1205: public int[] getRowsForPaths(TreePath[] path) {
1206: if (path == null) {
1207: return new int[0];
1208: }
1209: int[] result = new int[path.length];
1210: for (int i = 0; i < result.length; i++) {
1211: String str = (String) path[i].getLastPathComponent();
1212: if (!str.equals("a")) {
1213: result[i] = Integer.valueOf(str).intValue();
1214: } else {
1215: result[i] = -1;
1216: }
1217: }
1218: return result;
1219: }
1220: };
1221:
1222: public void testArePathsContiguous() {
1223: TreePath path1 = new TreePath("1");
1224: TreePath path2 = new TreePath("2");
1225: TreePath path3 = new TreePath("3");
1226: TreePath path4 = new TreePath("4");
1227: TreePath path5 = new TreePath("5");
1228: TreePath path6 = new TreePath("a");
1229: TreePath[] paths1 = new TreePath[] { path1, path2, path3,
1230: path4, path5 };
1231: TreePath[] paths2 = new TreePath[] { path2, path4, path1,
1232: path5, path3 };
1233: TreePath[] paths3 = new TreePath[] { path1, path4, path5 };
1234: TreePath[] paths4 = new TreePath[] { path2, path1, path3, path5 };
1235: TreePath[] paths5 = new TreePath[] { path6, path1, path2 };
1236: TreePath[] paths6 = new TreePath[] { path6 };
1237: TreePath[] paths7 = new TreePath[] { path1, path2, path3,
1238: path4, path4, path5 };
1239: assertTrue(model.arePathsContiguous(paths1));
1240: model.setRowMapper(new MyRowMapper());
1241: assertTrue(model.arePathsContiguous(paths1));
1242: assertTrue(model.arePathsContiguous(paths2));
1243: assertFalse(model.arePathsContiguous(paths3));
1244: assertFalse(model.arePathsContiguous(paths4));
1245: assertFalse(model.arePathsContiguous(paths5));
1246: assertTrue(model.arePathsContiguous(paths6));
1247: assertTrue(model.arePathsContiguous(paths7));
1248: assertTrue(model.arePathsContiguous(new TreePath[0]));
1249: }
1250:
1251: public void testToString() {
1252: TreePath path1 = new TreePath("1");
1253: TreePath path2 = new TreePath("2");
1254: TreePath path3 = new TreePath("3");
1255: assertNotNull(model.toString());
1256: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
1257: assertNotNull(model.toString());
1258: }
1259:
1260: public void testClone() throws CloneNotSupportedException {
1261: TreePath path1 = new TreePath("1");
1262: TreePath path2 = new TreePath("2");
1263: TreePath path3 = new TreePath("3");
1264: model.addSelectionPaths(new TreePath[] { path1, path2, path3 });
1265: model
1266: .addTreeSelectionListener(new ConcreteTreeSelectionListener());
1267: RowMapper mapper = new MyRowMapper();
1268: model.setRowMapper(mapper);
1269: model
1270: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
1271: Object cloned = model.clone();
1272: assertTrue(cloned instanceof DefaultTreeSelectionModel);
1273: DefaultTreeSelectionModel clonedModel = (DefaultTreeSelectionModel) cloned;
1274: assertEquals(model.getSelectionCount(), clonedModel
1275: .getSelectionCount());
1276: assertEquals(model.getLeadSelectionPath(), clonedModel
1277: .getLeadSelectionPath());
1278: assertEquals(model.getSelectionPaths()[0], clonedModel
1279: .getSelectionPaths()[0]);
1280: assertEquals(model.getSelectionPaths()[1], clonedModel
1281: .getSelectionPaths()[1]);
1282: assertEquals(model.getSelectionPaths()[2], clonedModel
1283: .getSelectionPaths()[2]);
1284: assertEquals(model.getRowMapper(), clonedModel.getRowMapper());
1285: assertEquals(model.getLeadSelectionRow(), clonedModel
1286: .getLeadSelectionRow());
1287: assertEquals(model.getSelectionMode(), clonedModel
1288: .getSelectionMode());
1289: assertEquals(0, clonedModel.getTreeSelectionListeners().length);
1290: assertEquals(model.getMinSelectionRow(), clonedModel
1291: .getMinSelectionRow());
1292: assertEquals(model.getMaxSelectionRow(), clonedModel
1293: .getMaxSelectionRow());
1294: }
1295: }
|