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.io.IOException;
23  import java.util.Stack;
24  
25  import javax.faces.component.UIComponentBase;
26  import javax.faces.context.FacesContext;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
32  
33  /**
34   * An abstract test case that sets up a Tree2 for testing using the structure below.
35   *
36   * - .. Root (0)
37   * |
38   * - .. A (0:0)
39   * |    |
40   * |    + .. AA (0:0:0)
41   * |    |
42   * |    + .. AB (0:0:1)
43   * |    |
44   * |    + .. AC (0:0:2)
45   * |         | .. aca (0:0:2:0)
46   * |         | .. acb (0:0:2:1)
47   * |
48   * + .. B (0:1)
49   * |    |
50   * |    + .. BA (0:1:0)
51   * |    |
52   * |    + .. BB (0:1:1)
53   * |
54   * | .. C (0:2)
55   * |
56   * | .. d (0:3)
57   */
58  
59  public class AbstractTreeTestCase extends AbstractTomahawkViewControllerTestCase
60  {
61      static final String DEFAULT_NODE_TYPE = "default";
62      static final String TREE_ID = "some_foo_tree";
63  
64      HtmlTree tree;
65      TreeNodeBase rootNode;
66  
67      /**
68       * Constructor
69       * @param name String
70       */
71      public AbstractTreeTestCase(String name)
72      {
73          super(name);
74      }
75  
76      /**
77       * See abstract class
78       */
79      protected void setUp() throws Exception
80      {
81          super.setUp();
82  
83          tree = new HtmlTree();
84          tree.setId(TREE_ID);
85  
86          // set up the test tree with the standard data
87          rootNode = new TreeNodeBase(DEFAULT_NODE_TYPE, "Root", "Root", false);
88  
89          TreeNodeBase A_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "A", "A", false);
90          A_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "AA", "AA", false));
91          A_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "AB", "AB", false));
92          TreeNodeBase AC_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "AC", "AC", false);
93          AC_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "aca", "aca", true));
94          AC_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "acb", "acb", true));
95          A_node.getChildren().add(AC_node);
96          rootNode.getChildren().add(A_node);
97  
98          TreeNodeBase B_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "B", "B", false);
99          B_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "BA", "BA", false));
100         B_node.getChildren().add(new TreeNodeBase(DEFAULT_NODE_TYPE, "BB", "BB", false));
101         rootNode.getChildren().add(B_node);
102 
103         TreeNodeBase C_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "C", "C", false);
104         rootNode.getChildren().add(C_node);
105 
106         TreeNodeBase d_node = new TreeNodeBase(DEFAULT_NODE_TYPE, "d", "d", true);
107         rootNode.getChildren().add(d_node);
108 
109         tree.setValue(rootNode);
110     }
111 
112     /**
113      * Instead of using UIText or whatever inside the tree we use this special "simulator."
114      * This way we can keep track of when the renderer is asking the node to render itself.
115      */
116     static final class NodeSimulator extends UIComponentBase
117     {
118         private static final String COMPONENT_FAMILY = "Foo";
119         private Stack nodeStack = new Stack();
120 
121         public String getFamily()
122         {
123             return COMPONENT_FAMILY;
124         }
125 
126         public void encodeBegin(FacesContext context) throws IOException
127         {
128             HtmlTree tree = (HtmlTree)super.getParent();
129             nodeStack.push(tree.getNode());
130         }
131 
132         public Stack getNodeStack()
133         {
134             return nodeStack;
135         }
136     }
137 
138     // Return the tests included in this test case.
139     public static Test suite()
140     {
141         return (new TestSuite());
142     }
143 }