View Javadoc

1   /*
2    * $Id: TreeTest.java 440597 2006-09-06 03:34:39Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp.ui;
19  
20  import org.apache.struts2.views.jsp.AbstractUITagTest;
21  
22  import com.opensymphony.xwork2.Action;
23  import com.opensymphony.xwork2.ActionSupport;
24  
25  /***
26   * Test case for Tree component.
27   */
28  public class TreeTest extends AbstractUITagTest{
29  	
30  	public void testStaticTree() throws Exception {
31  		// Root
32  		TreeTag tag = new TreeTag();
33  		tag.setShowRootGrid("false");
34  		tag.setShowGrid("false");
35  		tag.setTemplateCssPath("/struts/tree.css");
36  		tag.setTheme("ajax");
37  		tag.setPageContext(pageContext);
38  		tag.setId("rootId");
39  		tag.setLabel("Root");
40  		tag.doStartTag();
41  		
42  			// Child 1
43  			TreeNodeTag nodeTag1 = new TreeNodeTag();
44  			nodeTag1.setTheme("ajax");
45  			nodeTag1.setPageContext(pageContext);
46  			nodeTag1.setId("child1");
47  			nodeTag1.setLabel("Child 1");
48  			nodeTag1.doStartTag();
49  			nodeTag1.doEndTag();
50  			
51  			// Child 2
52  			TreeNodeTag nodeTag2 = new TreeNodeTag();
53  			nodeTag2.setTheme("ajax");
54  			nodeTag2.setPageContext(pageContext);
55  			nodeTag2.setId("child2");
56  			nodeTag2.setLabel("Child 2");
57  			nodeTag2.doStartTag();
58  			
59  				// Grand Child 1
60  				TreeNodeTag gNodeTag1 = new TreeNodeTag();
61  				gNodeTag1.setTheme("ajax");
62  				gNodeTag1.setPageContext(pageContext);
63  				gNodeTag1.setId("gChild1");
64  				gNodeTag1.setLabel("Grand Child 1");
65  				gNodeTag1.doStartTag();
66  				gNodeTag1.doEndTag();
67  			
68  				// Grand Child 2
69  				TreeNodeTag gNodeTag2 = new TreeNodeTag();
70  				gNodeTag2.setTheme("ajax");
71  				gNodeTag2.setPageContext(pageContext);
72  				gNodeTag2.setId("gChild2");
73  				gNodeTag2.setLabel("Grand Child 2");
74  				gNodeTag2.doStartTag();
75  				gNodeTag2.doEndTag();
76  			
77  				// Grand Child 3
78  				TreeNodeTag gNodeTag3= new TreeNodeTag();
79  				gNodeTag3.setTheme("ajax");
80  				gNodeTag3.setPageContext(pageContext);
81  				gNodeTag3.setId("gChild3");
82  				gNodeTag3.setLabel("Grand Child 3");
83  				gNodeTag3.doStartTag();
84  				gNodeTag3.doEndTag();
85  				
86  			nodeTag2.doEndTag();
87  			
88  			
89  			// Child 3
90  			TreeNodeTag nodeTag3 = new TreeNodeTag();
91  			nodeTag3.setTheme("ajax");
92  			nodeTag3.setPageContext(pageContext);
93  			nodeTag3.setId("child3");
94  			nodeTag3.setLabel("Child 4");
95  			nodeTag3.doStartTag();
96  			nodeTag3.doEndTag();
97  			
98  		tag.doEndTag();
99  		
100 		//System.out.println(writer.toString());
101 		verify(TreeTest.class.getResource("tree-1.txt"));
102 	}
103 	
104 	
105 	
106 	public void testDynamicTree() throws Exception {
107 		
108 		TreeTag tag = new TreeTag();
109 		tag.setPageContext(pageContext);
110 		tag.setTheme("ajax");
111 		tag.setId("myTree");
112 		tag.setRootNode("%{myTreeRoot}");
113 		tag.setNodeIdProperty("id");
114 		tag.setNodeTitleProperty("name");
115 		tag.setChildCollectionProperty("children");
116 		tag.doStartTag();
117 		tag.doEndTag();
118 		
119 		//System.out.println(writer.toString());
120 		verify(TreeTest.class.getResource("tree-2.txt"));
121 	}
122 	
123 	
124 	public Action getAction() {
125 		return new InternalActionSupport();
126 	}
127 	
128 	public static class InternalActionSupport extends ActionSupport {
129 		public Category getMyTreeRoot() {
130 			return Category.getById(1);
131 		}
132 	}
133 }