1   package com.sri.common.taglib;
2   
3   import javax.servlet.jsp.JspException;
4   import org.apache.cactus.JspTestCase;
5   import org.apache.cactus.WebResponse;
6   
7   /***
8    * @author Michael Rimov
9    */
10  public class TestTreeMenuTag extends JspTestCase {
11      private TreeMenuTag treeMenuTag = null;
12  
13      private TreeNode testNode = null;
14  
15      public TestTreeMenuTag(String name) {
16          super(name);
17      }
18  
19      protected void setUp() throws Exception {
20          super.setUp();
21          treeMenuTag = new TreeMenuTag();
22          testNode = new DefaultTreeNode();
23          setUpTreeNode((DefaultTreeNode) testNode);
24          this.request.setAttribute("TreeView", testNode);
25          treeMenuTag.setValue("${request.TreeView}");
26      }
27  
28      /***
29       * Constructs a tree node with nodes and leaves.
30       *
31       * @param root DefaultTreeNode
32       */
33      protected void setUpTreeNode(DefaultTreeNode root) {
34          root.setLabel("All Nodes");
35          root.setSelected(true);
36          root.setSelectedStyle("folder-open");
37          root.setUnselectedStyle("folder-close");
38          for (int i = 1; i < 10; i++) {
39              root.addNested(constructFolderNode(root));
40          }
41  
42          for (int i = 1; i < 5; i++) {
43              root.addNested(constructLeafNode(root));
44          }
45      }
46  
47      /***
48       * Constructs af older node.
49       *
50       * @param parent DefaultTreeNode
51       * @return DefaultTreeNode
52       */
53      protected DefaultTreeNode constructFolderNode(DefaultTreeNode parent) {
54          DefaultTreeNode newNode = new DefaultTreeNode(parent);
55          newNode.setSelected(true);
56          newNode.setLabel("Folder");
57          newNode.setSelectedStyle("folder-open");
58          newNode.setUnselectedStyle("folder-close");
59          for (int i = 1; i < 3; i++) {
60              newNode.addNested(constructLeafNode(parent));
61          }
62  
63          return newNode;
64      }
65  
66      /***
67       * Constructs a leaf node.
68       *
69       * @param parent DefaultTreeNode
70       * @return DefaultTreeNode
71       */
72      protected DefaultTreeNode constructLeafNode(DefaultTreeNode parent) {
73          DefaultTreeNode newNode = new DefaultTreeNode(parent);
74          newNode.setSelected(false);
75          newNode.setLabel("Item");
76          newNode.setSelectedStyle("selected");
77          newNode.setUnselectedStyle("subitem");
78          newNode.setLink("http://www.sri.com");
79          return newNode;
80      }
81  
82      protected void tearDown() throws Exception {
83          treeMenuTag = null;
84          testNode = null;
85          super.tearDown();
86      }
87  
88      public void testCheckGoodELEvaluation() throws JspException {
89          int value = treeMenuTag.doEndTag();
90          assertTrue(value == TreeMenuTag.EVAL_PAGE);
91      }
92  
93      public void testCheckBadELEvaluation() {
94          treeMenuTag.setValue("${abcdefg}");
95          try {
96              treeMenuTag.doEndTag();
97              fail("doEndTag() should have thrown an exception because of bad EL passed in");
98          } catch (JspException ex) {
99          	assertNotNull(ex.getMessage());
100         }
101     }
102 
103     public void testCheckTagRendering() throws JspException {
104         treeMenuTag.doEndTag();
105     }
106 
107     public void endCheckTagRendering(WebResponse response) throws JspException {
108         String value = response.getText();
109         assertTrue(value.indexOf("<ul") > -1);
110         assertTrue(value.indexOf("</ul>") > -1);
111         assertTrue(value.indexOf("<li") > -1);
112         assertTrue(value.indexOf("</li>") > -1);
113     }
114 
115 
116 }