001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.ArrayUtils;
005: import org.uispec4j.utils.AssertionFailureNotDetectedError;
006: import org.uispec4j.xml.EventLogger;
007:
008: import javax.swing.event.TreeSelectionEvent;
009: import javax.swing.event.TreeSelectionListener;
010: import javax.swing.tree.DefaultMutableTreeNode;
011: import javax.swing.tree.TreePath;
012:
013: public class TreeSelectionTest extends TreeTestCase {
014: protected SelectionListener selectionListener = new SelectionListener();
015:
016: protected void initTree() {
017: super .initTree();
018: jTree.addTreeSelectionListener(selectionListener);
019: }
020:
021: private void checkSelection(
022: DefaultMutableTreeNode[] expectedSelectedNodes,
023: String expectedSelectionLog) throws Exception {
024: TreePath[] expectedPaths = new TreePath[expectedSelectedNodes.length];
025: for (int i = 0; i < expectedSelectedNodes.length; i++) {
026: DefaultMutableTreeNode expectedNode = expectedSelectedNodes[i];
027: expectedPaths[i] = new TreePath(expectedNode.getPath());
028: }
029: ArrayUtils.assertEquals(expectedPaths, jTree
030: .getSelectionPaths());
031: selectionListener.assertEquals(expectedSelectionLog);
032: selectionListener.reset();
033: }
034:
035: public void testSelectRoot() throws Exception {
036: assertNull(jTree.getSelectionRows());
037: tree.selectRoot();
038: ArrayUtils.assertEquals(new int[] { 0 }, jTree
039: .getSelectionRows());
040: selectionListener.assertEquals("<log> "
041: + " <selection path='[obj:root]'/>" + "</log>");
042: }
043:
044: public void testSelectExistingPath() throws Exception {
045: assertNull(jTree.getSelectionRows());
046: tree.select("child1");
047: checkSelection(
048: new DefaultMutableTreeNode[] { child1Node },
049: "<log> "
050: + " <selection path='[obj:root, obj:child1]'/>"
051: + "</log>");
052:
053: tree.select("child1/child1_1");
054: checkSelection(
055: new DefaultMutableTreeNode[] { child1_1Node },
056: "<log> "
057: + " <selection path='[obj:root, obj:child1, obj:child1_1]'/>"
058: + "</log>");
059:
060: tree.select("child2");
061: checkSelection(
062: new DefaultMutableTreeNode[] { child2Node },
063: "<log> "
064: + " <selection path='[obj:root, obj:child2]'/>"
065: + "</log>");
066: }
067:
068: public void testPathsCanUseSubstringsOfNodeNames() throws Exception {
069: tree.select("d1/d1_1");
070: assertTrue(tree.selectionEquals("child1/child1_1"));
071: tree.select("2");
072: assertTrue(tree.selectionEquals("child2"));
073: tree.select("1/1");
074: assertTrue(tree.selectionEquals("child1/child1_1"));
075: }
076:
077: public void testPathsCanRevealNamingAmbiguitiesWhenUsingSubstrings()
078: throws Exception {
079: try {
080: tree.select("child/child1_1");
081: throw new AssertionFailureNotDetectedError();
082: } catch (AssertionFailedError e) {
083: assertEquals(
084: "Naming ambiguity: there are several 'child' under 'root'",
085: e.getMessage());
086: }
087: }
088:
089: public void testPathsCanRevealNamingAmbiguitiesWhenUsingExactNames()
090: throws Exception {
091: rootNode.add(new DefaultMutableTreeNode(child1));
092: try {
093: tree.select("child1/child1_1");
094: throw new AssertionFailureNotDetectedError();
095: } catch (AssertionFailedError e) {
096: assertEquals(
097: "Naming ambiguity: there are several 'child1' under 'root'",
098: e.getMessage());
099: }
100: }
101:
102: public void testMultiSelectionOfPaths() throws Exception {
103: assertNull(jTree.getSelectionRows());
104: assertTrue(tree.contentEquals("root\n" + " child1\n"
105: + " child1_1\n" + " child2\n"));
106: tree.addToSelection("child1");
107: tree.addToSelection("child2");
108: checkSelection(new DefaultMutableTreeNode[] { child1Node,
109: child2Node }, "<log> "
110: + " <selection path='[obj:root, obj:child1]'/>"
111: + " <selection path='[obj:root, obj:child2]'/>"
112: + "</log>");
113: tree.removeFromSelection("child1");
114: checkSelection(
115: new DefaultMutableTreeNode[] { child2Node },
116: "<log> "
117: + " <selection path='[obj:root, obj:child1]'/>"
118: + "</log>");
119:
120: tree.select("child1");
121: checkSelection(
122: new DefaultMutableTreeNode[] { child1Node },
123: "<log> "
124: + " <selection path='[obj:root, obj:child1]'/>"
125: + "</log>");
126:
127: tree.addToSelection("child1/child1_1");
128: tree.addToSelection("child2");
129: checkSelection(
130: new DefaultMutableTreeNode[] { child1Node,
131: child1_1Node, child2Node },
132: "<log> "
133: + " <selection path='[obj:root, obj:child1, obj:child1_1]'/>"
134: + " <selection path='[obj:root, obj:child2]'/>"
135: + "</log>");
136:
137: tree.select("child1");
138: tree.removeFromSelection("child1");
139: assertTrue(tree.selectionIsEmpty());
140: }
141:
142: public void testSelectingMultipleNodes() throws Exception {
143: assertNull(jTree.getSelectionRows());
144: assertTrue(tree.contentEquals("root\n" + " child1\n"
145: + " child1_1\n" + " child2\n"));
146: tree.select(new String[] { "child1", "child2" });
147: checkSelection(new DefaultMutableTreeNode[] { child1Node,
148: child2Node }, "<log> "
149: + " <selection path='[obj:root, obj:child1]'/>"
150: + " <selection path='[obj:root, obj:child2]'/>"
151: + "</log>");
152:
153: tree.select(new String[] { "child1" });
154: checkSelection(
155: new DefaultMutableTreeNode[] { child1Node },
156: "<log> "
157: + " <selection path='[obj:root, obj:child1]'/>"
158: + "</log>");
159:
160: tree.select(new String[] { "child1/child1_1", "child2" });
161: checkSelection(
162: new DefaultMutableTreeNode[] { child1_1Node, child2Node },
163: "<log> "
164: + " <selection path='[obj:root, obj:child1, obj:child1_1]'/>"
165: + " <selection path='[obj:root, obj:child2]'/>"
166: + "</log>");
167: }
168:
169: public void testSelectMultiplePathsWithInvalidPath()
170: throws Exception {
171: try {
172: tree.select(new String[] { "child1", "unknown" });
173: throw new AssertionFailureNotDetectedError();
174: } catch (AssertionFailedError e) {
175: assertEquals(Tree.badTreePath("unknown"), e.getMessage());
176: }
177: }
178:
179: public void testSelectNonexistingPath() throws Exception {
180: assertNull(jTree.getSelectionRows());
181: String path = "child1/unexistingElement";
182: try {
183: tree.select(path);
184: throw new AssertionFailureNotDetectedError();
185: } catch (AssertionFailedError e) {
186: assertEquals(Tree.badTreePath("child1/unexistingElement"),
187: e.getMessage());
188: }
189: assertNull(jTree.getSelectionRows());
190: }
191:
192: public void testSelectChildIndexUnderParent() throws Exception {
193: tree.select("", 0);
194: checkSelection(
195: new DefaultMutableTreeNode[] { child1Node },
196: "<log> "
197: + " <selection path='[obj:root, obj:child1]'/>"
198: + "</log>");
199:
200: tree.select("child1", 0);
201: checkSelection(
202: new DefaultMutableTreeNode[] { child1_1Node },
203: "<log> "
204: + " <selection path='[obj:root, obj:child1, obj:child1_1]'/>"
205: + "</log>");
206:
207: tree.select("", 1);
208: checkSelection(
209: new DefaultMutableTreeNode[] { child2Node },
210: "<log> "
211: + " <selection path='[obj:root, obj:child2]'/>"
212: + "</log>");
213:
214: tree.select("child1", 0);
215: tree.addToSelection("", 1);
216: checkSelection(
217: new DefaultMutableTreeNode[] { child1_1Node, child2Node },
218: "<log> "
219: + " <selection path='[obj:root, obj:child1, obj:child1_1]'/>"
220: + " <selection path='[obj:root, obj:child2]'/>"
221: + "</log>");
222: }
223:
224: public void testCheckSelection() throws Exception {
225: tree.select("child1");
226: assertTrue(tree.selectionEquals(new String[] { "child1" }));
227:
228: tree.select("child1/child1_1");
229: tree.addToSelection("child2");
230: assertTrue(tree.selectionEquals(new String[] {
231: "child1/child1_1", "child2" }));
232:
233: tree.select("child2");
234: assertTrue(tree.selectionEquals(new String[] { "child2" }));
235: }
236:
237: public void testCheckSelectionWhenSelectionIsNull()
238: throws Exception {
239: jTree.setSelectionPaths(null);
240: assertTrue(tree.selectionEquals(new String[0]));
241: tree.selectionIsEmpty();
242: }
243:
244: public void testCheckSelectionWithBadPath() throws Exception {
245: tree.select("child1/child1_1");
246: String pathToCheck = "child1/toto";
247: try {
248: assertTrue(tree.selectionEquals(pathToCheck));
249: throw new AssertionFailureNotDetectedError();
250: } catch (AssertionFailedError e) {
251: assertEquals(Tree.badTreePath("child1/toto"), e
252: .getMessage());
253: }
254: }
255:
256: public void testSelectChildrenWithSubstring() throws Exception {
257: assertTrue(tree.selectionIsEmpty());
258: tree.select("", "child");
259: assertTrue(tree.selectionEquals(new String[] { "child1",
260: "child2" }));
261: }
262:
263: public void testSelectChildrenWithNoMatch() throws Exception {
264: try {
265: tree.select("", "UNKNOWN");
266: throw new AssertionFailureNotDetectedError();
267: } catch (AssertionFailedError e) {
268: assertEquals("No children found", e.getMessage());
269: }
270: }
271:
272: public void testSelectionWithASpecificTreeCellValueConverter()
273: throws Exception {
274: tree.setCellValueConverter(new DummyTreeCellValueConverter());
275: String path1_1 = "_obj:child1_/_obj:child1_1_";
276: String path2 = "_obj:child2_";
277: tree.select(path1_1);
278: assertTrue(tree.selectionEquals(path1_1));
279: tree.addToSelection(path2);
280: assertTrue(tree
281: .selectionEquals(new String[] { path1_1, path2 }));
282: }
283:
284: private static class SelectionListener extends EventLogger
285: implements TreeSelectionListener {
286: public void valueChanged(TreeSelectionEvent e) {
287: if (e.getNewLeadSelectionPath() != null) {
288: log("selection").add("path", e.getPath().toString());
289: }
290: }
291: }
292: }
|