View Javadoc

1   /*
2    * $Id: PropertyTagTest.java 762875 2009-04-07 17:56:30Z musachy $
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 testEscapeJavaScript() throws Exception {
197         // setups
198         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}});
199 
200         Foo foo = new Foo();
201         foo.setTitle("\t\b\n\f\r\"\'///");
202         stack.push(foo);
203 
204         MockJspWriter jspWriter = new MockJspWriter();
205         jspWriter.setExpectedData("Foo is: //t//b//n//f//r//\"//\'///////");
206 
207         MockPageContext pageContext = new MockPageContext();
208         pageContext.setJspWriter(jspWriter);
209         pageContext.setRequest(request);
210 
211         // test
212         {PropertyTag tag = new PropertyTag();
213         tag.setEscape(false);
214         tag.setEscapeJavaScript(true);    
215         tag.setPageContext(pageContext);
216         tag.setValue("%{toString()}");
217         tag.doStartTag();
218         tag.doEndTag();}
219 
220         // verify test
221         request.verify();
222         jspWriter.verify();
223         pageContext.verify();
224     }
225 
226     public void testWithAltSyntax2() throws Exception {
227         // setups
228         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "true");}});
229 
230         Foo foo = new Foo();
231         foo.setTitle("tm_jee");
232         stack.push(foo);
233 
234         MockJspWriter jspWriter = new MockJspWriter();
235         jspWriter.setExpectedData("Foo is: tm_jee");
236 
237         MockPageContext pageContext = new MockPageContext();
238         pageContext.setJspWriter(jspWriter);
239         pageContext.setRequest(request);
240 
241         // test
242         {PropertyTag tag = new PropertyTag();
243         tag.setPageContext(pageContext);
244         tag.setValue("toString()");
245         tag.doStartTag();
246         tag.doEndTag();}
247 
248         // verify test
249         request.verify();
250         jspWriter.verify();
251         pageContext.verify();
252     }
253 
254     public void testWithoutAltSyntax1() throws Exception {
255         //      setups
256         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "false");}});
257 
258         Foo foo = new Foo();
259         foo.setTitle("tm_jee");
260         stack.push(foo);
261 
262         MockJspWriter jspWriter = new MockJspWriter();
263         jspWriter.setExpectedData("Foo is: tm_jee");
264 
265         MockPageContext pageContext = new MockPageContext();
266         pageContext.setJspWriter(jspWriter);
267         pageContext.setRequest(request);
268 
269         // test
270         {PropertyTag tag = new PropertyTag();
271         tag.setPageContext(pageContext);
272         tag.setValue("toString()");
273         tag.doStartTag();
274         tag.doEndTag();}
275 
276         // verify test
277         request.verify();
278         jspWriter.verify();
279         pageContext.verify();
280     }
281 
282 
283     public void testWithoutAltSyntax2() throws Exception {
284         //      setups
285         initDispatcher(new HashMap() {{ put(StrutsConstants.STRUTS_TAG_ALTSYNTAX, "false");}});
286 
287         Foo foo = new Foo();
288         foo.setTitle("tm_jee");
289         stack.push(foo);
290 
291         MockJspWriter jspWriter = new MockJspWriter();
292 
293         MockPageContext pageContext = new MockPageContext();
294         pageContext.setJspWriter(jspWriter);
295         pageContext.setRequest(request);
296 
297         // test
298         {PropertyTag tag = new PropertyTag();
299         tag.setPageContext(pageContext);
300         tag.setValue("%{toString()}");
301         tag.doStartTag();
302         tag.doEndTag();}
303 
304         // verify test
305         request.verify();
306         jspWriter.verify();
307         pageContext.verify();
308     }
309 
310 
311     protected void setUp() throws Exception {
312         super.setUp();
313         stack = ActionContext.getContext().getValueStack();
314         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
315     }
316 
317 
318     public class Foo {
319         private String title;
320 
321         public void setTitle(String title) {
322             this.title = title;
323         }
324 
325         public String getTitle() {
326             return title;
327         }
328 
329         public String toString() {
330             return "Foo is: " + title;
331         }
332     }
333 }