View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.model;
21  
22  import org.junit.Assert;
23  import org.junit.BeforeClass;
24  import org.junit.Test;
25  
26  import javax.swing.tree.DefaultMutableTreeNode;
27  
28  public class ExpandedStateUnitTest {
29  
30    private static final DefaultMutableTreeNode ROOT = new DefaultMutableTreeNode("root");
31    private static final DefaultMutableTreeNode A = new DefaultMutableTreeNode("a");
32    private static final DefaultMutableTreeNode B = new DefaultMutableTreeNode("b");
33    private static final DefaultMutableTreeNode C = new DefaultMutableTreeNode("c");
34    private static final DefaultMutableTreeNode A1 = new DefaultMutableTreeNode("a1");
35    private static final DefaultMutableTreeNode A2 = new DefaultMutableTreeNode("a2");
36    private static final DefaultMutableTreeNode A3 = new DefaultMutableTreeNode("a3");
37    private static final DefaultMutableTreeNode B1 = new DefaultMutableTreeNode("b1");
38    private static final DefaultMutableTreeNode B2 = new DefaultMutableTreeNode("b2");
39    private static final DefaultMutableTreeNode B3 = new DefaultMutableTreeNode("b3");
40    private static final DefaultMutableTreeNode C1 = new DefaultMutableTreeNode("c1");
41    private static final DefaultMutableTreeNode C2 = new DefaultMutableTreeNode("c2");
42    private static final DefaultMutableTreeNode C3 = new DefaultMutableTreeNode("c3");
43  
44    @BeforeClass
45    public static void setup() {
46      ROOT.add(A);
47      ROOT.add(B);
48      ROOT.add(C);
49      A.add(A1);
50      A.add(A2);
51      A.add(A3);
52      B.add(B1);
53      B.add(B2);
54      B.add(B3);
55      C.add(C1);
56      C.add(C2);
57      C.add(C3);
58    }
59  
60    /**
61     * <pre>
62     *  + root
63     *  |
64     *  +-+ a
65     *  | |
66     *  | +-+ a1
67     *  | |
68     *  | +-+ a2
69     *  | |
70     *  | +-+ a3
71     *  |
72     *  +-+ b
73     *  | |
74     *  | +-+ b1
75     *  | |
76     *  | +-+ b2
77     *  | |
78     *  | +-+ b3
79     *  |
80     *  +-+ c
81     *    |
82     *    +-+ c1
83     *    |
84     *    +-+ c2
85     *    |
86     *    +-+ c3
87     *
88     * </pre>
89     */
90    @Test
91    public void test() {
92  
93      ExpandedState state = new ExpandedState(2);
94  
95      final TreePath root = new TreePath(ROOT);
96      final TreePath a = new TreePath(A);
97      final TreePath b = new TreePath(B);
98      final TreePath c = new TreePath(C);
99      final TreePath a1 = new TreePath(A1);
100     final TreePath a2 = new TreePath(A2);
101     final TreePath a3 = new TreePath(A3);
102     final TreePath b1 = new TreePath(B1);
103     final TreePath b2 = new TreePath(B2);
104     final TreePath b3 = new TreePath(B3);
105     final TreePath c1 = new TreePath(C1);
106     final TreePath c2 = new TreePath(C2);
107     final TreePath c3 = new TreePath(C3);
108 
109     Assert.assertTrue(state.isExpanded(ROOT));
110 
111     Assert.assertTrue(state.isExpanded(root));
112     Assert.assertTrue(state.isExpanded(a));
113     Assert.assertTrue(state.isExpanded(b));
114     Assert.assertTrue(state.isExpanded(c));
115     Assert.assertFalse(state.isExpanded(a1));
116     Assert.assertFalse(state.isExpanded(a2));
117     Assert.assertFalse(state.isExpanded(a3));
118     Assert.assertFalse(state.isExpanded(b1));
119     Assert.assertFalse(state.isExpanded(b2));
120     Assert.assertFalse(state.isExpanded(b3));
121     Assert.assertFalse(state.isExpanded(c1));
122     Assert.assertFalse(state.isExpanded(c2));
123     Assert.assertFalse(state.isExpanded(c3));
124 
125     state.expand(a);
126     state.expand(a1);
127     state.expand(a2);
128     state.expand(c);
129 
130     state.collapse(root);
131     state.collapse(b);
132     state.collapse(b3);
133     state.collapse(a2);
134     state.collapse(c);
135 
136     Assert.assertFalse(state.isExpanded(root));
137     Assert.assertTrue(state.isExpanded(a));
138     Assert.assertFalse(state.isExpanded(b));
139     Assert.assertFalse(state.isExpanded(c));
140     Assert.assertTrue(state.isExpanded(a1));
141     Assert.assertFalse(state.isExpanded(a2));
142     Assert.assertFalse(state.isExpanded(a3));
143     Assert.assertFalse(state.isExpanded(b1));
144     Assert.assertFalse(state.isExpanded(b2));
145     Assert.assertFalse(state.isExpanded(b3));
146     Assert.assertFalse(state.isExpanded(c1));
147     Assert.assertFalse(state.isExpanded(c2));
148     Assert.assertFalse(state.isExpanded(c3));
149     Assert.assertEquals(1, state.getExpandedSet().size());
150     Assert.assertEquals(3, state.getCollapsedSet().size());
151 
152     // expand(1) should only expand the root node
153     state.expand(1);
154 
155     Assert.assertTrue(state.isExpanded(root));
156     Assert.assertTrue(state.isExpanded(a));
157     Assert.assertFalse(state.isExpanded(b));
158     Assert.assertFalse(state.isExpanded(c));
159     Assert.assertTrue(state.isExpanded(a1));
160     Assert.assertFalse(state.isExpanded(a2));
161     Assert.assertFalse(state.isExpanded(a3));
162     Assert.assertFalse(state.isExpanded(b1));
163     Assert.assertFalse(state.isExpanded(b2));
164     Assert.assertFalse(state.isExpanded(b3));
165     Assert.assertFalse(state.isExpanded(c1));
166     Assert.assertFalse(state.isExpanded(c2));
167     Assert.assertFalse(state.isExpanded(c3));
168     Assert.assertEquals(1, state.getExpandedSet().size());
169     Assert.assertEquals(2, state.getCollapsedSet().size());
170 
171     // collapse(3) should collapse a1 only
172     state.collapse(3);
173 
174     Assert.assertTrue(state.isExpanded(root));
175     Assert.assertTrue(state.isExpanded(a));
176     Assert.assertFalse(state.isExpanded(b));
177     Assert.assertFalse(state.isExpanded(c));
178     Assert.assertFalse(state.isExpanded(a1));
179     Assert.assertFalse(state.isExpanded(a2));
180     Assert.assertFalse(state.isExpanded(a3));
181     Assert.assertFalse(state.isExpanded(b1));
182     Assert.assertFalse(state.isExpanded(b2));
183     Assert.assertFalse(state.isExpanded(b3));
184     Assert.assertFalse(state.isExpanded(c1));
185     Assert.assertFalse(state.isExpanded(c2));
186     Assert.assertFalse(state.isExpanded(c3));
187     Assert.assertEquals(0, state.getExpandedSet().size());
188     Assert.assertEquals(2, state.getCollapsedSet().size());
189 
190     // expand(2) should expand b and c
191     state.expand(2);
192 
193     Assert.assertTrue(state.isExpanded(root));
194     Assert.assertTrue(state.isExpanded(a));
195     Assert.assertTrue(state.isExpanded(b));
196     Assert.assertTrue(state.isExpanded(c));
197     Assert.assertFalse(state.isExpanded(a1));
198     Assert.assertFalse(state.isExpanded(a2));
199     Assert.assertFalse(state.isExpanded(a3));
200     Assert.assertFalse(state.isExpanded(b1));
201     Assert.assertFalse(state.isExpanded(b2));
202     Assert.assertFalse(state.isExpanded(b3));
203     Assert.assertFalse(state.isExpanded(c1));
204     Assert.assertFalse(state.isExpanded(c2));
205     Assert.assertFalse(state.isExpanded(c3));
206     Assert.assertEquals(0, state.getExpandedSet().size());
207     Assert.assertEquals(0, state.getCollapsedSet().size());
208 
209     // collapse(2) should expand b and c
210     state.collapse(2);
211 
212     Assert.assertTrue(state.isExpanded(root));
213     Assert.assertFalse(state.isExpanded(a));
214     Assert.assertFalse(state.isExpanded(b));
215     Assert.assertFalse(state.isExpanded(c));
216     Assert.assertFalse(state.isExpanded(a1));
217     Assert.assertFalse(state.isExpanded(a2));
218     Assert.assertFalse(state.isExpanded(a3));
219     Assert.assertFalse(state.isExpanded(b1));
220     Assert.assertFalse(state.isExpanded(b2));
221     Assert.assertFalse(state.isExpanded(b3));
222     Assert.assertFalse(state.isExpanded(c1));
223     Assert.assertFalse(state.isExpanded(c2));
224     Assert.assertFalse(state.isExpanded(c3));
225     Assert.assertEquals(0, state.getExpandedSet().size());
226     Assert.assertEquals(0, state.getCollapsedSet().size());
227 
228     // expand(2) should expand b and c
229     state.expand(a);
230     state.expand(b);
231     state.expand(2);
232 
233     Assert.assertTrue(state.isExpanded(root));
234     Assert.assertTrue(state.isExpanded(a));
235     Assert.assertTrue(state.isExpanded(b));
236     Assert.assertTrue(state.isExpanded(c));
237     Assert.assertFalse(state.isExpanded(a1));
238     Assert.assertFalse(state.isExpanded(a2));
239     Assert.assertFalse(state.isExpanded(a3));
240     Assert.assertFalse(state.isExpanded(b1));
241     Assert.assertFalse(state.isExpanded(b2));
242     Assert.assertFalse(state.isExpanded(b3));
243     Assert.assertFalse(state.isExpanded(c1));
244     Assert.assertFalse(state.isExpanded(c2));
245     Assert.assertFalse(state.isExpanded(c3));
246     Assert.assertEquals(0, state.getExpandedSet().size());
247     Assert.assertEquals(0, state.getCollapsedSet().size());
248 
249     // collapse(2) should expand b and c
250     state.collapse(a);
251     state.collapse(b);
252     state.collapse(2);
253 
254     Assert.assertTrue(state.isExpanded(root));
255     Assert.assertFalse(state.isExpanded(a));
256     Assert.assertFalse(state.isExpanded(b));
257     Assert.assertFalse(state.isExpanded(c));
258     Assert.assertFalse(state.isExpanded(a1));
259     Assert.assertFalse(state.isExpanded(a2));
260     Assert.assertFalse(state.isExpanded(a3));
261     Assert.assertFalse(state.isExpanded(b1));
262     Assert.assertFalse(state.isExpanded(b2));
263     Assert.assertFalse(state.isExpanded(b3));
264     Assert.assertFalse(state.isExpanded(c1));
265     Assert.assertFalse(state.isExpanded(c2));
266     Assert.assertFalse(state.isExpanded(c3));
267     Assert.assertEquals(0, state.getExpandedSet().size());
268     Assert.assertEquals(0, state.getCollapsedSet().size());
269 
270     state.expand(2);
271     state.expand(b); // do something noise
272     state.expand(c1); // do something noise
273     state.collapseAll();
274     Assert.assertFalse(state.isExpanded(root));
275     Assert.assertFalse(state.isExpanded(a));
276     Assert.assertFalse(state.isExpanded(b));
277     Assert.assertFalse(state.isExpanded(c));
278     Assert.assertFalse(state.isExpanded(a1));
279     Assert.assertFalse(state.isExpanded(a2));
280     Assert.assertFalse(state.isExpanded(a3));
281     Assert.assertFalse(state.isExpanded(b1));
282     Assert.assertFalse(state.isExpanded(b2));
283     Assert.assertFalse(state.isExpanded(b3));
284     Assert.assertFalse(state.isExpanded(c1));
285     Assert.assertFalse(state.isExpanded(c2));
286     Assert.assertFalse(state.isExpanded(c3));
287     Assert.assertEquals(0, state.getExpandedSet().size());
288     Assert.assertEquals(0, state.getCollapsedSet().size());
289 
290     state.expand(2);
291     state.expand(b); // do something noise
292     state.expand(c1); // do something noise
293     state.expandAll();
294     Assert.assertTrue(state.isExpanded(root));
295     Assert.assertTrue(state.isExpanded(a));
296     Assert.assertTrue(state.isExpanded(b));
297     Assert.assertTrue(state.isExpanded(c));
298     Assert.assertTrue(state.isExpanded(a1));
299     Assert.assertTrue(state.isExpanded(a2));
300     Assert.assertTrue(state.isExpanded(a3));
301     Assert.assertTrue(state.isExpanded(b1));
302     Assert.assertTrue(state.isExpanded(b2));
303     Assert.assertTrue(state.isExpanded(b3));
304     Assert.assertTrue(state.isExpanded(c1));
305     Assert.assertTrue(state.isExpanded(c2));
306     Assert.assertTrue(state.isExpanded(c3));
307     Assert.assertEquals(0, state.getExpandedSet().size());
308     Assert.assertEquals(0, state.getCollapsedSet().size());
309 
310     state.expand(2);
311     state.expand(b); // do something noise
312     state.expand(c1); // do something noise
313     state.collapseAllButRoot();
314     Assert.assertTrue(state.isExpanded(root));
315     Assert.assertFalse(state.isExpanded(a));
316     Assert.assertFalse(state.isExpanded(b));
317     Assert.assertFalse(state.isExpanded(c));
318     Assert.assertFalse(state.isExpanded(a1));
319     Assert.assertFalse(state.isExpanded(a2));
320     Assert.assertFalse(state.isExpanded(a3));
321     Assert.assertFalse(state.isExpanded(b1));
322     Assert.assertFalse(state.isExpanded(b2));
323     Assert.assertFalse(state.isExpanded(b3));
324     Assert.assertFalse(state.isExpanded(c1));
325     Assert.assertFalse(state.isExpanded(c2));
326     Assert.assertFalse(state.isExpanded(c3));
327     Assert.assertEquals(0, state.getExpandedSet().size());
328     Assert.assertEquals(0, state.getCollapsedSet().size());
329 
330     // text TreeNode
331 
332     state.expand(A);
333     Assert.assertTrue(state.isExpanded(A));
334     state.collapse(A);
335     Assert.assertFalse(state.isExpanded(A));
336 
337 
338   }
339 }