View Javadoc

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