View Javadoc

1   package org.apache.velocity.tools.generic;
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 org.junit.*;
23  import static org.junit.Assert.*;
24  import java.net.URI;
25  import java.util.HashMap;
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  import org.apache.velocity.runtime.log.Log;
29  import org.apache.velocity.tools.generic.ValueParser;
30  
31  /**
32   * <p>Tests for generic version of LinkTool</p>
33   *
34   * @author Nathan Bubna
35   * @since VelocityTools 2.0
36   * @version $Id$
37   */
38  public class LinkToolTests {
39  
40      public static final Map DEFAULT_PROPS = new HashMap();
41      static
42      {
43          // don't lock configure() for testing
44          DEFAULT_PROPS.put(LinkTool.SAFE_MODE_KEY, false);
45          DEFAULT_PROPS.put(LinkTool.LOCK_CONFIG_KEY, false);
46      }
47  
48      /**
49       * Returns a new instance configured with the 
50       * default testing properties.
51       */
52      public LinkTool newInstance()
53      {
54          LinkTool link = new LinkTool();
55          link.configure(DEFAULT_PROPS);
56          return link;
57      }
58  
59      public LinkTool newInstance(String uri)
60      {
61          return newInstance(LinkTool.URI_KEY, uri);
62      }
63  
64      /**
65       * Returns a new instance configured with the 
66       * default testing properties and the specified
67       * non-default property.
68       */
69      public LinkTool newInstance(String key, Object value)
70      {
71          LinkTool link = new LinkTool();
72          Map props = new HashMap(DEFAULT_PROPS);
73          props.put(key, value);
74          link.configure(props);
75          return link;
76      }
77  
78      public @Test void ctorLinkTool() throws Exception
79      {
80          try
81          {
82              new LinkTool();
83          }
84          catch (Exception e)
85          {
86              fail("Constructor 'LinkTool()' failed due to: " + e);
87          }
88      }
89  
90      public @Test void methodConfigure_ValueParser() throws Exception
91      {
92          LinkTool link = newInstance("mailto:nbubna@apache.org");
93          assertEquals("mailto", link.getScheme());
94          assertEquals("mailto:nbubna@apache.org", link.toString());
95          assertTrue(link.getUri().isOpaque());
96          assertTrue(link.isAbsolute());
97      }
98  
99      public @Test void methodDuplicate() throws Exception
100     {
101         LinkTool link = newInstance("http://apache.org/foo.html");
102         LinkTool result = link.duplicate();
103         assertFalse(link == result);
104         assertSame(link.getScheme(), result.getScheme());
105         assertSame(link.getUser(), result.getUser());
106         assertSame(link.getHost(), result.getHost());
107         assertSame(link.getPath(), result.getPath());
108         assertSame(link.query, result.query);
109         assertSame(link.getAnchor(), result.getAnchor());
110         assertSame(link.getSelf(), result.getSelf());
111     }
112 
113     public @Test void methodDuplicate_boolean() throws Exception
114     {
115         LinkTool link = newInstance("http://apache.org/foo.html?foo=bar");
116         LinkTool result = link.duplicate(true);
117         assertFalse(link == result);
118         assertSame(link.getPath(), result.getPath());
119         assertFalse(link.query == result.query);
120         assertEquals(link.getQuery(), result.getQuery());
121         assertSame(link.getSelf(), result.getSelf());
122         assertSame(result.query, result.duplicate(false).query);
123     }
124 
125     public @Test void methodEncode_Object() throws Exception
126     {
127         LinkTool link = newInstance();
128         assertEquals("this+has+spaces", link.encode("this has spaces"));
129         assertEquals("%40%2F+", link.encode("@/ "));
130     }
131 
132     public @Test void methodDecode_Object() throws Exception
133     {
134         LinkTool link = newInstance();
135         assertEquals("this has spaces", link.decode("this+has+spaces"));
136         assertEquals("@/ ", link.decode("%40%2F+"));
137     }
138 
139     public @Test void methodSetScheme_Object() throws Exception
140     {
141         LinkTool link = newInstance(LinkTool.SCHEME_KEY, LinkTool.DEFAULT_SCHEME);
142         assertEquals(LinkTool.DEFAULT_SCHEME, link.getScheme());
143         link.setScheme(null);
144         assertEquals(null, link.getScheme());
145         link.setScheme("foo:");
146         assertEquals("foo", link.getScheme());
147     }
148 
149     public @Test void methodScheme_Object() throws Exception
150     {
151         LinkTool link = newInstance();
152         LinkTool result = link.scheme(null);
153         assertEquals(null, result.getScheme());
154         link = newInstance("https://apache.org");
155         assertEquals(null, link.getPath());
156         assertEquals("https://apache.org", link.toString());
157         assertEquals(LinkTool.SECURE_SCHEME, link.getScheme());
158         assertTrue(link.isSecure());
159         result = link.scheme(LinkTool.DEFAULT_SCHEME);
160         assertEquals("http://apache.org", result.toString());
161         assertFalse(result.isSecure());
162     }
163 
164     public @Test void methodGetScheme() throws Exception
165     {
166         LinkTool link = newInstance();
167         assertEquals(null, link.getScheme());
168         assertEquals("mailto", link.scheme("mailto").getScheme());
169     }
170 
171     public @Test void methodSecure() throws Exception
172     {
173         LinkTool link = newInstance();
174         assertFalse(link.isSecure());
175         LinkTool result = link.secure();
176         assertTrue(result.isSecure());
177     }
178 
179     public @Test void methodInsecure() throws Exception
180     {
181         LinkTool link = newInstance("https://apache.org");
182         assertTrue(link.isSecure());
183         LinkTool result = link.insecure();
184         assertFalse(result.isSecure());
185     }
186 
187     public @Test void methodIsAbsolute() throws Exception
188     {
189         LinkTool link = newInstance();
190         assertFalse(link.isAbsolute());
191         LinkTool result = link.absolute("http://apache.org");
192         assertTrue(result.isAbsolute());
193     }
194 
195     public @Test void methodSetUserInfo_Object() throws Exception
196     {
197         LinkTool link = newInstance(LinkTool.USER_KEY, "nbubna");
198         assertEquals("nbubna", link.getUser());
199         link.setUserInfo(null);
200         assertEquals(null, link.getUser());
201         // no encoding should happen here
202         link.setUserInfo("@#$ /@!");
203         assertEquals("@#$ /@!", link.getUser());
204     }
205 
206     public @Test void methodGetUser() throws Exception
207     {
208         LinkTool link = newInstance();
209         assertEquals(null, link.getUser());
210         link = newInstance("http://nbubna@apache.org");
211         assertEquals("nbubna", link.getUser());
212     }
213 
214     public @Test void methodUser_Object() throws Exception
215     {
216         LinkTool link = newInstance("http://nbubna@apache.org");
217         assertEquals(null, link.user(null).getUser());
218         assertEquals("nbubna", link.user("nbubna").getUser());
219         assertEquals("@#$ /!", link.user("@#$ /!").getUser());
220         assertEquals("http://%40%23$%20%2F!@apache.org", link.user("@#$ /!").toString());
221     }
222 
223     public @Test void methodGetHost() throws Exception
224     {
225         LinkTool link = newInstance("http://apache.org");
226         assertEquals("apache.org", link.getHost());
227         link.setFromURI("http://velocity.apache.org/tools/devel/");
228         assertEquals("velocity.apache.org", link.getHost());
229     }
230 
231     public @Test void methodHost_Object() throws Exception
232     {
233         LinkTool link = newInstance();
234         assertEquals("apache.org", link.host("apache.org").getHost());
235         link = newInstance("https://nbubna@www.apache.org");
236         assertEquals("https://nbubna@people.apache.org", link.host("people.apache.org").toString());
237     }
238 
239     public @Test void methodSetHost_Object() throws Exception
240     {
241         LinkTool link = newInstance();
242         link.setHost("foo.com");
243         assertEquals("foo.com", link.getHost());
244     }
245 
246     public @Test void methodGetPort() throws Exception
247     {
248         LinkTool link = newInstance();
249         assertNull(link.getPort());
250         link = newInstance(LinkTool.PORT_KEY, 42);
251         assertEquals(42, link.getPort());
252     }
253 
254     public @Test void methodPort_Object() throws Exception
255     {
256         LinkTool link = newInstance();
257         assertNull(link.port(null).getPort());
258         assertNull(link.port(":asd").getPort());
259         assertEquals(1, link.port(1).getPort());
260         assertEquals(42, link.port("42").getPort());
261     }
262 
263     public @Test void methodSetPort_Object() throws Exception
264     {
265         LinkTool link = newInstance();
266         link.setPort(42);
267         assertEquals(42, link.getPort());
268     }
269 
270     public @Test void methodGetPath() throws Exception
271     {
272         LinkTool link = newInstance();
273         assertNull(link.getPath());
274         link = newInstance("http://velocity.apache.org/tools/devel");
275         assertEquals("/tools/devel", link.getPath());
276     }
277 
278     public @Test void methodSetPath_Object() throws Exception
279     {
280         LinkTool link = newInstance();
281         assertNull(link.getPath());
282         link.setPath("foo");
283         assertEquals("/foo", link.getPath());
284         link.setPath("/foo");
285         assertEquals("/foo", link.getPath());
286         link.setPath("/foo/");
287         assertEquals("/foo/", link.getPath());
288         link.setPath("/foo/");
289         assertEquals("/foo/", link.getPath());
290     }
291 
292     public @Test void methodPath_Object() throws Exception
293     {
294         LinkTool link = newInstance();
295         assertNull(link.getPath());
296         assertEquals("/bar", link.path("bar").getPath());
297         assertEquals("/bar", link.path("/bar").getPath());
298         assertEquals("/bar/", link.path("bar/").getPath());
299         assertEquals("/bar/", link.path("/bar/").getPath());
300         link = newInstance("http://foo.com/this/that.vm");
301         assertEquals("http://foo.com/this/that.vm", link.toString());
302         assertEquals("http://foo.com/bar.vm", link.path("bar.vm").toString());
303     }
304 
305     public @Test void methodCombinePath_StringString() throws Exception
306     {
307         LinkTool link = newInstance();
308         String none = null;
309         String empty = "";
310         String test = "test";
311         String starts = "/this";
312         String ends = "that/";
313         String both = "/these/";
314         assertNull(link.combinePath(none, none));
315         assertSame(empty, link.combinePath(none, empty));
316         assertSame(empty, link.combinePath(empty, none));
317         assertEquals("/test", link.combinePath(empty, test));
318         assertEquals("test/", link.combinePath(test, empty));
319         assertEquals("/this/this", link.combinePath(starts, starts));
320         assertEquals("that/that/", link.combinePath(ends, ends));
321         assertEquals("/this/that/", link.combinePath(starts, ends));
322         assertEquals("that/this", link.combinePath(ends, starts));
323         assertEquals("/these/these/", link.combinePath(both, both));
324     }
325 
326     public @Test void methodAppendPath_Object() throws Exception
327     {
328         LinkTool link = newInstance(LinkTool.PATH_KEY, "/foo");
329         assertEquals("/foo", link.getPath());
330         link.appendPath("bar");
331         assertEquals("/foo/bar", link.getPath());
332         link.appendPath("/bar");
333         assertEquals("/foo/bar/bar", link.getPath());
334         link.setPath("/foo/");
335         link.appendPath("bar/");
336         assertEquals("/foo/bar/", link.getPath());
337         link.appendPath("/bar");
338         assertEquals("/foo/bar/bar", link.getPath());
339     }
340 
341     public @Test void methodAppend_Object() throws Exception
342     {
343         LinkTool link = newInstance(LinkTool.PATH_KEY, "/foo");
344         assertEquals("/foo", link.append(null).getPath());
345         link.setPath(null);
346         assertNull(link.getPath());
347         link = link.append("bar");
348         assertEquals("/bar", link.getPath());
349         assertEquals("/bar/foo", link.append("foo").getPath());
350     }
351 
352     public @Test void methodGetDirectory() throws Exception
353     {
354         LinkTool link = newInstance("http://foo.com/ctx/request.vm?this=that#anc");
355         assertEquals("/ctx/", link.getDirectory());
356         link = newInstance("http://foo.com");
357         assertNull(link.getDirectory());
358         link = newInstance("http://foo.com/bar");
359         assertEquals("/", link.getDirectory());
360         link = newInstance("http://foo.com/bar/foo/bar/foo");
361         assertEquals("/bar/foo/bar/", link.getDirectory());
362     }
363 
364     public @Test void methodGetFile() throws Exception
365     {
366         LinkTool link = newInstance("http://foo.com/ctx/request.vm?this=that#anc");
367         assertEquals("request.vm", link.getFile());
368         link = newInstance("http://foo.com/foo/bar/request.vm?this=that#anc");
369         assertEquals("request.vm", link.getFile());
370         link = newInstance("http://foo.com/bar/");
371         assertEquals("", link.getFile());
372     }
373 
374     public @Test void methodGetRoot() throws Exception
375     {
376         LinkTool link = newInstance("http://foo.com/ctx/request.vm?this=that#anc");
377         assertEquals("http://foo.com", link.getRoot());
378         link.setHost("apache.org");
379         assertEquals("https://apache.org", link.secure().getRoot());
380     }
381 
382     public @Test void methodDirectory() throws Exception
383     {
384         LinkTool link = newInstance("http://foo.com/ctx/request.vm?this=that#anc");
385         assertEquals("http://foo.com/ctx/", link.directory().toString());
386         link = newInstance("http://foo.com");
387         assertEquals("http://foo.com", link.directory().toString());
388     }
389 
390     public @Test void methodRoot() throws Exception
391     {
392         LinkTool link = newInstance("http://foo.com/ctx/request.vm?this=that#anc");
393         assertEquals("http://foo.com", link.root().toString());
394         link = newInstance("http://foo.com");
395         assertEquals("http://foo.com", link.root().toString());
396         link = newInstance("dev@velocity.apache.org");
397         assertNull(link.root());
398     }
399 
400     public @Test void methodSetForceRelative_boolean() throws Exception
401     {
402         LinkTool link = newInstance("http://apache.org/bar");
403         assertEquals("http://apache.org/bar", link.toString());
404         link.setForceRelative(true);
405         assertEquals("/bar", link.toString());
406     }
407 
408     public @Test void methodRelative_Object() throws Exception
409     {
410         LinkTool link = newInstance("/ctx/request.vm?this=that#anc");
411         assertEquals("/ctx/request.vm", link.getPath());
412         assertEquals("/ctx/", link.getDirectory());
413         assertEquals("this=that", link.getQuery());
414         assertEquals("anc", link.getAnchor());
415         assertEquals("/ctx/request.vm?this=that#anc", link.toString());
416         assertEquals("/ctx/?this=that#anc", link.relative(null).toString());
417         assertEquals("/ctx/other.vm?this=that#anc", link.relative("other.vm").toString());
418         link = newInstance("http://foo.com/bar/");
419         assertEquals("/bar/woogie.vm", link.relative("woogie.vm").toString());
420         link = newInstance("/bar/");
421         assertEquals("/bar/foo/woogie.vm", link.relative("foo/woogie.vm").toString());
422         assertEquals("/bar/yo", link.relative("yo").toString());
423     }
424 
425     public @Test void methodIsRelative() throws Exception
426     {
427         LinkTool link = newInstance("/ctx/request.vm?this=that#anc");
428         assertTrue(link.isRelative());
429         link = newInstance("http://foo.com/bar.vm?q=woogie");
430         assertFalse(link.isRelative());
431         assertTrue(link.relative().isRelative());
432         link = newInstance("http://apache.org").relative("foo.vm");
433         assertTrue(link.isRelative());
434     }
435 
436     public @Test void methodAbsolute_Object() throws Exception
437     {
438         LinkTool link = newInstance();
439         LinkTool result = link.absolute(null);
440 
441         result = link.absolute("http://apache.org");
442         assertEquals(link.uri("http://apache.org"), result);
443         assertTrue(result.isAbsolute());
444         assertEquals("http://apache.org", result.toString());
445         assertEquals(LinkTool.DEFAULT_SCHEME, result.getScheme());
446         assertEquals("apache.org", result.getHost());
447 
448         assertFalse(link.isAbsolute());
449         result = link.absolute("/test/foo.vm");
450         assertTrue(result.isAbsolute());
451         assertEquals("/test/foo.vm", result.getPath());
452         assertEquals(null, result.getHost());
453         assertEquals("http:/test/foo.vm", result.toString());
454         result = result.host("apache.org");
455         assertEquals("http://apache.org/test/foo.vm", result.toString());
456         result = result.absolute("bar.vm");
457         assertEquals("http://apache.org/test/bar.vm", result.toString());
458         result = result.absolute("/woogie.vm");
459         assertEquals("http://apache.org/woogie.vm", result.toString());
460     }
461 
462     public @Test void methodGetBaseRef() throws Exception
463     {
464         LinkTool link = newInstance("http://foo.com/ctx/request.vm?this=that#anc");
465         assertEquals("http://foo.com/ctx/request.vm", link.getBaseRef());
466         assertEquals(null, newInstance().getBaseRef());
467     }
468 
469     public @Test void methodGetUri() throws Exception
470     {
471         LinkTool link = newInstance(LinkTool.SAFE_MODE_KEY, true);
472         link.setFromURI("http://velocity.apache.org");
473         assertNull(link.getUri());
474         link = newInstance();
475         assertNull(link.getUri());
476         link = link.secure().user("nbubna").host("people.apache.org");
477         assertNotNull(link.getUri());
478         assertEquals("nbubna", link.getUri().getUserInfo());
479     }
480 
481     public @Test void methodSetFromURI_Object() throws Exception
482     {
483         LinkTool link = newInstance();
484         assertNull(link.toString());
485         link.setFromURI("*%&$^%#$*&^!");
486         assertNull(link.toString());
487         link.setFromURI("http://velocity.apache.org");
488         assertNotNull(link.toString());
489         assertEquals("velocity.apache.org", link.getHost());
490     }
491 
492     public @Test void methodToURI_Object() throws Exception
493     {
494         LinkTool link = newInstance();
495         URI uri = new URI("http://apache.org");
496         assertSame(uri, link.toURI(uri));
497         assertNull(link.toURI("*%&$^%#$*&^!"));
498         assertNotNull(link.toURI("http://velocity.apache.org"));
499         assertNotNull(link.toURI(new Object() {
500             public String toString() {
501                 return "http://google.com";
502             }
503         }));
504     }
505 
506     public @Test void methodCreateURI() throws Exception
507     {
508         LinkTool link = newInstance();
509         assertNull(link.createURI());
510         link.setFromURI("http://velocity.apache.org");
511         assertNotNull(link.createURI());
512         link.setPort("foo");
513         assertNull(link.createURI());
514         link.setPort(null);
515         assertTrue(link.setFromURI("mailto:nbubna@apache.org"));
516         assertTrue(link.isOpaque());
517         assertNotNull(link.createURI());
518         assertTrue(link.createURI().isOpaque());
519         assertEquals(link.createURI(), link.createURI());
520         assertFalse(link.createURI() == link.createURI());
521     }
522 
523     public @Test void methodUri_Object() throws Exception
524     {
525         LinkTool link = newInstance();
526         assertEquals(null, link.uri(null).toString());
527         assertEquals("http://apache.org?a=b#c", link.uri("http://apache.org?a=b#c").toString());
528         link.setFromURI("https://nbubna@people.apache.org");
529         assertEquals("people.apache.org", link.getHost());
530         assertEquals("https://nbubna@people.apache.org", link.uri(link.createURI()).toString());
531         URI uri = new URI("mailto:nbubna@apache.org");
532         assertEquals("mailto:nbubna@apache.org", link.uri(uri).toString());
533     }
534 
535     public @Test void methodSetAppendParams_boolean() throws Exception
536     {
537         LinkTool link = newInstance("/bar?a=b");
538         LinkTool result = link.param("a","c");
539         assertEquals("a=b&amp;a=c", result.getQuery());
540         link.setAppendParams(false);
541         result = link.param("a", "d");
542         assertEquals("a=d", result.getQuery());
543     }
544 
545     public @Test void methodSetQuery_Object() throws Exception
546     {
547         LinkTool link = newInstance("/bar?a=b");
548         assertNotNull(link.getQuery());
549         assertEquals("a=b", link.getQuery());
550         link.setQuery("c=d&e=f");
551         assertEquals("c=d&amp;e=f", link.getQuery());
552         link.setXHTML(false);
553         link.setQuery("x=1&amp;y=2");
554         assertEquals("x=1&y=2", link.getQuery());
555         link.setQuery(null);
556         assertEquals(null, link.getQuery());
557     }
558 
559     public @Test void methodNormalizeQuery_String() throws Exception
560     {
561         LinkTool link = new LinkTool();
562         assertEquals("a=b", link.normalizeQuery("a=b"));
563         assertEquals("a=b&amp;b=c", link.normalizeQuery("a=b&b=c"));
564         assertEquals("a=b&amp;b=c", link.normalizeQuery("a=b&amp;b=c"));
565         assertEquals("a=b&amp;b=c&amp;t=f", link.normalizeQuery("a=b&amp;b=c&t=f"));
566     }
567 
568     public @Test void methodGetQuery() throws Exception
569     {
570         LinkTool link = newInstance();
571         assertEquals(null, link.getQuery());
572         assertEquals("this=that", link.query("this=that").getQuery());
573     }
574 
575     public @Test void methodQuery_Object() throws Exception
576     {
577         LinkTool link = newInstance("https://gmail.com");
578         assertEquals("https://gmail.com", link.query(null).toString());
579         assertEquals("https://gmail.com?v=2", link.query("v=2").toString());
580         link = newInstance("http://go.com?foo=bar");
581         assertEquals("foo=wog", link.query("foo=wog").getQuery());
582         assertEquals("http://go.com", link.query(null).toString());
583     }
584 
585     // this method also tests setXHTML
586     public @Test void methodCombineQuery_StringString() throws Exception
587     {
588         LinkTool link = newInstance();
589         String none = null;
590         String empty = "";
591         String test = "test=1";
592         String test2 = "a=b";
593         assertSame(none, link.combineQuery(none, none));
594         assertSame(none, link.combineQuery(none, empty));
595         assertSame(test, link.combineQuery(test, none));
596         assertEquals("test=1", link.combineQuery(test, empty));
597         assertEquals("test=1&amp;a=b", link.combineQuery(test, test2));
598         link.setXHTML(false);
599         assertEquals("a=b&test=1", link.combineQuery(test2, test));
600     }
601 
602     public @Test void methodAppendQuery_Object() throws Exception
603     {
604         LinkTool link = newInstance("/foo?bar=woogie");
605         link.appendQuery("x=1");
606         assertEquals("bar=woogie&amp;x=1", link.getQuery());
607         link.appendQuery("y=2");
608         assertEquals("bar=woogie&amp;x=1&amp;y=2", link.getQuery());
609         link.setQuery(null);
610         assertEquals(null, link.getQuery());
611         link.appendQuery("z=3");
612         assertEquals("z=3", link.getQuery());
613     }
614 
615     public @Test void methodToQuery_Map() throws Exception
616     {
617         LinkTool link = new LinkTool();
618         Map test = new LinkedHashMap();
619         test.put("a", "b");
620         assertEquals("a=b", link.toQuery(test));
621         test.put("b", "c");
622         assertEquals("a=b&amp;b=c", link.toQuery(test));
623         Boolean[] array = new Boolean[] { Boolean.TRUE, Boolean.FALSE };
624         test.put("b", array);
625         assertEquals("a=b&amp;b=true&amp;b=false", link.toQuery(test));
626     }
627 
628     public @Test void methodToQuery_ObjectObject() throws Exception
629     {
630         LinkTool link = newInstance();
631         assertEquals("null=", link.toQuery(null, null));
632         assertEquals("a+b=c", link.toQuery("a b", "c"));
633         assertEquals("x=1", link.toQuery('x', 1));
634         assertEquals("true=false", link.toQuery(true, false));
635         assertEquals("path=%2Ffoo+bar%2Fnew", link.toQuery("path", "/foo bar/new"));
636         // try all URI reserved chars
637         assertEquals("x=%2C%3B%3A%24%26%2B%3D%3F%2F%5B%5D%40", link.toQuery('x', ",;:$&+=?/[]@"));
638     }
639 
640     public @Test void methodSetParam_ObjectObjectboolean() throws Exception
641     {
642         LinkTool link = newInstance();
643         assertNull(link.query);
644         link.setParam("a","b", true);
645         assertNotNull(link.query);
646         assertEquals("a=b", link.getQuery());
647         link.setParam("a", "c", true);
648         assertEquals("a=b&amp;a=c", link.getQuery());
649         link.setParam("a", "foo", false);
650         assertEquals("a=foo", link.getQuery());
651     }
652 
653     public @Test void methodSetParams_Objectboolean() throws Exception
654     {
655         LinkTool link = newInstance();
656         assertNull(link.query);
657         link.setParams("a=b", true);
658         assertNotNull(link.query);
659         assertEquals("a=b", link.getQuery());
660         link.setParams("a=c&a=d", true);
661         assertEquals("a=b&amp;a=c&amp;a=d", link.getQuery());
662         Map test = new LinkedHashMap();
663         test.put("a", "foo");
664         link.setParams(test, false);
665         assertEquals("a=foo", link.getQuery());
666     }
667 
668     public @Test void methodParams_Object() throws Exception
669     {
670         LinkTool link = newInstance();
671         Map test = new LinkedHashMap();
672         test.put("a", "foo");
673         assertEquals("a=foo", link.params(test).getQuery());
674         assertEquals("a=b&amp;b=c", link.params("a=b&b=c").getQuery());
675         link = newInstance("/foo?q=a&a=q");
676         assertEquals("/foo", link.params(false).toString());
677         assertEquals("/foo?q=a&amp;a=q", link.params(true).toString());
678     }
679 
680     public @Test void methodParam_ObjectObject() throws Exception
681     {
682         LinkTool link = newInstance();
683         assertEquals("null=", link.param(null, null).getQuery());
684         assertEquals("x=1", link.param("x",1).getQuery());
685         assertEquals("x=1&amp;y=2", link.param("x",1).param("y",2).getQuery());
686         link = newInstance("/hee/haa.vm?a=b");
687         assertEquals("/hee/haa.vm?a=b&amp;b=true", link.param('b', true).toString());
688     }
689 
690     public @Test void methodAppend_ObjectObject() throws Exception
691     {
692         LinkTool link = newInstance();
693         link.setAppendParams(false); //using append(key,val) should override
694         assertEquals("x=1", link.append("x",1).getQuery());
695         assertEquals("x=1&amp;x=2", link.append("x",1).append("x",2).getQuery());
696     }
697 
698     public @Test void methodSet_ObjectObject() throws Exception
699     {
700         LinkTool link = newInstance();
701         link.setAppendParams(true); //using set(key,val) should override
702         assertEquals("x=1", link.set("x",1).getQuery());
703         assertEquals("x=2", link.set("x",1).set("x",2).getQuery());
704     }
705 
706     public @Test void methodRemove_Object() throws Exception
707     {
708         LinkTool link = newInstance("/foo?q=bar");
709         assertEquals("q=bar", link.getQuery());
710         assertEquals("", link.remove("q").getQuery());
711         assertEquals("q=bar", link.set("x",1).remove("x").getQuery());
712     }
713 
714     public @Test void methodRemoveParam_Object() throws Exception
715     {
716         LinkTool link = newInstance();
717         assertNull(link.removeParam("a"));
718         link.setParam("a","b",true);
719         assertEquals("a=b", link.getQuery());
720         assertEquals("b", link.removeParam("a"));
721         assertNull(link.removeParam("a"));
722     }
723 
724     public @Test void methodHandleParamsBoolean_boolean() throws Exception
725     {
726         LinkTool link = newInstance();
727         assertNull(link.query);
728         link.setParam("a","b",true);
729         Map q = link.query;
730         link.handleParamsBoolean(true);
731         assertSame(q, link.query);
732         assertEquals("a=b", link.getQuery());
733         link.handleParamsBoolean(false);
734         assertNull(link.query);
735     }
736 
737     public @Test void methodParams_Map() throws Exception
738     {
739         LinkTool link = newInstance("http://go.com");
740         Map params = new LinkedHashMap();
741         params.put("this", "that");
742         params.put('x', 1);
743         params.put(true, false);
744         assertEquals("http://go.com?this=that&amp;x=1&amp;true=false", link.params(params).toString());
745         assertEquals("http://go.com", link.params(null).toString());
746         assertEquals("http://go.com", link.params(new HashMap()).toString());
747     }
748 
749     public @Test void methodParseQuery_String() throws Exception
750     {
751         LinkTool link = newInstance();
752         Map result = link.parseQuery("a=b&x=1");
753         assertEquals("b", result.get("a"));
754         assertEquals("1", result.get("x"));
755         link.setXHTML(false);
756         result = link.parseQuery("true=false&amp;false=true&black=white");
757         assertEquals("false", result.get("true"));
758         assertEquals("true", result.get("false"));
759         assertEquals("white", result.get("black"));
760     }
761 
762     public @Test void methodGetParams() throws Exception
763     {
764         LinkTool link = newInstance("/foo?a=b&amp;x=true");
765         Map result = link.getParams();
766         assertEquals("b", result.get("a"));
767         assertEquals("true", result.get("x"));
768         Map newresult = link.param('y',false).getParams();
769         assertFalse(result.equals(newresult));
770         assertEquals("b", newresult.get("a"));
771         assertEquals(Boolean.FALSE, newresult.get("y"));
772     }
773 
774     public @Test void methodSetFragment_Object() throws Exception
775     {
776         LinkTool link = newInstance();
777         link.setFragment("foo");
778         assertEquals("#foo", link.toString());
779         link.setFragment(null);
780         assertEquals(null, link.toString());
781         link = newInstance("/foo#bar");
782         link.setFragment("woo gie");
783         assertEquals("/foo#woo%20gie", link.toString());
784     }
785 
786     public @Test void methodGetAnchor() throws Exception
787     {
788         LinkTool link = newInstance();
789         assertEquals(null, link.getAnchor());
790         link.setFragment("foo");
791         assertEquals("foo", link.getAnchor());
792         link = newInstance("http://go.com#espn");
793         assertEquals("espn", link.getAnchor());
794         link = newInstance(LinkTool.FRAGMENT_KEY, "foo");
795         assertEquals("foo", link.getAnchor());
796     }
797 
798     public @Test void methodAnchor_Object() throws Exception
799     {
800         LinkTool link = newInstance();
801         // here are possible string values to test:
802         String none = null;
803         String empty = "";
804         String space = "a b";
805         String test = "test";
806         assertEquals(null, link.anchor(none).getAnchor());
807         assertEquals(null, link.anchor(empty).getAnchor());
808         assertEquals(test, link.anchor(test).getAnchor());
809         assertEquals("a b", link.anchor(space).getAnchor());
810         link = newInstance("http://go.com#foo");
811         assertEquals("http://go.com#true", link.anchor(true).toString());
812         assertEquals("http://go.com#a%20b", link.anchor(space).toString());
813     }
814 
815     public @Test void methodGetSelf() throws Exception
816     {
817         LinkTool link = newInstance();
818         assertSame(link, link.getSelf());
819         assertSame(link, link.uri("http://go.com").getSelf());
820         assertSame(link, link.path("foo").param(1,true).anchor('a').getSelf());
821     }
822 
823     public @Test void methodToString() throws Exception
824     {
825         LinkTool link = newInstance();
826         assertEquals(null, link.toString());
827         assertEquals(null, link.secure().toString());
828         assertEquals("http://go.com", link.host("go.com").toString());
829         assertEquals(null, link.port(42).toString());
830         assertEquals("/foo", link.path("foo").toString());
831         assertEquals("?a=1", link.param('a',1).toString());
832         assertEquals("#42", link.anchor(42).toString());
833     }
834 
835     public @Test void methodNoDoubleEncode() throws Exception
836     {
837         LinkTool link = newInstance().relative("/foo");
838         assertEquals("/foo", link.toString());
839         link = link.param("q","a:b c");
840         assertEquals("/foo?q=a%3Ab+c", link.toString());
841         link = link.anchor("a(b, c)");
842         assertEquals("/foo?q=a%3Ab+c#a(b,%20c)", link.toString());
843         link = link.param("evil","%25%24%").anchor(null);
844         assertEquals("/foo?q=a%3Ab+c&amp;evil=%2525%2524%25", link.toString());
845     }
846 
847 }
848