View Javadoc

1   package org.apache.velocity.tools.test.blackbox;
2   
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  import java.lang.reflect.Proxy;
23  import java.lang.reflect.InvocationHandler;
24  import java.util.HashMap;
25  import java.util.Map;
26  import org.apache.velocity.tools.view.tools.LinkTool;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  /**
34   * <p>LinkTool tests.</p>
35   *
36   * @author Christopher Schultz
37   * @version $Id$
38   */
39  public class LinkToolTests
40  {
41      private LinkTool newLinkTool(InvocationHandler handler)
42      {
43          Object proxy
44              = Proxy.newProxyInstance(this.getClass().getClassLoader(),
45                                       new Class[] { HttpServletRequest.class,
46                                                     HttpServletResponse.class },
47                                       handler);
48  
49          HttpServletRequest request = (HttpServletRequest)proxy;
50          HttpServletResponse response = (HttpServletResponse)proxy;
51  
52          LinkTool link = new LinkTool();
53          link.setRequest(request);
54          link.setResponse(response);
55          return link;
56      }
57  
58      private LinkTool newLinkTool(Map params)
59      {
60          return newLinkTool(new ServletAdaptor("/test","/link.vm", params));
61      }
62  
63      private LinkTool newLinkTool(String key, Object value)
64      {
65          HashMap params = new HashMap();
66          params.put(key, value);
67          return newLinkTool(params);
68      }
69  
70      public @Test void testAddAllParameters()
71      {
72          LinkTool link = newLinkTool("a", "b");
73          Assert.assertEquals("/test", link.getContextPath());
74  
75          String url = link.setRelative("/target")
76              .addQueryData("foo", "bar")
77              .addQueryData("bar", "baz")
78              .addAllParameters()
79              .toString();
80  
81          Assert.assertEquals("/test/target?foo=bar&amp;bar=baz&amp;a=b", url);
82      }
83  
84      public @Test void testAddMultiValueParameters()
85      {
86          LinkTool link = newLinkTool("a", new String[] { "a", "b", "c" });
87  
88          String url = link.setRelative("/target")
89              .addQueryData("foo", "bar")
90              .addQueryData("bar", "baz")
91              .addAllParameters()
92              .toString();
93  
94          Assert.assertEquals("/test/target?foo=bar&amp;bar=baz&amp;a=a&amp;a=b&amp;a=c", url);
95      }
96  
97      public @Test void testAddIgnoreParameters()
98      {
99          HashMap params = new HashMap();
100         params.put("a", "b");
101         params.put("b", "c");
102         LinkTool link = newLinkTool(params);
103 
104         String url = link.setRelative("/target")
105             .addQueryData("foo", "bar")
106             .addQueryData("bar", "baz")
107             .addIgnore("b")
108             .addAllParameters()
109             .toString();
110 
111         Assert.assertEquals("/test/target?foo=bar&amp;bar=baz&amp;a=b", url);
112     }
113 
114     public @Test void testAddAllParametersFirst()
115     {
116         LinkTool link = newLinkTool("a", "b");
117 
118         String url = link.setRelative("/target")
119             .addAllParameters()
120             .addQueryData("foo", "bar")
121             .addQueryData("bar", "baz")
122             .toString();
123 
124         Assert.assertEquals("/test/target?a=b&amp;foo=bar&amp;bar=baz", url);
125     }
126 
127     public @Test void testAddAdditionalValue()
128     {
129         LinkTool link = newLinkTool("a", "b");
130         link.setAutoIgnoreParameters(false);
131 
132         String url = link.setRelative("/target")
133             .addQueryData("a", "c")
134             .addAllParameters()
135             .toString();
136 
137         Assert.assertEquals("/test/target?a=c&amp;a=b", url);
138     }
139 
140     public @Test void testAddAdditionalValueAfter()
141     {
142         LinkTool link = newLinkTool("a", "b");
143         link.setAutoIgnoreParameters(false);
144 
145         String url = link.setRelative("/target")
146             .addAllParameters()
147             .addQueryData("a", "c")
148             .toString();
149 
150         Assert.assertEquals("/test/target?a=b&amp;a=c", url);
151     }
152 
153     public @Test void testAutoIgnore()
154     {
155         LinkTool link = newLinkTool("a", "b");
156 
157         String url = link.setRelative("/target")
158             .addQueryData("a", "c")
159             .toString();
160 
161         Assert.assertEquals("/test/target?a=c", url);
162     }
163 
164     public @Test void testAutoIgnoreMultiple()
165     {
166         LinkTool link = newLinkTool("a", new String[] { "a", "b", "c" });
167 
168         String url = link.setRelative("/target")
169             .addQueryData("a", "d")
170             .addAllParameters()
171             .toString();
172 
173         Assert.assertEquals("/test/target?a=d", url);
174     }
175 
176     public @Test void testNoIgnoreMultiple_WrongOrder()
177     {
178         LinkTool link = newLinkTool("a", new String[] { "a", "b", "c" });
179 
180         String url = link.setRelative("/target")
181             .addAllParameters()
182             .addQueryData("a", "d")
183             .toString();
184 
185         Assert.assertEquals("/test/target?a=a&amp;a=b&amp;a=c&amp;a=d", url);
186     }
187 
188 }