001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.tree;
022:
023: import java.util.*;
024: import java.sql.*;
025: import junit.framework.*;
026: import org.apache.log4j.*;
027: import com.methodhead.test.*;
028: import javax.swing.tree.*;
029:
030: public class TreeTest extends TestCase {
031:
032: static {
033: TestUtils.initLogger();
034: }
035:
036: public TreeTest(String name) {
037: super (name);
038: }
039:
040: private Tree tree = null;
041: private DefaultMutableTreeNode node = null;
042: private DefaultMutableTreeNode node2 = null;
043: private DefaultMutableTreeNode node3 = null;
044: private DefaultMutableTreeNode subnode = null;
045:
046: private DefaultMutableTreeNode node1_ = null;
047: private DefaultMutableTreeNode node2_ = null;
048: private DefaultMutableTreeNode node3_ = null;
049: private DefaultMutableTreeNode node4_ = null;
050: private DefaultMutableTreeNode node5_ = null;
051:
052: protected void createData() {
053:
054: // Node1
055: // Node2
056: // Node3
057: // Node4
058: // Node5
059: node1_ = new DefaultMutableTreeNode();
060: node1_.setUserObject("Node1");
061: node2_ = new DefaultMutableTreeNode();
062: node2_.setUserObject("Node2");
063: node3_ = new DefaultMutableTreeNode();
064: node3_.setUserObject("Node3");
065: node4_ = new DefaultMutableTreeNode();
066: node4_.setUserObject("Node4");
067: node5_ = new DefaultMutableTreeNode();
068: node5_.setUserObject("Node5");
069:
070: node1_.add(node2_);
071: node2_.add(node3_);
072: node1_.add(node4_);
073: node1_.add(node5_);
074: }
075:
076: protected void setUp() {
077: try {
078: } catch (Exception e) {
079: fail(e.getMessage());
080: }
081: }
082:
083: protected void tearDown() {
084: }
085:
086: public void testGetSetRoot() {
087: try {
088: Tree tree = new Tree();
089:
090: assertNull(tree.getRoot());
091:
092: DefaultMutableTreeNode node = new DefaultMutableTreeNode();
093:
094: tree.setRoot(node);
095:
096: assertEquals(node, tree.getRoot());
097: } catch (Exception e) {
098: e.printStackTrace();
099: fail();
100: }
101: }
102:
103: public void testInsertUnder() {
104: try {
105: Tree tree = null;
106: DefaultMutableTreeNode node = null;
107:
108: //
109: // insert under root
110: //
111: createData();
112: tree = new Tree();
113: tree.setRoot(node1_);
114:
115: node = new DefaultMutableTreeNode();
116: tree.insertUnder(node1_, node);
117:
118: assertEquals(4, node1_.getChildCount());
119: assertEquals(node, node1_.getChildAt(3));
120:
121: //
122: // insert under another node
123: //
124: createData();
125: tree = new Tree();
126: tree.setRoot(node1_);
127:
128: node = new DefaultMutableTreeNode();
129: tree.insertUnder(node3_, node);
130:
131: assertEquals(1, node3_.getChildCount());
132: assertEquals(node, node3_.getChildAt(0));
133: } catch (Exception e) {
134: e.printStackTrace();
135: fail();
136: }
137: }
138:
139: public void testInsertAfter() {
140: try {
141: Tree tree = null;
142: DefaultMutableTreeNode node = null;
143:
144: //
145: // insert after root
146: //
147: createData();
148: tree = new Tree();
149: tree.setRoot(node1_);
150:
151: try {
152: node = new DefaultMutableTreeNode();
153: tree.insertAfter(node1_, node);
154: fail("Inserted after root.");
155: } catch (Exception e) {
156: // success
157: }
158:
159: //
160: // insert after another node
161: //
162: createData();
163: tree = new Tree();
164: tree.setRoot(node1_);
165:
166: node = new DefaultMutableTreeNode();
167: tree.insertAfter(node2_, node);
168:
169: assertEquals(4, node1_.getChildCount());
170: assertEquals(node, node1_.getChildAt(1));
171:
172: //
173: // insert after a last node
174: //
175: createData();
176: tree = new Tree();
177: tree.setRoot(node1_);
178:
179: node = new DefaultMutableTreeNode();
180: tree.insertAfter(node5_, node);
181:
182: assertEquals(4, node1_.getChildCount());
183: assertEquals(node, node1_.getChildAt(3));
184: } catch (Exception e) {
185: e.printStackTrace();
186: fail();
187: }
188: }
189:
190: public void testInsertBefore() {
191: try {
192: Tree tree = null;
193: DefaultMutableTreeNode node = null;
194:
195: //
196: // insert before root
197: //
198: createData();
199: tree = new Tree();
200: tree.setRoot(node1_);
201:
202: try {
203: node = new DefaultMutableTreeNode();
204: tree.insertBefore(node1_, node);
205: fail("Inserted before root.");
206: } catch (Exception e) {
207: // success
208: }
209:
210: //
211: // insert before another node
212: //
213: createData();
214: tree = new Tree();
215: tree.setRoot(node1_);
216:
217: node = new DefaultMutableTreeNode();
218: tree.insertBefore(node2_, node);
219:
220: assertEquals(4, node1_.getChildCount());
221: assertEquals(node, node1_.getChildAt(0));
222:
223: //
224: // insert before a last node
225: //
226: createData();
227: tree = new Tree();
228: tree.setRoot(node1_);
229:
230: node = new DefaultMutableTreeNode();
231: tree.insertBefore(node5_, node);
232:
233: assertEquals(4, node1_.getChildCount());
234: assertEquals(node, node1_.getChildAt(2));
235: } catch (Exception e) {
236: e.printStackTrace();
237: fail();
238: }
239: }
240:
241: public void testRemove() {
242: try {
243: Tree tree = null;
244: DefaultMutableTreeNode node = null;
245:
246: //
247: // remove node 2
248: //
249: createData();
250: tree = new Tree();
251: tree.setRoot(node1_);
252:
253: tree.remove(node2_);
254:
255: assertEquals(2, node1_.getChildCount());
256:
257: node = (DefaultMutableTreeNode) tree.getRoot()
258: .getChildAt(0);
259:
260: assertEquals(node4_, node);
261:
262: node = (DefaultMutableTreeNode) tree.getRoot()
263: .getChildAt(1);
264:
265: assertEquals(node5_, node);
266:
267: //
268: // remove the root
269: //
270: createData();
271: tree = new Tree();
272: tree.setRoot(node1_);
273: tree.remove(node1_);
274:
275: assertNull(tree.getRoot());
276: } catch (Exception e) {
277: e.printStackTrace();
278: fail();
279: }
280: }
281:
282: public void testRemoveAndPromoteChildren() {
283: try {
284: Tree tree = null;
285: DefaultMutableTreeNode node = null;
286: DefaultMutableTreeNode subnode = null;
287:
288: //
289: // remove node 2
290: //
291: createData();
292: tree = new Tree();
293: tree.setRoot(node1_);
294:
295: tree.removeAndPromoteChildren(node2_);
296:
297: assertEquals(3, node1_.getChildCount());
298:
299: node = (DefaultMutableTreeNode) tree.getRoot()
300: .getChildAt(0);
301:
302: assertEquals(node4_, node);
303:
304: node = (DefaultMutableTreeNode) tree.getRoot()
305: .getChildAt(1);
306:
307: assertEquals(node5_, node);
308:
309: node = (DefaultMutableTreeNode) tree.getRoot()
310: .getChildAt(2);
311:
312: assertEquals(node3_, node);
313:
314: //
315: // remove the root
316: //
317: createData();
318: tree = new Tree();
319: tree.setRoot(node1_);
320: tree.removeAndPromoteChildren(node1_);
321:
322: node = tree.getRoot();
323:
324: assertNotNull(node);
325: assertEquals(node2_, node);
326:
327: subnode = (DefaultMutableTreeNode) node.getChildAt(0);
328:
329: assertEquals(node4_, subnode);
330:
331: subnode = (DefaultMutableTreeNode) node.getChildAt(1);
332:
333: assertEquals(node5_, subnode);
334:
335: subnode = (DefaultMutableTreeNode) node.getChildAt(2);
336:
337: assertEquals(node3_, subnode);
338: } catch (Exception e) {
339: e.printStackTrace();
340: fail();
341: }
342: }
343:
344: public void testMoveUnder() {
345: try {
346: createData();
347: tree = new Tree();
348: tree.setRoot(node1_);
349:
350: try {
351: tree.moveUnder(node2_, node1_);
352: fail("Exception not thrown.");
353: } catch (Exception e) {
354: // success
355: }
356:
357: try {
358: tree.moveUnder(node2_, node2_);
359: fail("Exception not thrown.");
360: } catch (Exception e) {
361: // success
362: }
363:
364: tree.moveUnder(node4_, node3_);
365:
366: assertEquals(0, node2_.getChildCount());
367: assertEquals(1, node4_.getChildCount());
368: assertEquals(node3_, node4_.getChildAt(0));
369: } catch (Exception e) {
370: e.printStackTrace();
371: fail();
372: }
373: }
374:
375: public void testMoveAfter() {
376: try {
377: createData();
378: tree = new Tree();
379: tree.setRoot(node1_);
380:
381: try {
382: tree.moveAfter(node2_, node1_);
383: fail("Exception not thrown.");
384: } catch (Exception e) {
385: // success
386: }
387:
388: try {
389: tree.moveAfter(node2_, node2_);
390: fail("Exception not thrown.");
391: } catch (Exception e) {
392: // success
393: }
394:
395: try {
396: tree.moveAfter(node3_, node2_);
397: fail("Exception not thrown.");
398: } catch (Exception e) {
399: // success
400: }
401:
402: tree.moveAfter(node4_, node2_);
403:
404: assertEquals(3, node1_.getChildCount());
405: assertEquals(node4_, node1_.getChildAt(0));
406: assertEquals(node2_, node1_.getChildAt(1));
407: } catch (Exception e) {
408: e.printStackTrace();
409: fail();
410: }
411: }
412:
413: public void testMoveBefore() {
414: try {
415: createData();
416: tree = new Tree();
417: tree.setRoot(node1_);
418:
419: try {
420: tree.moveAfter(node2_, node1_);
421: fail("Exception not thrown.");
422: } catch (Exception e) {
423: // success
424: }
425:
426: try {
427: tree.moveAfter(node2_, node2_);
428: fail("Exception not thrown.");
429: } catch (Exception e) {
430: // success
431: }
432:
433: try {
434: tree.moveAfter(node3_, node2_);
435: fail("Exception not thrown.");
436: } catch (Exception e) {
437: // success
438: }
439:
440: tree.moveBefore(node2_, node4_);
441:
442: assertEquals(3, node1_.getChildCount());
443: assertEquals(node4_, node1_.getChildAt(0));
444: assertEquals(node2_, node1_.getChildAt(1));
445: } catch (Exception e) {
446: e.printStackTrace();
447: fail();
448: }
449: }
450:
451: public void testValidateMove() {
452: try {
453: createData();
454: tree = new Tree();
455: tree.setRoot(node1_);
456:
457: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEROOT, tree
458: .validateMove(node1_, node2_, Tree.POSITION_UNDER));
459: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEROOT, tree
460: .validateMove(node1_, node2_, Tree.POSITION_BEFORE));
461: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEROOT, tree
462: .validateMove(node1_, node2_, Tree.POSITION_AFTER));
463:
464: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEBEFOREROOT, tree
465: .validateMove(node2_, node1_, Tree.POSITION_BEFORE));
466: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEAFTERROOT, tree
467: .validateMove(node2_, node1_, Tree.POSITION_AFTER));
468:
469: assertEquals(Tree.INVALIDMOVE_CANNOTMOVENEARSELF, tree
470: .validateMove(node2_, node2_, Tree.POSITION_AFTER));
471:
472: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF, tree
473: .validateMove(node2_, node3_, Tree.POSITION_UNDER));
474: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF, tree
475: .validateMove(node2_, node3_, Tree.POSITION_BEFORE));
476: assertEquals(Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF, tree
477: .validateMove(node2_, node3_, Tree.POSITION_AFTER));
478:
479: assertEquals(Tree.VALIDMOVE, tree.validateMove(node2_,
480: node4_, Tree.POSITION_UNDER));
481: } catch (Exception e) {
482: e.printStackTrace();
483: fail();
484: }
485: }
486:
487: public void testSort() {
488: try {
489: createData();
490: tree = new Tree();
491: tree.setRoot(node1_);
492:
493: //
494: // re-order tree
495: //
496: tree.moveBefore(node2_, node4_);
497: Tree.sort(node1_, new Comparator() {
498: public int compare(Object o1, Object o2) {
499: DefaultMutableTreeNode n1 = (DefaultMutableTreeNode) o1;
500: DefaultMutableTreeNode n2 = (DefaultMutableTreeNode) o2;
501: return ((String) n1.getUserObject())
502: .compareTo((String) n2.getUserObject());
503: }
504: });
505:
506: assertEquals(3, node1_.getChildCount());
507:
508: node = (DefaultMutableTreeNode) tree.getRoot()
509: .getChildAt(0);
510: assertEquals(node2_, node);
511:
512: node = (DefaultMutableTreeNode) tree.getRoot()
513: .getChildAt(1);
514: assertEquals(node4_, node);
515:
516: node = (DefaultMutableTreeNode) tree.getRoot()
517: .getChildAt(2);
518: assertEquals(node5_, node);
519: } catch (Exception e) {
520: e.printStackTrace();
521: fail();
522: }
523: }
524:
525: public void testCopy() {
526: try {
527: createData();
528: tree = new Tree();
529:
530: node = Tree.copy(node1_);
531:
532: assertTrue(node != node1_);
533: assertEquals("Node1", node.getUserObject());
534: assertEquals(3, node.getChildCount());
535:
536: node2 = (DefaultMutableTreeNode) node.getChildAt(0);
537: assertEquals("Node2", node2.getUserObject());
538: assertEquals(1, node2.getChildCount());
539:
540: node3 = (DefaultMutableTreeNode) node2.getChildAt(0);
541: assertEquals("Node3", node3.getUserObject());
542: assertEquals(0, node3.getChildCount());
543:
544: node2 = (DefaultMutableTreeNode) node.getChildAt(1);
545: assertEquals("Node4", node2.getUserObject());
546: assertEquals(0, node2.getChildCount());
547:
548: node2 = (DefaultMutableTreeNode) node.getChildAt(2);
549: assertEquals("Node5", node2.getUserObject());
550: assertEquals(0, node2.getChildCount());
551: } catch (Exception e) {
552: e.printStackTrace();
553: fail();
554: }
555: }
556: }
|