KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > tree > MutableTreeModelDecoratorTest


1 package com.tonbeller.wcf.tree;
2
3 import com.tonbeller.wcf.tree.TestTreeModel.Node;
4
5 /**
6  * MutableTreeModelDecorator uses ChangeOrderUtils which has its own tests. So we dont test
7  * all index combinations.
8  *
9  * @author av
10  */

11 public class MutableTreeModelDecoratorTest extends NodeCountTestBase {
12   TreeModel ttm;
13   MutableTreeModelDecorator mtd;
14   TestTreeModel.Node[] roots;
15   TestTreeModel.Node[] children;
16
17   void setUp(int n) {
18     ttm = new TestTreeModel(new int[] { 1, n});
19     mtd = new MutableTreeModelDecorator(ttm);
20     roots = (Node[]) mtd.getRoots();
21     children = (Node[]) mtd.getChildren(roots[0]);
22   }
23
24   protected void setUp() throws Exception JavaDoc {
25     setUp(4);
26   }
27
28   void assertOrder(int[] ord) {
29     assertEquals(children.length, ord.length);
30     children = (Node[]) mtd.getChildren(roots[0]);
31     for (int i = 0; i < ord.length; i++) {
32       String JavaDoc s = "A[" + ord[i] + "]";
33       assertEquals("node[" + i + "] = ", s, children[i].getName());
34     }
35   }
36   void assertOrder(int i1, int i2, int i3, int i4) {
37     assertOrder(new int[]{i1, i2, i3, i4});
38   }
39   
40   void fwd(int i) {
41     mtd.move(roots[0], children[i], i, i+1);
42   }
43   
44   void bwd(int i) {
45     mtd.move(roots[0], children[i], i, i-1);
46   }
47   
48   void move(int oldIndex, int newIndex) {
49     mtd.move(roots[0], children[oldIndex], oldIndex, newIndex);
50   }
51
52   public void testFwdBwd() {
53     assertEquals(4, children.length);
54     assertOrder(0, 1, 2, 3);
55     bwd(0);
56     assertOrder(0, 1, 2, 3);
57     fwd(0);
58     assertOrder(1, 0, 2, 3);
59     bwd(0);
60     assertOrder(1, 0, 2, 3);
61     bwd(1);
62     assertOrder(0, 1, 2, 3);
63     bwd(1);
64     assertOrder(1, 0, 2, 3);
65     bwd(1);
66     assertOrder(0, 1, 2, 3);
67     fwd(3);
68     assertOrder(0, 1, 2, 3);
69     bwd(3);
70     assertOrder(0, 1, 3, 2);
71     bwd(3);
72     assertOrder(0, 1, 2, 3);
73     fwd(2);
74     assertOrder(0, 1, 3, 2);
75     fwd(2);
76     assertOrder(0, 1, 2, 3);
77   }
78   
79   public void testInterval() {
80     assertEquals(4, children.length);
81     assertOrder(0, 1, 2, 3);
82     move(0, 0);
83     move(1, 1);
84     assertOrder(0, 1, 2, 3);
85     move(0, 1);
86     assertOrder(1, 0, 2, 3);
87     move(0, 2);
88     assertOrder(0, 2, 1, 3);
89     move(0, 3);
90     assertOrder(2, 1, 3, 0);
91   }
92 }
93
Popular Tags