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 java.util.Stack;
23  
24  import javax.faces.component.html.HtmlForm;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  /**
30   * - .. Root (0)
31   * |
32   * - .. A (0:0)
33   * |    |
34   * |    + .. AA (0:0:0)
35   * |    |
36   * |    + .. AB (0:0:1)
37   * |    |
38   * |    + .. AC (0:0:2)
39   * |         | .. aca (0:0:2:0)
40   * |         | .. acb (0:0:2:1)
41   * |
42   * + .. B (0:1)
43   * |    |
44   * |    + .. BA (0:1:0)
45   * |    |
46   * |    + .. BB (0:1:1)
47   * |
48   * | .. C (0:2)
49   * |
50   * | .. d (0:3)
51   */
52  
53  public class HtmlTreeRendererTest extends AbstractTreeTestCase
54  {
55      private NodeSimulator nodeSim;
56  
57      /**
58       * Constructor
59       * @param name String
60       */
61      public HtmlTreeRendererTest(String name)
62      {
63          super(name);
64      }
65  
66      /**
67       * See abstract class
68       */
69      protected void setUp() throws Exception
70      {
71          super.setUp();
72  
73          nodeSim = new NodeSimulator();
74  
75          // the tree needs a default facet - use a node simulator instead of the usual components
76          // so we can detect when a node is rendered
77          tree.getFacets().put(DEFAULT_NODE_TYPE, nodeSim);
78  
79          // set up the renderer for the component.  with mock stuff we don't have faces config doing this for us
80          renderKit.addRenderer(HtmlTree.COMPONENT_FAMILY, HtmlTree.COMPONENT_TYPE, new HtmlTreeRenderer());
81      }
82  
83      /**
84       * Tests the option of hiding the root node (with server side toggling option)
85       * @throws Exception
86       */
87      public void testHideRootNodeServer() throws Exception
88      {
89          tree.setParent(new HtmlForm());
90          tree.setClientSideToggle(false);
91          tree.setShowRootNode(false);
92  
93          HtmlTreeRenderer treeRenderer = new HtmlTreeRenderer();
94          treeRenderer.encodeChildren(facesContext, tree);
95  
96          // we expect the nodes to come off the stack in the reverse order in which they were added
97          Stack nodeStack = nodeSim.getNodeStack();
98          int numRendered = nodeStack.size();
99          assertEquals("Unexpected number of nodes rendered", 4, numRendered);
100 
101         TreeNode node = (TreeNode)nodeStack.pop();
102         assertEquals("d", node.getIdentifier());
103 
104         node = (TreeNode)nodeStack.pop();
105         assertEquals("C", node.getIdentifier());
106 
107         node = (TreeNode)nodeStack.pop();
108         assertEquals("B", node.getIdentifier());
109 
110         node = (TreeNode)nodeStack.pop();
111         assertEquals("A", node.getIdentifier());
112     }
113 
114     /**
115      * Tests the option of hiding the root node (with client side toggling option)
116      * @throws Exception
117      */
118     public void testHideRootNodeClient() throws Exception
119     {
120         tree.setClientSideToggle(true);
121         tree.setShowRootNode(false);
122 
123         HtmlTreeRenderer treeRenderer = new HtmlTreeRenderer();
124         treeRenderer.encodeChildren(facesContext, tree);
125 
126         // we expect the nodes to come off the stack in the reverse order in which they were added
127         Stack nodeStack = nodeSim.getNodeStack();
128         int numRendered = nodeStack.size();
129         assertEquals("Unexpected number of nodes rendered", 11, numRendered);
130 
131         TreeNode node = (TreeNode)nodeStack.pop();
132         assertEquals("d", node.getIdentifier());
133 
134         node = (TreeNode)nodeStack.pop();
135         assertEquals("C", node.getIdentifier());
136 
137         node = (TreeNode)nodeStack.pop();
138         assertEquals("BB", node.getIdentifier());
139 
140         node = (TreeNode)nodeStack.pop();
141         assertEquals("BA", node.getIdentifier());
142 
143         node = (TreeNode)nodeStack.pop();
144         assertEquals("B", node.getIdentifier());
145 
146         node = (TreeNode)nodeStack.pop();
147         assertEquals("acb", node.getIdentifier());
148 
149         node = (TreeNode)nodeStack.pop();
150         assertEquals("aca", node.getIdentifier());
151 
152         node = (TreeNode)nodeStack.pop();
153         assertEquals("AC", node.getIdentifier());
154 
155         node = (TreeNode)nodeStack.pop();
156         assertEquals("AB", node.getIdentifier());
157 
158         node = (TreeNode)nodeStack.pop();
159         assertEquals("AA", node.getIdentifier());
160 
161         node = (TreeNode)nodeStack.pop();
162         assertEquals("A", node.getIdentifier());
163     }
164 
165     public static Test suite()
166     {
167         return new TestSuite(HtmlTreeRendererTest.class);
168     }
169 }