View Javadoc

    1   /*
    2    * $Id: URLTagTest.java 471756 2006-11-06 15:01:43Z husted $
    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?param2=value2&amp;param0=value0&amp;param1=value1", 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(writer.getBuffer().toString(), "/context/someAction.action?name=John&amp;id=33");
    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(writer.getBuffer().toString(), "/context/someAction.action?name=John&amp;id=33#testAnchor");
    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&amp;company=acme+inc", writer.toString());
    349     }
    350
    351     public void testRequestURINoActionIncludeNone() throws Exception {
    352         request.setRequestURI("/public/about");
    353         request.setQueryString("section=team&company=acme inc");
    354
    355         tag.setAction(null);
    356         tag.setIncludeParams("none");
    357         tag.doStartTag();
    358         tag.doEndTag();
    359
    360         assertEquals("/public/about", writer.toString());
    361     }
    362
    363     public void testNoActionIncludeGet() throws Exception {
    364         request.setRequestURI("/public/about");
    365         request.setQueryString("section=team&company=acme inc");
    366
    367         tag.setAction(null);
    368         tag.setIncludeParams("get");
    369         tag.doStartTag();
    370         tag.doEndTag();
    371
    372         assertEquals("/public/about?section=team&amp;company=acme+inc", writer.toString());
    373     }
    374
    375     public void testRequestURIActionIncludeAll() throws Exception {
    376         request.setRequestURI("/public/about");
    377         request.setQueryString("section=team&company=acme inc");
    378
    379         tag.setAction("team");
    380         tag.setIncludeParams("all");
    381
    382         tag.doStartTag();
    383
    384         // include nested param tag
    385         ParamTag paramTag = new ParamTag();
    386         paramTag.setPageContext(pageContext);
    387         paramTag.setName("year");
    388         paramTag.setValue("2006");
    389         paramTag.doStartTag();
    390         paramTag.doEndTag();
    391
    392         tag.doEndTag();
    393
    394         assertEquals("/team.action?section=team&amp;year=2006&amp;company=acme+inc", writer.toString());
    395     }
    396
    397     public void testRequestURINoActionIncludeAll() throws Exception {
    398         request.setRequestURI("/public/about");
    399         request.setQueryString("section=team&company=acme inc");
    400
    401         tag.setAction(null);
    402         tag.setIncludeParams("all");
    403
    404         tag.doStartTag();
    405
    406         // include nested param tag
    407         ParamTag paramTag = new ParamTag();
    408         paramTag.setPageContext(pageContext);
    409         paramTag.setName("year");
    410         paramTag.setValue("2006");
    411         paramTag.doStartTag();
    412         paramTag.doEndTag();
    413
    414         tag.doEndTag();
    415
    416         assertEquals("/public/about?section=team&amp;year=2006&amp;company=acme+inc", writer.toString());
    417     }
    418
    419     public void testUnknownIncludeParam() throws Exception {
    420         request.setRequestURI("/public/about");
    421         request.setQueryString("section=team");
    422
    423         tag.setIncludeParams("unknown"); // will log at WARN level
    424         tag.doStartTag();
    425         tag.doEndTag();
    426         assertEquals("/public/about", writer.toString()); // should not add any request parameters
    427     }
    428
    429     public void testRequestURIWithAnchor() throws Exception {
    430         request.setRequestURI("/public/about");
    431         request.setQueryString("company=acme inc#canada");
    432
    433         tag.setAction("company");
    434         tag.setIncludeParams("get");
    435         tag.doStartTag();
    436         tag.doEndTag();
    437
    438         assertEquals("/company.action?company=acme+inc", writer.toString()); // will always chop anchor if using requestURI
    439     }
    440
    441     public void testIncludeContext() throws Exception {
    442         request.setupGetContext("/myapp");
    443
    444         tag.setIncludeContext("true");
    445         tag.setAction("company");
    446         tag.doStartTag();
    447         tag.doEndTag();
    448
    449         assertEquals("/myapp/company.action", writer.toString());
    450     }
    451
    452     protected void setUp() throws Exception {
    453         super.setUp();
    454
    455         request.setScheme("http");
    456         request.setServerName("localhost");
    457         request.setServerPort(80);
    458
    459         tag = new URLTag();
    460         tag.setPageContext(pageContext);
    461         JspWriter jspWriter = new StrutsMockJspWriter(writer);
    462         pageContext.setJspWriter(jspWriter);
    463     }
    464
    465     public class Foo {
    466         private String title;
    467
    468         public void setTitle(String title) {
    469             this.title = title;
    470         }
    471
    472         public String getTitle() {
    473             return title;
    474         }
    475
    476         public String toString() {
    477             return "Foo is: " + title;
    478         }
    479     }
    480 }