View Javadoc

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