View Javadoc

1   /*
2    * $Id: PropertyTagTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.views.jsp;
23  
24  import java.util.HashMap;
25  
26  import javax.servlet.jsp.JspException;
27  
28  import org.apache.struts2.ServletActionContext;
29  import org.apache.struts2.StrutsConstants;
30  import org.apache.struts2.StrutsTestCase;
31  
32  import com.mockobjects.servlet.MockJspWriter;
33  import com.mockobjects.servlet.MockPageContext;
34  import com.opensymphony.xwork2.ActionContext;
35  import com.opensymphony.xwork2.util.ValueStack;
36  import com.opensymphony.xwork2.util.ValueStackFactory;
37  
38  
39  /***
40   * PropertyTag test case.
41   *
42   */
43  public class PropertyTagTest extends StrutsTestCase {
44  
45      StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
46      ValueStack stack;
47  
48  
49      public void testDefaultValue() {
50          PropertyTag tag = new PropertyTag();
51  
52          Foo foo = new Foo();
53  
54          stack.push(foo);
55  
56          MockJspWriter jspWriter = new MockJspWriter();
57          jspWriter.setExpectedData("TEST");
58  
59          MockPageContext pageContext = new MockPageContext();
60          pageContext.setJspWriter(jspWriter);
61          pageContext.setRequest(request);
62  
63          tag.setPageContext(pageContext);
64          tag.setValue("title");
65          tag.setDefault("TEST");
66  
67          try {
68              tag.doStartTag();
69          } catch (JspException e) {
70              e.printStackTrace();
71              fail();
72          }
73  
74          request.verify();
75          jspWriter.verify();
76          pageContext.verify();
77      }
78  
79      public void testNull() {
80          PropertyTag tag = new PropertyTag();
81  
82          Foo foo = new Foo();
83  
84          stack.push(foo);
85  
86          MockJspWriter jspWriter = new MockJspWriter();
87          jspWriter.setExpectedData("");
88  
89          MockPageContext pageContext = new MockPageContext();
90          pageContext.setJspWriter(jspWriter);
91          pageContext.setRequest(request);
92  
93          tag.setPageContext(pageContext);
94          tag.setValue("title");
95  
96          try {
97              tag.doStartTag();
98          } catch (JspException e) {
99              e.printStackTrace();
100             fail();
101         }
102 
103         request.verify();
104         jspWriter.verify();
105         pageContext.verify();
106     }
107 
108     public void testSimple() {
109         PropertyTag tag = new PropertyTag();
110 
111         Foo foo = new Foo();
112         foo.setTitle("test");
113 
114         stack.push(foo);
115 
116         MockJspWriter jspWriter = new MockJspWriter();
117         jspWriter.setExpectedData("test");
118 
119         MockPageContext pageContext = new MockPageContext();
120         pageContext.setJspWriter(jspWriter);
121         pageContext.setRequest(request);
122 
123         tag.setPageContext(pageContext);
124         tag.setValue("title");
125 
126         try {
127             tag.doStartTag();
128         } catch (JspException e) {
129             e.printStackTrace();
130             fail();
131         }
132 
133         request.verify();
134         jspWriter.verify();
135         pageContext.verify();
136     }
137 
138     public void testTopOfStack() {
139         PropertyTag tag = new PropertyTag();
140 
141         Foo foo = new Foo();
142         foo.setTitle("test");
143 
144         stack.push(foo);
145 
146         MockJspWriter jspWriter = new MockJspWriter();
147         jspWriter.setExpectedData("Foo is: test");
148 
149         MockPageContext pageContext = new MockPageContext();
150         pageContext.setJspWriter(jspWriter);
151         pageContext.setRequest(request);
152 
153         tag.setPageContext(pageContext);
154 
155         try {
156             tag.doStartTag();
157         } catch (JspException e) {
158             e.printStackTrace();
159             fail();
160         }
161 
162         request.verify();
163         jspWriter.verify();
164         pageContext.verify();
165     }
166 
167 
168     public void testWithAltSyntax1() throws Exception {
169         // setups
170         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}});
171 
172         Foo foo = new Foo();
173         foo.setTitle("tm_jee");
174         stack.push(foo);
175 
176         MockJspWriter jspWriter = new MockJspWriter();
177         jspWriter.setExpectedData("Foo is: tm_jee");
178 
179         MockPageContext pageContext = new MockPageContext();
180         pageContext.setJspWriter(jspWriter);
181         pageContext.setRequest(request);
182 
183         // test
184         {PropertyTag tag = new PropertyTag();
185         tag.setPageContext(pageContext);
186         tag.setValue("%{toString()}");
187         tag.doStartTag();
188         tag.doEndTag();}
189 
190         // verify test
191         request.verify();
192         jspWriter.verify();
193         pageContext.verify();
194     }
195 
196     public void testWithAltSyntax2() throws Exception {
197         // setups
198         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}});
199 
200         Foo foo = new Foo();
201         foo.setTitle("tm_jee");
202         stack.push(foo);
203 
204         MockJspWriter jspWriter = new MockJspWriter();
205         jspWriter.setExpectedData("Foo is: tm_jee");
206 
207         MockPageContext pageContext = new MockPageContext();
208         pageContext.setJspWriter(jspWriter);
209         pageContext.setRequest(request);
210 
211         // test
212         {PropertyTag tag = new PropertyTag();
213         tag.setPageContext(pageContext);
214         tag.setValue("toString()");
215         tag.doStartTag();
216         tag.doEndTag();}
217 
218         // verify test
219         request.verify();
220         jspWriter.verify();
221         pageContext.verify();
222     }
223 
224     public void testWithoutAltSyntax1() throws Exception {
225         //      setups
226         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "false");}});
227 
228         Foo foo = new Foo();
229         foo.setTitle("tm_jee");
230         stack.push(foo);
231 
232         MockJspWriter jspWriter = new MockJspWriter();
233         jspWriter.setExpectedData("Foo is: tm_jee");
234 
235         MockPageContext pageContext = new MockPageContext();
236         pageContext.setJspWriter(jspWriter);
237         pageContext.setRequest(request);
238 
239         // test
240         {PropertyTag tag = new PropertyTag();
241         tag.setPageContext(pageContext);
242         tag.setValue("toString()");
243         tag.doStartTag();
244         tag.doEndTag();}
245 
246         // verify test
247         request.verify();
248         jspWriter.verify();
249         pageContext.verify();
250     }
251 
252 
253     public void testWithoutAltSyntax2() throws Exception {
254         //      setups
255         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "false");}});
256 
257         Foo foo = new Foo();
258         foo.setTitle("tm_jee");
259         stack.push(foo);
260 
261         MockJspWriter jspWriter = new MockJspWriter();
262 
263         MockPageContext pageContext = new MockPageContext();
264         pageContext.setJspWriter(jspWriter);
265         pageContext.setRequest(request);
266 
267         // test
268         {PropertyTag tag = new PropertyTag();
269         tag.setPageContext(pageContext);
270         tag.setValue("%{toString()}");
271         tag.doStartTag();
272         tag.doEndTag();}
273 
274         // verify test
275         request.verify();
276         jspWriter.verify();
277         pageContext.verify();
278     }
279 
280 
281     protected void setUp() throws Exception {
282         super.setUp();
283         stack = ActionContext.getContext().getValueStack();
284         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
285     }
286 
287 
288     public class Foo {
289         private String title;
290 
291         public void setTitle(String title) {
292             this.title = title;
293         }
294 
295         public String getTitle() {
296             return title;
297         }
298 
299         public String toString() {
300             return "Foo is: " + title;
301         }
302     }
303 }