KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.tonbeller.wcf.tree;
2
3 import java.util.Comparator JavaDoc;
4
5 import com.tonbeller.wcf.tree.GroupingTreeModelDecorator.Group;
6 import com.tonbeller.wcf.tree.TestTreeModel.Node;
7
8
9
10 public class GroupingTreeModelDecoratorTest extends NodeCountTestBase {
11   TreeModel ttm ;
12   GroupingTreeModelDecorator gtm;
13
14   public void testGrouping() {
15     
16     ttm = new TestTreeModel(new int[] {10});
17     testParentChildConsistence(ttm);
18     
19     // 2 pages 5 items each
20
gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 5);
21     testParentChildConsistence(gtm);
22
23     assertLevelCount(gtm, 2);
24     assertChildCount(gtm, 0, 2);
25     assertChildCount(gtm, 1, 5);
26
27     // 4, 4, 2 items
28
gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 4);
29     testParentChildConsistence(gtm);
30     assertLevelCount(gtm, 2);
31     Object JavaDoc[] o = gtm.getRoots();
32     assertEquals("root count", 3, o.length);
33     assertEquals(4, gtm.getChildren(o[0]).length);
34     assertEquals(4, gtm.getChildren(o[1]).length);
35     assertEquals(2, gtm.getChildren(o[2]).length);
36     
37   }
38
39   
40   public void testHierarchically() {
41     ttm = new TestTreeModel(new int[] {9, 11});
42     gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 5);
43     testParentChildConsistence(ttm);
44     assertLevelCount(gtm, 4);
45     // roots = group A ...
46
// A
47
// group A[0] ...
48
// A[0]
49
// A[1]
50
// ...
51
// A[4]
52
// group A[5] ...
53
// A[5]
54
// A[6]
55
// A[7]
56
// A[8]
57
// A[9]
58
// group A[10] ...
59
// A[10]
60
// B
61
// C
62
// D
63
// E
64
// group F ...
65
// F
66
// G
67
// H
68
// I
69

70     // level 0 = roots
71
assertChildCount(gtm, new int[]{}, 2);
72     // level 1
73
assertChildCount(gtm, new int[]{0}, 5);
74     assertChildCount(gtm, new int[]{1}, 4);
75     // level 2
76
assertChildCount(gtm, new int[]{0, 0}, 3);
77     assertChildCount(gtm, new int[]{0, 1}, 3);
78     assertChildCount(gtm, new int[]{0, 2}, 3);
79     assertChildCount(gtm, new int[]{0, 3}, 3);
80     assertChildCount(gtm, new int[]{0, 4}, 3);
81     assertChildCount(gtm, new int[]{1, 0}, 3);
82     assertChildCount(gtm, new int[]{1, 1}, 3);
83     assertChildCount(gtm, new int[]{1, 2}, 3);
84     assertChildCount(gtm, new int[]{1, 3}, 3);
85     // level 3
86
assertChildCount(gtm, new int[]{0, 0, 0}, 5);
87     assertChildCount(gtm, new int[]{0, 0, 1}, 5);
88     assertChildCount(gtm, new int[]{0, 0, 2}, 1);
89   }
90
91   public void testCaching() {
92     ttm = new TestTreeModel(new int[] {4, 4});
93     gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 2);
94
95     Object JavaDoc[] o = gtm.getRoots();
96     Object JavaDoc[] r = o;
97     Group g0 = (Group) o[0];
98     o = gtm.getChildren(o[0]);
99     o = gtm.getChildren(o[0]);
100     Group g1 = (Group) o[0];
101
102     // repeat
103
o = gtm.getRoots();
104     assertEquals(r, o);
105     assertEquals(g0, o[0]);
106     o = gtm.getChildren(o[0]);
107     o = gtm.getChildren(o[0]);
108     assertEquals(g1, o[0]);
109     
110     ttm.fireModelChanged(true);
111     o = gtm.getRoots();
112     assertNotSame(r, o);
113     assertNotSame(g0, o[0]);
114     o = gtm.getChildren(o[0]);
115     o = gtm.getChildren(o[0]);
116     assertNotSame(g1, o[0]);
117   }
118   
119   /** fetch parent without fetching its children before */
120   public void testExpandSelected() {
121     ttm = new TestTreeModel(new int[] {10});
122     Object JavaDoc[] o = ttm.getRoots();
123     gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 5);
124     Object JavaDoc g = gtm.getParent(o[0]);
125     assertNotNull(g);
126     assertTrue("Group expected ", g instanceof Group);
127     assertNull(gtm.getParent(g));
128   }
129
130   /** very many nodes need more than additional level */
131   public void testRecursive() {
132     ttm = new TestTreeModel(new int[] {18});
133     gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 3);
134
135     // the current implementation puts the smallest group on top:
136
// g1
137
// g11
138
// n1
139
// n2
140
// n3
141
// g12
142
// n4
143
// n5
144
// n6
145
// g13
146
// n7
147
// n8
148
// n9
149
// g2
150
// g21
151
// n10
152
// n11
153
// n12
154
// g22
155
// n13
156
// n14
157
// n15
158
// g23
159
// n16
160
// n17
161
// n18
162

