1 package com.tonbeller.wcf.tree; 2 3 import java.util.Comparator ; 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 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 gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 4); 29 testParentChildConsistence(gtm); 30 assertLevelCount(gtm, 2); 31 Object [] 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 70 assertChildCount(gtm, new int[]{}, 2); 72 assertChildCount(gtm, new int[]{0}, 5); 74 assertChildCount(gtm, new int[]{1}, 4); 75 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 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 [] o = gtm.getRoots(); 96 Object [] 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 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 120 public void testExpandSelected() { 121 ttm = new TestTreeModel(new int[] {10}); 122 Object [] o = ttm.getRoots(); 123 gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 5); 124 Object g = gtm.getParent(o[0]); 125 assertNotNull(g); 126 assertTrue("Group expected ", g instanceof Group); 127 assertNull(gtm.getParent(g)); 128 } 129 130 131 public void testRecursive() { 132 ttm = new TestTreeModel(new int[] {18}); 133 gtm = new GroupingTreeModelDecorator (new DefaultLabelProvider(), ttm, 3); 134 135 163 assertLevelCount(gtm, 3); 164 Object [] 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 180 181 static class VolatileNode { 182 String label; 183 VolatileNode(String label) { 184 this.label = label; 185 } 186 public String toString() { 187 return label; 188 } 189 } 190 static class NodeComparator implements Comparator { 191 public int compare(Object o1, Object o2) { 192 String s1 = ((VolatileNode)o1).toString(); 193 String s2 = ((VolatileNode)o2).toString(); 194 return s1.compareTo(s2); 195 } 196 } 197 198 202 static class VolatileTreeModel extends AbstractTreeModel { 203 public Object [] getRoots() { 204 Object [] o = new Object [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 node) { 212 VolatileNode n = (VolatileNode)node; 213 return n.label.length() == 1; 214 } 215 public Object [] getChildren(Object node) { 216 VolatileNode n = (VolatileNode)node; 217 Object [] o = new Object [2]; 218 o[0] = new VolatileNode(n.label + "1"); 219 o[1] = new VolatileNode(n.label + "2"); 220 return o; 221 } 222 public Object getParent(Object node) { 223 VolatileNode n = (VolatileNode)node; 224 if (n.label.length() > 1) { 225 String s = n.label.substring(0, 1); 226 return new VolatileNode(s); 227 } 228 return null; 229 } 230 } 231 232 233 public void testTreeMap() { 234 ttm = new VolatileTreeModel(); 235 gtm = new GroupingTreeModelDecorator (new NodeComparator(), new DefaultLabelProvider(), ttm, 2); 236 237 Object [] o = gtm.getRoots(); 238 Object [] 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 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 |