View Javadoc

1   /*
2    * $Id: URLTagTest.java 439747 2006-09-03 09:22:46Z 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 java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.jsp.JspWriter;
24  
25  import org.apache.struts2.components.URL;
26  
27  /***
28   * Unit test for {@link URLTag}.
29   *
30   */
31  public class URLTagTest extends AbstractUITagTest {
32  
33      private URLTag tag;
34      
35      
36      /***
37       * To test priority of parameter passed in to url component though 
38       * various way 
39       *  - current request url
40       *  - tag's value attribute
41       *  - tag's nested param tag
42       * 
43  	 * id1
44  	 * ===
45  	 * - found in current request url
46  	 * - found in tag's value attribute
47  	 * - found in tag's param tag
48  	 * CONCLUSION: tag's param tag takes precedence (paramId1)
49  	 * 
50  	 * id2
51  	 * ===
52  	 * - found in current request url
53  	 * - found in tag's value attribute
54  	 * CONCLUSION: tag's value attribute take precedence (tagId2)
55  	 * 
56  	 * urlParam1
57  	 * =========
58  	 * - found in current request url
59  	 * CONCLUSION: param in current request url will be used (urlValue1)
60  	 * 
61  	 * urlParam2
62  	 * =========
63  	 * - found in current request url
64  	 * CONCLUSION: param in current request url will be used. (urlValue2)
65  	 * 
66  	 * tagId
67  	 * =====
68  	 * - found in tag's value attribute
69  	 * CONCLUSION: param in tag's value attribute wil; be used. (tagValue)
70  	 * 
71  	 * param1
72  	 * ======
73  	 * - found in nested param tag
74  	 * CONCLUSION: param in nested param tag will be used. (param1value)
75  	 * 
76  	 * param2
77  	 * ======
78  	 * - found in nested param tag
79  	 * CONCLUSION: param in nested param tag will be used. (param2value)
80  	 */
81      public void testParametersPriority() throws Exception {
82      	request.setQueryString("id1=urlId1&id2=urlId2&urlParam1=urlValue1&urlParam2=urlValue2");
83      	
84      	tag.setValue("testAction.action?id1=tagId1&id2=tagId2&tagId=tagValue");
85      	
86      	ParamTag param1 = new ParamTag();
87      	param1.setPageContext(pageContext);
88      	param1.setName("param1");
89      	param1.setValue("%{'param1value'}");
90      	
91      	ParamTag param2 = new ParamTag();
92      	param2.setPageContext(pageContext);
93      	param2.setName("param2");
94      	param2.setValue("%{'param2value'}");
95      	
96      	ParamTag param3 = new ParamTag();
97      	param3.setPageContext(pageContext);
98      	param3.setName("id1");
99      	param3.setValue("%{'paramId1'}");
100     	
101     	
102     	tag.doStartTag();
103     	param1.doStartTag();
104     	param1.doEndTag();
105     	param2.doStartTag();
106     	param2.doEndTag();
107     	param3.doStartTag();
108     	param3.doEndTag();
109     	
110     	URL url = (URL) tag.getComponent();
111     	Map parameters = url.getParameters();
112     	
113     	
114     	assertNotNull(parameters);
115     	assertEquals(parameters.size(), 7);
116     	assertEquals(parameters.get("id1"), "paramId1");
117     	assertEquals(parameters.get("id2"), "tagId2");
118     	assertEquals(parameters.get("urlParam1"), "urlValue1");
119     	assertEquals(parameters.get("urlParam2"), "urlValue2");
120     	assertEquals(parameters.get("tagId"), "tagValue");
121     	assertEquals(parameters.get("param1"), "param1value");
122     	assertEquals(parameters.get("param2"), "param2value");
123     }
124     
125     
126     /***
127      * To test priority of parameter passed in to url component though 
128      * various way, with includeParams="NONE"
129      *  - current request url
130      *  - tag's value attribute
131      *  - tag's nested param tag
132      *  
133      *  In this case only parameters from the tag itself is taken into account.
134      *  Those from request will not count, only those in tag's value attribute 
135      *  and nested param tag.
136      *  
137      * @throws Exception
138      */
139     public void testParametersPriorityWithIncludeParamsAsNONE() throws Exception {
140     	request.setQueryString("id1=urlId1&id2=urlId2&urlParam1=urlValue1&urlParam2=urlValue2");
141     	
142     	tag.setValue("testAction.action?id1=tagId1&id2=tagId2&tagId=tagValue");
143     	tag.setIncludeParams("NONE");
144     	
145     	ParamTag param1 = new ParamTag();
146     	param1.setPageContext(pageContext);
147     	param1.setName("param1");
148     	param1.setValue("%{'param1value'}");
149     	
150     	ParamTag param2 = new ParamTag();
151     	param2.setPageContext(pageContext);
152     	param2.setName("param2");
153     	param2.setValue("%{'param2value'}");
154     	
155     	ParamTag param3 = new ParamTag();
156     	param3.setPageContext(pageContext);
157     	param3.setName("id1");
158     	param3.setValue("%{'paramId1'}");
159     	
160     	
161     	tag.doStartTag();
162     	param1.doStartTag();
163     	param1.doEndTag();
164     	param2.doStartTag();
165     	param2.doEndTag();
166     	param3.doStartTag();
167     	param3.doEndTag();
168     	
169     	URL url = (URL) tag.getComponent();
170     	Map parameters = url.getParameters();
171     	
172     	assertEquals(parameters.size(), 5);
173     	assertEquals(parameters.get("id1"), "paramId1");
174     	assertEquals(parameters.get("id2"), "tagId2");
175     	assertEquals(parameters.get("tagId"), "tagValue");
176     	assertEquals(parameters.get("param1"), "param1value");
177     	assertEquals(parameters.get("param2"), "param2value");
178     }
179 
180     public void testIncludeParamsDefaultToGET() throws Exception {
181     	request.setQueryString("one=oneVal&two=twoVal&three=threeVal");
182     	
183     	// request parameter map should not have any effect, as includeParams 
184     	// default to GET, which get its param from request.getQueryString()
185     	Map tmp = new HashMap();
186     	tmp.put("one", "aaa");
187     	tmp.put("two", "bbb");
188     	tmp.put("three", "ccc");
189     	request.setParameterMap(tmp);
190     	
191     	tag.setValue("TestAction.acton");
192     	
193     	tag.doStartTag();
194     	
195     	URL url = (URL) tag.getComponent();
196     	Map parameters = url.getParameters();
197     	
198     	tag.doEndTag();
199     	
200     	assertEquals(parameters.get("one"), "oneVal");
201     	assertEquals(parameters.get("two"), "twoVal");
202     	assertEquals(parameters.get("three"), "threeVal");
203     }
204     
205     public void testActionURL() throws Exception {
206         tag.setValue("TestAction.action");
207 
208         tag.doStartTag();
209         tag.doEndTag();
210         assertEquals("TestAction.action", writer.toString());
211     }
212 
213     public void testAddParameters() throws Exception {
214         request.setAttribute("struts.request_uri", "/Test.action");
215 
216         request.setAttribute("struts.request_uri", "/TestAction.action");
217         request.setQueryString("param0=value0");
218 
219         tag.doStartTag();
220         tag.component.addParameter("param1", "value1");
221         tag.component.addParameter("param2", "value2");
222         tag.doEndTag();
223         assertEquals("/TestAction.action?param2=value2&param0=value0&param1=value1", writer.toString());
224     }
225 
226     public void testEvaluateValue() throws Exception {
227         Foo foo = new Foo();
228         foo.setTitle("test");
229         stack.push(foo);
230         tag.setValue("%{title}");
231 
232         tag.doStartTag();
233         tag.doEndTag();
234         assertEquals("test", writer.toString());
235     }
236 
237     public void testHttps() throws Exception {
238         request.setScheme("https");
239         request.setServerName("localhost");
240         request.setServerPort(443);
241 
242         tag.setValue("list-members.action");
243 
244         tag.doStartTag();
245         tag.doEndTag();
246         assertEquals("list-members.action", writer.toString());
247     }
248 
249     public void testAnchor() throws Exception {
250         request.setScheme("https");
251         request.setServerName("localhost");
252         request.setServerPort(443);
253 
254         tag.setValue("list-members.action");
255         tag.setAnchor("test");
256 
257         tag.doStartTag();
258         tag.doEndTag();
259         assertEquals("list-members.action#test", writer.toString());
260     }
261     
262     public void testParamPrecedence() throws Exception {
263     	request.setRequestURI("/context/someAction.action");
264     	request.setQueryString("id=22&name=John");
265     	
266     	URLTag urlTag = new URLTag();
267     	urlTag.setPageContext(pageContext);
268     	urlTag.setIncludeParams("get");
269     	urlTag.setEncode("%{false}");
270     	
271     	ParamTag paramTag = new ParamTag();
272     	paramTag.setPageContext(pageContext);
273     	paramTag.setName("id");
274     	paramTag.setValue("%{'33'}");
275     	
276     	urlTag.doStartTag();
277     	paramTag.doStartTag();
278     	paramTag.doEndTag();
279     	urlTag.doEndTag();
280     	
281     	assertEquals(writer.getBuffer().toString(), "/context/someAction.action?name=John&id=33");
282     }
283 
284     public void testParamPrecedenceWithAnchor() throws Exception {
285     	request.setRequestURI("/context/someAction.action");
286     	request.setQueryString("id=22&name=John");
287 
288     	URLTag urlTag = new URLTag();
289     	urlTag.setPageContext(pageContext);
290     	urlTag.setIncludeParams("get");
291     	urlTag.setEncode("%{false}");
292         urlTag.setAnchor("testAnchor");
293 
294         ParamTag paramTag = new ParamTag();
295     	paramTag.setPageContext(pageContext);
296     	paramTag.setName("id");
297     	paramTag.setValue("%{'33'}");
298 
299     	urlTag.doStartTag();
300     	paramTag.doStartTag();
301     	paramTag.doEndTag();
302     	urlTag.doEndTag();
303 
304     	assertEquals(writer.getBuffer().toString(), "/context/someAction.action?name=John&id=33#testAnchor");
305     }
306 
307     public void testPutId() throws Exception {
308         tag.setValue("/public/about");
309         assertEquals(null, stack.findString("myId")); // nothing in stack
310         tag.setId("myId");
311         tag.doStartTag();
312         tag.doEndTag();
313         assertEquals("", writer.toString());
314         assertEquals("/public/about", stack.findString("myId")); // is in stack now
315     }
316     
317     public void testUsingValueOnly() throws Exception {
318         tag.setValue("/public/about/team.jsp");
319         tag.doStartTag();
320         tag.doEndTag();
321         assertEquals("/public/about/team.jsp", writer.toString());
322     }
323 
324     public void testRequestURIActionIncludeNone() throws Exception {
325         request.setRequestURI("/public/about");
326         request.setQueryString("section=team&company=acme inc");
327 
328         tag.setAction("team");
329         tag.setIncludeParams("none");
330         tag.doStartTag();
331         tag.doEndTag();
332 
333         assertEquals("/team.action", writer.toString());
334     }
335 
336     public void testRequestURIActionIncludeGet() throws Exception {
337         request.setRequestURI("/public/about");
338         request.setQueryString("section=team&company=acme inc");
339 
340         tag.setAction("team");
341         tag.setIncludeParams("get");
342         tag.doStartTag();
343         tag.doEndTag();
344 
345         assertEquals("/team.action?section=team&company=acme+inc", writer.toString());
346     }
347 
348     public void testRequestURINoActionIncludeNone() throws Exception {
349         request.setRequestURI("/public/about");
350         request.setQueryString("section=team&company=acme inc");
351 
352         tag.setAction(null);
353         tag.setIncludeParams("none");
354         tag.doStartTag();
355         tag.doEndTag();
356 
357         assertEquals("/public/about", writer.toString());
358     }
359 
360     public void testNoActionIncludeGet() throws Exception {
361         request.setRequestURI("/public/about");
362         request.setQueryString("section=team&company=acme inc");
363 
364         tag.setAction(null);
365         tag.setIncludeParams("get");
366         tag.doStartTag();
367         tag.doEndTag();
368 
369         assertEquals("/public/about?section=team&company=acme+inc", writer.toString());
370     }
371 
372     public void testRequestURIActionIncludeAll() throws Exception {
373         request.setRequestURI("/public/about");
374         request.setQueryString("section=team&company=acme inc");
375 
376         tag.setAction("team");
377         tag.setIncludeParams("all");
378 
379         tag.doStartTag();
380 
381         // include nested param tag
382         ParamTag paramTag = new ParamTag();
383         paramTag.setPageContext(pageContext);
384         paramTag.setName("year");
385         paramTag.setValue("2006");
386         paramTag.doStartTag();
387         paramTag.doEndTag();
388 
389         tag.doEndTag();
390 
391         assertEquals("/team.action?section=team&year=2006&company=acme+inc", writer.toString());
392     }
393 
394     public void testRequestURINoActionIncludeAll() throws Exception {
395         request.setRequestURI("/public/about");
396         request.setQueryString("section=team&company=acme inc");
397 
398         tag.setAction(null);
399         tag.setIncludeParams("all");
400 
401         tag.doStartTag();
402 
403         // include nested param tag
404         ParamTag paramTag = new ParamTag();
405         paramTag.setPageContext(pageContext);
406         paramTag.setName("year");
407         paramTag.setValue("2006");
408         paramTag.doStartTag();
409         paramTag.doEndTag();
410 
411         tag.doEndTag();
412 
413         assertEquals("/public/about?section=team&year=2006&company=acme+inc", writer.toString());
414     }
415 
416     public void testUnknownIncludeParam() throws Exception {
417         request.setRequestURI("/public/about");
418         request.setQueryString("section=team");
419 
420         tag.setIncludeParams("unknown"); // will log at WARN level
421         tag.doStartTag();
422         tag.doEndTag();
423         assertEquals("/public/about", writer.toString()); // should not add any request parameters
424     }
425 
426     public void testRequestURIWithAnchor() throws Exception {
427         request.setRequestURI("/public/about");
428         request.setQueryString("company=acme inc#canada");
429 
430         tag.setAction("company");
431         tag.setIncludeParams("get");
432         tag.doStartTag();
433         tag.doEndTag();
434 
435         assertEquals("/company.action?company=acme+inc", writer.toString()); // will always chop anchor if using requestURI
436     }
437 
438     public void testIncludeContext() throws Exception {
439         request.setupGetContext("/myapp");
440 
441         tag.setIncludeContext("true");
442         tag.setAction("company");
443         tag.doStartTag();
444         tag.doEndTag();
445 
446         assertEquals("/myapp/company.action", writer.toString());
447     }
448 
449     protected void setUp() throws Exception {
450         super.setUp();
451 
452         request.setScheme("http");
453         request.setServerName("localhost");
454         request.setServerPort(80);
455 
456         tag = new URLTag();
457         tag.setPageContext(pageContext);
458         JspWriter jspWriter = new StrutsMockJspWriter(writer);
459         pageContext.setJspWriter(jspWriter);
460     }
461 
462     public class Foo {
463         private String title;
464 
465         public void setTitle(String title) {
466             this.title = title;
467         }
468 
469         public String getTitle() {
470             return title;
471         }
472 
473         public String toString() {
474             return "Foo is: " + title;
475         }
476     }
477 }