163     assertLevelCount(gtm, 3);
164     Object JavaDoc[] o = gtm.getRoots();
165     assertEquals(2, o.length);
166     assertTrue(o[0] instanceof Group);
167     
168     assertTrue(gtm.hasChildren(o[0]));
169     o = gtm.getChildren(o[0]);
170     assertEquals(3, o.length);
171     assertTrue(o[0] instanceof Group);
172
173     assertTrue(gtm.hasChildren(o[0]));
174     o = gtm.getChildren(o[0]);
175     assertEquals(3, o.length);
176     assertTrue(o[0] instanceof Node);
177   }
178   
179   /* ========================= TreeMap test ===================== */
180
181   static class VolatileNode {
182     String JavaDoc label;
183     VolatileNode(String JavaDoc label) {
184       this.label = label;
185     }
186     public String JavaDoc toString() {
187       return label;
188     }
189   }
190   static class NodeComparator implements Comparator JavaDoc {
191     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
192       String JavaDoc s1 = ((VolatileNode)o1).toString();
193       String JavaDoc s2 = ((VolatileNode)o2).toString();
194       return s1.compareTo(s2);
195     }
196   }
197
198   /**
199    * a tree model that always creates new node instances that
200    * are considered "the same" by the nodeComparator.
201    */

202   static class VolatileTreeModel extends AbstractTreeModel {
203     public Object JavaDoc[] getRoots() {
204       Object JavaDoc[] o = new Object JavaDoc[4];
205       o[0] = new VolatileNode("A");
206       o[1] = new VolatileNode("B");
207       o[2] = new VolatileNode("C");
208       o[3] = new VolatileNode("D");
209       return o;
210     }
211     public boolean hasChildren(Object JavaDoc node) {
212       VolatileNode n = (VolatileNode)node;
213       return n.label.length() == 1;
214     }
215     public Object JavaDoc[] getChildren(Object JavaDoc node) {
216       VolatileNode n = (VolatileNode)node;
217       Object JavaDoc[] o = new Object JavaDoc[2];
218       o[0] = new VolatileNode(n.label + "1");
219       o[1] = new VolatileNode(n.label + "2");
220       return o;
221     }
222     public Object JavaDoc getParent(Object JavaDoc node) {
223       VolatileNode n = (VolatileNode)node;
224       if (n.label.length() > 1) {
225         String JavaDoc s = n.label.substring(0, 1);
226         return new VolatileNode(s);
227       }
228       return null;
229     }
230   }
231   
232   /** exchange TreeModel */
233   public void testTreeMap() {
234     ttm = new VolatileTreeModel();
235     gtm = new GroupingTreeModelDecorator (new NodeComparator(), new DefaultLabelProvider(), ttm, 2);
236
237     Object JavaDoc[] o = gtm.getRoots();
238     Object JavaDoc[] r = o;
239     Group g0 = (Group) o[0];
240     o = gtm.getChildren(o[0]);
241     o = gtm.getChildren(o[0]);
242     VolatileNode n1 = (VolatileNode) o[0];
243
244     // repeat
245
o = gtm.getRoots();
246     assertEquals(r, o);
247     assertEquals(g0, o[0]);
248     o = gtm.getChildren(o[0]);
249     o = gtm.getChildren(o[0]);
250     VolatileNode n2 = (VolatileNode) o[0];
251     assertEquals(n1, n2);
252     
253     ttm.fireModelChanged(true);
254     o = gtm.getRoots();
255     assertNotSame(r, o);
256     assertNotSame(g0, o[0]);
257     o = gtm.getChildren(o[0]);
258     o = gtm.getChildren(o[0]);
259     n2 = (VolatileNode) o[0];
260     assertNotSame(n1, n2);
261   }
262 }
263
Popular Tags