View Javadoc

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