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.custom.tree2;
21  
22  import junit.framework.TestCase;
23  
24  
25  /**
26   * - .. Root (0)
27   * |
28   * - .. A (0:0)
29   * |    |
30   * |    + .. AA (0:0:0)
31   * |    |
32   * |    + .. AB (0:0:1)
33   * |    |
34   * |    + .. AC (0:0:2)
35   * |         | .. aca (0:0:2:0)
36   * |         | .. acb (0:0:2:1)
37   * |
38   * + .. B (0:1)
39   * |    |
40   * |    + .. BA (0:1:0)
41   * |    |
42   * |    + .. BB (0:1:1)
43   * |
44   * | .. C (0:2)
45   * |
46   * | .. d (0:3)
47   */
48  
49  public class TreeWalkerBaseTest extends TestCase
50  {
51      private static final String DEFAULT_NODE_TYPE = "default";
52  
53      UITreeData treeData = new UITreeData();
54      TreeWalker treeWalker;
55  
56      protected void setUp() throws Exception
57      {
58          // set up the test tree with the standard data
59          TreeNodeBase Root_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "Root", "Root", false);
60  
61          TreeNodeBase A_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "A", "A", false);
62          A_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "AA", "AA", false));
63          A_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "AB", "AB", false));
64          TreeNodeBase AC_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "AC", "AC", false);
65          AC_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "aca", "aca", true));
66          AC_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "acb", "acb", true));
67          A_node.getChildren().add(AC_node);
68          Root_node.getChildren().add(A_node);
69  
70          TreeNodeBase B_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "B", "B", false);
71          B_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "BA", "BA", false));
72          B_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "BB", "BB", false));
73          Root_node.getChildren().add(B_node);
74  
75          TreeNodeBase C_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "C", "C", false);
76          Root_node.getChildren().add(C_node);
77  
78          TreeNodeBase d_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "d", "d", true);
79          Root_node.getChildren().add(d_node);
80  
81          treeData.setValue(Root_node);
82  
83          // setup the tree state
84          TreeState treeState = treeData.getDataModel().getTreeState();
85          treeState.toggleExpanded("0");
86          treeState.toggleExpanded("0:0");
87  
88          // setup the tree walker
89          treeWalker = treeData.getDataModel().getTreeWalker();
90          treeWalker.setTree(treeData);
91      }
92  
93      public void testGetRootNodeId() throws Exception
94      {
95          String nodeId = treeWalker.getRootNodeId();
96          assertEquals("Unexpected rootId", "0", nodeId);
97      }
98  
99      /**
100      * Walk through all of the nodes and make sure the return value is correct as well as the current node of
101      * the tree.  Continue until the walker runs out of nodes.  Since the checkState property is true only the
102      * expanded nodes will be walked.
103      *
104      * @throws Exception
105      */
106     public void testNextCheckState() throws Exception
107     {
108         treeWalker.setCheckState(true);
109 
110         assertTrue("unxpected return value", treeWalker.next());
111         assertEquals("unexpected nodeId", "0", treeData.getNodeId());
112         assertEquals("unexpected node", "Root", treeData.getNode().getIdentifier());
113 
114         assertTrue("unxpected return value", treeWalker.next());
115         assertEquals("unexpected nodeId", "0:0", treeData.getNodeId());
116         assertEquals("unexpected node", "A", treeData.getNode().getIdentifier());
117 
118         assertTrue("unxpected return value", treeWalker.next());
119         assertEquals("unexpected nodeId", "0:0:0", treeData.getNodeId());
120         assertEquals("unexpected node", "AA", treeData.getNode().getIdentifier());
121 
122         assertTrue("unxpected return value", treeWalker.next());
123         assertEquals("unexpected nodeId", "0:0:1", treeData.getNodeId());
124         assertEquals("unexpected node", "AB", treeData.getNode().getIdentifier());
125 
126         assertTrue("unxpected return value", treeWalker.next());
127         assertEquals("unexpected nodeId", "0:0:2", treeData.getNodeId());
128         assertEquals("unexpected node", "AC", treeData.getNode().getIdentifier());
129 
130         assertTrue("unxpected return value", treeWalker.next());
131         assertEquals("unexpected nodeId", "0:1", treeData.getNodeId());
132         assertEquals("unexpected node", "B", treeData.getNode().getIdentifier());
133 
134         assertTrue("unxpected return value", treeWalker.next());
135         assertEquals("unexpected nodeId", "0:2", treeData.getNodeId());
136         assertEquals("unexpected node", "C", treeData.getNode().getIdentifier());
137 
138         assertTrue("unxpected return value", treeWalker.next());
139         assertEquals("unexpected nodeId", "0:3", treeData.getNodeId());
140         assertEquals("unexpected node", "d", treeData.getNode().getIdentifier());
141 
142         // not expecting anymore nodes
143         assertFalse("unxpected return value", treeWalker.next());
144     }
145 
146 
147     /**
148      * Walk through all of the nodes and make sure the return value is correct as well as the current node of
149      * the tree.  Continue until the walker runs out of nodes.  Sets the checkState property to false so all of
150      * the nodes will be walked.
151      *
152      * @throws Exception
153      */
154     public void testNextNoCheckState() throws Exception
155     {
156         // walk through all of the nodes and make sure the return value is correct until we run out of
157         // nodes
158         treeWalker.setCheckState(false);
159 
160         assertTrue("unxpected return value", treeWalker.next());
161         assertEquals("unexpected nodeId", "0", treeData.getNodeId());
162         assertEquals("unexpected node", "Root", treeData.getNode().getIdentifier());
163 
164 
165         assertTrue("unxpected return value", treeWalker.next());
166         assertEquals("unexpected nodeId", "0:0", treeData.getNodeId());
167         assertEquals("unexpected node", "A", treeData.getNode().getIdentifier());
168 
169         assertTrue("unxpected return value", treeWalker.next());
170         assertEquals("unexpected nodeId", "0:0:0", treeData.getNodeId());
171         assertEquals("unexpected node", "AA", treeData.getNode().getIdentifier());
172 
173         assertTrue("unxpected return value", treeWalker.next());
174         assertEquals("unexpected nodeId", "0:0:1", treeData.getNodeId());
175         assertEquals("unexpected node", "AB", treeData.getNode().getIdentifier());
176 
177         assertTrue("unxpected return value", treeWalker.next());
178         assertEquals("unexpected nodeId", "0:0:2", treeData.getNodeId());
179         assertEquals("unexpected node", "AC", treeData.getNode().getIdentifier());
180 
181         assertTrue("unxpected return value", treeWalker.next());
182         assertEquals("unexpected nodeId", "0:0:2:0", treeData.getNodeId());
183         assertEquals("unexpected node", "aca", treeData.getNode().getIdentifier());
184 
185         assertTrue("unxpected return value", treeWalker.next());
186         assertEquals("unexpected nodeId", "0:0:2:1", treeData.getNodeId());
187         assertEquals("unexpected node", "acb", treeData.getNode().getIdentifier());
188 
189         assertTrue("unxpected return value", treeWalker.next());
190         assertEquals("unexpected nodeId", "0:1", treeData.getNodeId());
191         assertEquals("unexpected node", "B", treeData.getNode().getIdentifier());
192 
193         assertTrue("unxpected return value", treeWalker.next());
194         assertEquals("unexpected nodeId", "0:1:0", treeData.getNodeId());
195         assertEquals("unexpected node", "BA", treeData.getNode().getIdentifier());
196 
197         assertTrue("unxpected return value", treeWalker.next());
198         assertEquals("unexpected nodeId", "0:1:1", treeData.getNodeId());
199         assertEquals("unexpected node", "BB", treeData.getNode().getIdentifier());
200 
201         assertTrue("unxpected return value", treeWalker.next());
202         assertEquals("unexpected nodeId", "0:2", treeData.getNodeId());
203         assertEquals("unexpected node", "C", treeData.getNode().getIdentifier());
204 
205         assertTrue("unxpected return value", treeWalker.next());
206         assertEquals("unexpected nodeId", "0:3", treeData.getNodeId());
207         assertEquals("unexpected node", "d", treeData.getNode().getIdentifier());
208 
209         // not expecting anymore nodes
210         assertFalse("unxpected return value", treeWalker.next());
211     }
212 
213     /**
214      * Walk through the tree.  Then call reset.  Make sure the tree is walked through again
215      * from the beginning.  This can be tested by running some of the other tests twice with
216      * a call to reset in between.
217      */
218     public void testReset()
219     {
220         try
221         {
222             testNextCheckState();
223             treeWalker.reset();
224             testNextCheckState();
225         }
226         catch (Exception e)
227         {
228             fail("Unable to successfuly check the next method twice with a reset");
229         }
230     }
231 }