View Javadoc

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