View Javadoc

1   /*
2    * $Id: PropertyTagTest.java 474191 2006-11-13 08:30:40Z mrdon $
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  package org.apache.struts2.views.jsp;
22  
23  import java.util.HashMap;
24  
25  import javax.servlet.jsp.JspException;
26  
27  import org.apache.struts2.ServletActionContext;
28  import org.apache.struts2.StrutsConstants;
29  import org.apache.struts2.StrutsTestCase;
30  
31  import com.mockobjects.servlet.MockJspWriter;
32  import com.mockobjects.servlet.MockPageContext;
33  import com.opensymphony.xwork2.ActionContext;
34  import com.opensymphony.xwork2.util.ValueStack;
35  import com.opensymphony.xwork2.util.ValueStackFactory;
36  
37  
38  /***
39   * PropertyTag test case.
40   *
41   */
42  public class PropertyTagTest extends StrutsTestCase {
43  
44      StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
45      ValueStack stack = ValueStackFactory.getFactory().createValueStack();
46  
47  
48      public void testDefaultValue() {
49          PropertyTag tag = new PropertyTag();
50  
51          Foo foo = new Foo();
52  
53          stack.push(foo);
54  
55          MockJspWriter jspWriter = new MockJspWriter();
56          jspWriter.setExpectedData("TEST");
57  
58          MockPageContext pageContext = new MockPageContext();
59          pageContext.setJspWriter(jspWriter);
60          pageContext.setRequest(request);
61  
62          tag.setPageContext(pageContext);
63          tag.setValue("title");
64          tag.setDefault("TEST");
65  
66          try {
67              tag.doStartTag();
68          } catch (JspException e) {
69              e.printStackTrace();
70              fail();
71          }
72  
73          request.verify();
74          jspWriter.verify();
75          pageContext.verify();
76      }
77  
78      public void testNull() {
79          PropertyTag tag = new PropertyTag();
80  
81          Foo foo = new Foo();
82  
83          stack.push(foo);
84  
85          MockJspWriter jspWriter = new MockJspWriter();
86          jspWriter.setExpectedData("");
87  
88          MockPageContext pageContext = new MockPageContext();
89          pageContext.setJspWriter(jspWriter);
90          pageContext.setRequest(request);
91  
92          tag.setPageContext(pageContext);
93          tag.setValue("title");
94  
95          try {
96              tag.doStartTag();
97          } catch (JspException e) {
98              e.printStackTrace();
99              fail();
100         }
101 
102         request.verify();
103         jspWriter.verify();
104         pageContext.verify();
105     }
106 
107     public void testSimple() {
108         PropertyTag tag = new PropertyTag();
109 
110         Foo foo = new Foo();
111         foo.setTitle("test");
112 
113         stack.push(foo);
114 
115         MockJspWriter jspWriter = new MockJspWriter();
116         jspWriter.setExpectedData("test");
117 
118         MockPageContext pageContext = new MockPageContext();
119         pageContext.setJspWriter(jspWriter);
120         pageContext.setRequest(request);
121 
122         tag.setPageContext(pageContext);
123         tag.setValue("title");
124 
125         try {
126             tag.doStartTag();
127         } catch (JspException e) {
128             e.printStackTrace();
129             fail();
130         }
131 
132         request.verify();
133         jspWriter.verify();
134         pageContext.verify();
135     }
136 
137     public void testTopOfStack() {
138         PropertyTag tag = new PropertyTag();
139 
140         Foo foo = new Foo();
141         foo.setTitle("test");
142 
143         stack.push(foo);
144 
145         MockJspWriter jspWriter = new MockJspWriter();
146         jspWriter.setExpectedData("Foo is: test");
147 
148         MockPageContext pageContext = new MockPageContext();
149         pageContext.setJspWriter(jspWriter);
150         pageContext.setRequest(request);
151 
152         tag.setPageContext(pageContext);
153 
154         try {
155             tag.doStartTag();
156         } catch (JspException e) {
157             e.printStackTrace();
158             fail();
159         }
160 
161         request.verify();
162         jspWriter.verify();
163         pageContext.verify();
164     }
165 
166 
167     public void testWithAltSyntax1() throws Exception {
168         // setups
169         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}});
170 
171         Foo foo = new Foo();
172         foo.setTitle("tm_jee");
173         stack.push(foo);
174 
175         MockJspWriter jspWriter = new MockJspWriter();
176         jspWriter.setExpectedData("Foo is: tm_jee");
177 
178         MockPageContext pageContext = new MockPageContext();
179         pageContext.setJspWriter(jspWriter);
180         pageContext.setRequest(request);
181 
182         // test
183         {PropertyTag tag = new PropertyTag();
184         tag.setPageContext(pageContext);
185         tag.setValue("%{toString()}");
186         tag.doStartTag();
187         tag.doEndTag();}
188 
189         // verify test
190         request.verify();
191         jspWriter.verify();
192         pageContext.verify();
193     }
194 
195     public void testWithAltSyntax2() throws Exception {
196         // setups
197         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}});
198 
199         Foo foo = new Foo();
200         foo.setTitle("tm_jee");
201         stack.push(foo);
202 
203         MockJspWriter jspWriter = new MockJspWriter();
204         jspWriter.setExpectedData("Foo is: tm_jee");
205 
206         MockPageContext pageContext = new MockPageContext();
207         pageContext.setJspWriter(jspWriter);
208         pageContext.setRequest(request);
209 
210         // test
211         {PropertyTag tag = new PropertyTag();
212         tag.setPageContext(pageContext);
213         tag.setValue("toString()");
214         tag.doStartTag();
215         tag.doEndTag();}
216 
217         // verify test
218         request.verify();
219         jspWriter.verify();
220         pageContext.verify();
221     }
222 
223     public void testWithoutAltSyntax1() throws Exception {
224         //      setups
225         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "false");}});
226 
227         Foo foo = new Foo();
228         foo.setTitle("tm_jee");
229         stack.push(foo);
230 
231         MockJspWriter jspWriter = new MockJspWriter();
232         jspWriter.setExpectedData("Foo is: tm_jee");
233 
234         MockPageContext pageContext = new MockPageContext();
235         pageContext.setJspWriter(jspWriter);
236         pageContext.setRequest(request);
237 
238         // test
239         {PropertyTag tag = new PropertyTag();
240         tag.setPageContext(pageContext);
241         tag.setValue("toString()");
242         tag.doStartTag();
243         tag.doEndTag();}
244 
245         // verify test
246         request.verify();
247         jspWriter.verify();
248         pageContext.verify();
249     }
250 
251 
252     public void testWithoutAltSyntax2() throws Exception {
253         //      setups
254         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "false");}});
255 
256         Foo foo = new Foo();
257         foo.setTitle("tm_jee");
258         stack.push(foo);
259 
260         MockJspWriter jspWriter = new MockJspWriter();
261 
262         MockPageContext pageContext = new MockPageContext();
263         pageContext.setJspWriter(jspWriter);
264         pageContext.setRequest(request);
265 
266         // test
267         {PropertyTag tag = new PropertyTag();
268         tag.setPageContext(pageContext);
269         tag.setValue("%{toString()}");
270         tag.doStartTag();
271         tag.doEndTag();}
272 
273         // verify test
274         request.verify();
275         jspWriter.verify();
276         pageContext.verify();
277     }
278 
279 
280     protected void setUp() throws Exception {
281         super.setUp();
282         ActionContext.getContext().setValueStack(stack);
283         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
284     }
285 
286 
287     public class Foo {
288         private String title;
289 
290         public void setTitle(String title) {
291             this.title = title;
292         }
293 
294         public String getTitle() {
295             return title;
296         }
297 
298         public String toString() {
299             return "Foo is: " + title;
300         }
301     }
302 }