KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > tree > TreeTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.tree;
22
23 import java.util.*;
24 import java.sql.*;
25 import junit.framework.*;
26 import org.apache.log4j.*;
27 import com.methodhead.test.*;
28 import javax.swing.tree.*;
29
30 public class TreeTest extends TestCase {
31
32   static {
33     TestUtils.initLogger();
34   }
35
36   public TreeTest( String JavaDoc name ) {
37     super( name );
38   }
39
40   private Tree tree = null;
41   private DefaultMutableTreeNode node = null;
42   private DefaultMutableTreeNode node2 = null;
43   private DefaultMutableTreeNode node3 = null;
44   private DefaultMutableTreeNode subnode = null;
45
46   private DefaultMutableTreeNode node1_ = null;
47   private DefaultMutableTreeNode node2_ = null;
48   private DefaultMutableTreeNode node3_ = null;
49   private DefaultMutableTreeNode node4_ = null;
50   private DefaultMutableTreeNode node5_ = null;
51
52   protected void createData() {
53
54     // Node1
55
// Node2
56
// Node3
57
// Node4
58
// Node5
59
node1_ = new DefaultMutableTreeNode();
60     node1_.setUserObject( "Node1" );
61     node2_ = new DefaultMutableTreeNode();
62     node2_.setUserObject( "Node2" );
63     node3_ = new DefaultMutableTreeNode();
64     node3_.setUserObject( "Node3" );
65     node4_ = new DefaultMutableTreeNode();
66     node4_.setUserObject( "Node4" );
67     node5_ = new DefaultMutableTreeNode();
68     node5_.setUserObject( "Node5" );
69
70     node1_.add( node2_ );
71     node2_.add( node3_ );
72     node1_.add( node4_ );
73     node1_.add( node5_ );
74   }
75
76   protected void setUp() {
77     try {
78     }
79     catch ( Exception JavaDoc e ) {
80       fail( e.getMessage() );
81     }
82   }
83
84   protected void tearDown() {
85   }
86
87   public void testGetSetRoot() {
88     try {
89       Tree tree = new Tree();
90
91       assertNull( tree.getRoot() );
92
93       DefaultMutableTreeNode node = new DefaultMutableTreeNode();
94
95       tree.setRoot( node );
96
97       assertEquals( node, tree.getRoot() );
98     }
99     catch ( Exception JavaDoc e ) {
100       e.printStackTrace();
101       fail();
102     }
103   }
104
105   public void testInsertUnder() {
106     try {
107       Tree tree = null;
108       DefaultMutableTreeNode node = null;
109
110       //
111
// insert under root
112
//
113
createData();
114       tree = new Tree();
115       tree.setRoot( node1_ );
116
117       node = new DefaultMutableTreeNode();
118       tree.insertUnder( node1_, node );
119
120       assertEquals( 4, node1_.getChildCount() );
121       assertEquals( node, node1_.getChildAt( 3 ) );
122
123       //
124
// insert under another node
125
//
126
createData();
127       tree = new Tree();
128       tree.setRoot( node1_ );
129
130       node = new DefaultMutableTreeNode();
131       tree.insertUnder( node3_, node );
132
133       assertEquals( 1, node3_.getChildCount() );
134       assertEquals( node, node3_.getChildAt( 0 ) );
135     }
136     catch ( Exception JavaDoc e ) {
137       e.printStackTrace();
138       fail();
139     }
140   }
141
142   public void testInsertAfter() {
143     try {
144       Tree tree = null;
145       DefaultMutableTreeNode node = null;
146
147       //
148
// insert after root
149
//
150
createData();
151       tree = new Tree();
152       tree.setRoot( node1_ );
153
154       try {
155         node = new DefaultMutableTreeNode();
156         tree.insertAfter( node1_, node );
157         fail( "Inserted after root." );
158       }
159       catch ( Exception JavaDoc e ) {
160         // success
161
}
162
163       //
164
// insert after another node
165
//
166
createData();
167       tree = new Tree();
168       tree.setRoot( node1_ );
169
170       node = new DefaultMutableTreeNode();
171       tree.insertAfter( node2_, node );
172
173       assertEquals( 4, node1_.getChildCount() );
174       assertEquals( node, node1_.getChildAt( 1 ) );
175
176       //
177
// insert after a last node
178
//
179
createData();
180       tree = new Tree();
181       tree.setRoot( node1_ );
182
183       node = new DefaultMutableTreeNode();
184       tree.insertAfter( node5_, node );
185
186       assertEquals( 4, node1_.getChildCount() );
187       assertEquals( node, node1_.getChildAt( 3 ) );
188     }
189     catch ( Exception JavaDoc e ) {
190       e.printStackTrace();
191       fail();
192     }
193   }
194
195   public void testInsertBefore() {
196     try {
197       Tree tree = null;
198       DefaultMutableTreeNode node = null;
199
200       //
201
// insert before root
202
//
203
createData();
204       tree = new Tree();
205       tree.setRoot( node1_ );
206
207       try {
208         node = new DefaultMutableTreeNode();
209         tree.insertBefore( node1_, node );
210         fail( "Inserted before root." );
211       }
212       catch ( Exception JavaDoc e ) {
213         // success
214
}
215
216       //
217
// insert before another node
218
//
219
createData();
220       tree = new Tree();
221       tree.setRoot( node1_ );
222
223       node = new DefaultMutableTreeNode();
224       tree.insertBefore( node2_, node );
225
226       assertEquals( 4, node1_.getChildCount() );
227       assertEquals( node, node1_.getChildAt( 0 ) );
228
229       //
230
// insert before a last node
231
//
232
createData();
233       tree = new Tree();
234       tree.setRoot( node1_ );
235
236       node = new DefaultMutableTreeNode();
237       tree.insertBefore( node5_, node );
238
239       assertEquals( 4, node1_.getChildCount() );
240       assertEquals( node, node1_.getChildAt( 2 ) );
241     }
242     catch ( Exception JavaDoc e ) {
243       e.printStackTrace();
244       fail();
245     }
246   }
247
248   public void testRemove() {
249     try {
250       Tree tree = null;
251       DefaultMutableTreeNode node = null;
252
253       //
254
// remove node 2
255
//
256
createData();
257       tree = new Tree();
258       tree.setRoot( node1_ );
259
260       tree.remove( node2_ );
261
262       assertEquals( 2, node1_.getChildCount() );
263
264       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 0 );
265
266       assertEquals( node4_, node );
267
268       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 1 );
269
270       assertEquals( node5_, node );
271
272       //
273
// remove the root
274
//
275
createData();
276       tree = new Tree();
277       tree.setRoot( node1_ );
278       tree.remove( node1_ );
279
280       assertNull( tree.getRoot() );
281     }
282     catch ( Exception JavaDoc e ) {
283       e.printStackTrace();
284       fail();
285     }
286   }
287
288   public void testRemoveAndPromoteChildren() {
289     try {
290       Tree tree = null;
291       DefaultMutableTreeNode node = null;
292       DefaultMutableTreeNode subnode = null;
293
294       //
295
// remove node 2
296
//
297
createData();
298       tree = new Tree();
299       tree.setRoot( node1_ );
300
301       tree.removeAndPromoteChildren( node2_ );
302
303       assertEquals( 3, node1_.getChildCount() );
304
305       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 0 );
306
307       assertEquals( node4_, node );
308
309       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 1 );
310
311       assertEquals( node5_, node );
312
313       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 2 );
314
315       assertEquals( node3_, node );
316
317       //
318
// remove the root
319
//
320
createData();
321       tree = new Tree();
322       tree.setRoot( node1_ );
323       tree.removeAndPromoteChildren( node1_ );
324
325       node = tree.getRoot();
326
327       assertNotNull( node );
328       assertEquals( node2_, node );
329
330       subnode = ( DefaultMutableTreeNode )node.getChildAt( 0 );
331
332       assertEquals( node4_, subnode );
333
334       subnode = ( DefaultMutableTreeNode )node.getChildAt( 1 );
335
336       assertEquals( node5_, subnode );
337
338       subnode = ( DefaultMutableTreeNode )node.getChildAt( 2 );
339
340       assertEquals( node3_, subnode );
341     }
342     catch ( Exception JavaDoc e ) {
343       e.printStackTrace();
344       fail();
345     }
346   }
347
348   public void testMoveUnder() {
349     try {
350       createData();
351       tree = new Tree();
352       tree.setRoot( node1_ );
353
354       try {
355         tree.moveUnder( node2_, node1_ );
356         fail( "Exception not thrown." );
357       }
358       catch ( Exception JavaDoc e ) {
359         // success
360
}
361
362       try {
363         tree.moveUnder( node2_, node2_ );
364         fail( "Exception not thrown." );
365       }
366       catch ( Exception JavaDoc e ) {
367         // success
368
}
369
370       tree.moveUnder( node4_, node3_ );
371
372       assertEquals( 0, node2_.getChildCount() );
373       assertEquals( 1, node4_.getChildCount() );
374       assertEquals( node3_, node4_.getChildAt( 0 ) );
375     }
376     catch ( Exception JavaDoc e ) {
377       e.printStackTrace();
378       fail();
379     }
380   }
381
382   public void testMoveAfter() {
383     try {
384       createData();
385       tree = new Tree();
386       tree.setRoot( node1_ );
387
388       try {
389         tree.moveAfter( node2_, node1_ );
390         fail( "Exception not thrown." );
391       }
392       catch ( Exception JavaDoc e ) {
393         // success
394
}
395
396       try {
397         tree.moveAfter( node2_, node2_ );
398         fail( "Exception not thrown." );
399       }
400       catch ( Exception JavaDoc e ) {
401         // success
402
}
403
404       try {
405         tree.moveAfter( node3_, node2_ );
406         fail( "Exception not thrown." );
407       }
408       catch ( Exception JavaDoc e ) {
409         // success
410
}
411
412       tree.moveAfter( node4_, node2_ );
413
414       assertEquals( 3, node1_.getChildCount() );
415       assertEquals( node4_, node1_.getChildAt( 0 ) );
416       assertEquals( node2_, node1_.getChildAt( 1 ) );
417     }
418     catch ( Exception JavaDoc e ) {
419       e.printStackTrace();
420       fail();
421     }
422   }
423
424   public void testMoveBefore() {
425     try {
426       createData();
427       tree = new Tree();
428       tree.setRoot( node1_ );
429
430       try {
431         tree.moveAfter( node2_, node1_ );
432         fail( "Exception not thrown." );
433       }
434       catch ( Exception JavaDoc e ) {
435         // success
436
}
437
438       try {
439         tree.moveAfter( node2_, node2_ );
440         fail( "Exception not thrown." );
441       }
442       catch ( Exception JavaDoc e ) {
443         // success
444
}
445
446       try {
447         tree.moveAfter( node3_, node2_ );
448         fail( "Exception not thrown." );
449       }
450       catch ( Exception JavaDoc e ) {
451         // success
452
}
453
454       tree.moveBefore( node2_, node4_ );
455
456       assertEquals( 3, node1_.getChildCount() );
457       assertEquals( node4_, node1_.getChildAt( 0 ) );
458       assertEquals( node2_, node1_.getChildAt( 1 ) );
459     }
460     catch ( Exception JavaDoc e ) {
461       e.printStackTrace();
462       fail();
463     }
464   }
465
466   public void testValidateMove() {
467     try {
468       createData();
469       tree = new Tree();
470       tree.setRoot( node1_ );
471
472       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEROOT, tree.validateMove( node1_, node2_, Tree.POSITION_UNDER ) );
473       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEROOT, tree.validateMove( node1_, node2_, Tree.POSITION_BEFORE ) );
474       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEROOT, tree.validateMove( node1_, node2_, Tree.POSITION_AFTER ) );
475
476       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEBEFOREROOT, tree.validateMove( node2_, node1_, Tree.POSITION_BEFORE ) );
477       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEAFTERROOT, tree.validateMove( node2_, node1_, Tree.POSITION_AFTER ) );
478
479       assertEquals( Tree.INVALIDMOVE_CANNOTMOVENEARSELF, tree.validateMove( node2_, node2_, Tree.POSITION_AFTER ) );
480
481       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF, tree.validateMove( node2_, node3_, Tree.POSITION_UNDER ) );
482       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF, tree.validateMove( node2_, node3_, Tree.POSITION_BEFORE ) );
483       assertEquals( Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF, tree.validateMove( node2_, node3_, Tree.POSITION_AFTER ) );
484
485       assertEquals( Tree.VALIDMOVE, tree.validateMove( node2_, node4_, Tree.POSITION_UNDER ) );
486     }
487     catch ( Exception JavaDoc e ) {
488       e.printStackTrace();
489       fail();
490     }
491   }
492
493   public void testSort() {
494     try {
495       createData();
496       tree = new Tree();
497       tree.setRoot( node1_ );
498
499       //
500
// re-order tree
501
//
502
tree.moveBefore( node2_, node4_ );
503       Tree.sort( node1_, new Comparator() {
504         public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
505           DefaultMutableTreeNode n1 = ( DefaultMutableTreeNode )o1;
506           DefaultMutableTreeNode n2 = ( DefaultMutableTreeNode )o2;
507           return ( ( String JavaDoc )n1.getUserObject() ).compareTo( ( String JavaDoc )n2.getUserObject() );
508         }
509       } );
510
511       assertEquals( 3, node1_.getChildCount() );
512
513       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 0 );
514       assertEquals( node2_, node );
515
516       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 1 );
517       assertEquals( node4_, node );
518
519       node = ( DefaultMutableTreeNode )tree.getRoot().getChildAt( 2 );
520       assertEquals( node5_, node );
521     }
522     catch ( Exception JavaDoc e ) {
523       e.printStackTrace();
524       fail();
525     }
526   }
527
528   public void testCopy() {
529     try {
530       createData();
531       tree = new Tree();
532
533       node = Tree.copy( node1_ );
534
535       assertTrue( node != node1_ );
536       assertEquals( "Node1", node.getUserObject() );
537       assertEquals( 3, node.getChildCount() );
538
539       node2 = ( DefaultMutableTreeNode )node.getChildAt( 0 );
540       assertEquals( "Node2", node2.getUserObject() );
541       assertEquals( 1, node2.getChildCount() );
542
543       node3 = ( DefaultMutableTreeNode )node2.getChildAt( 0 );
544       assertEquals( "Node3", node3.getUserObject() );
545       assertEquals( 0, node3.getChildCount() );
546
547       node2 = ( DefaultMutableTreeNode )node.getChildAt( 1 );
548       assertEquals( "Node4", node2.getUserObject() );
549       assertEquals( 0, node2.getChildCount() );
550
551       node2 = ( DefaultMutableTreeNode )node.getChildAt( 2 );
552       assertEquals( "Node5", node2.getUserObject() );
553       assertEquals( 0, node2.getChildCount() );
554     }
555     catch ( Exception JavaDoc e ) {
556       e.printStackTrace();
557       fail();
558     }
559   }
560 }
561
Popular Tags