KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > util > DefaultHrefTest


1 package org.displaytag.util;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import org.displaytag.test.URLAssert;
9
10
11 /**
12  * Test case for org.displaytag.util.Href.
13  * @author Fabrizio Giustina
14  * @version $Revision$ ($Author$)
15  */

16 public class DefaultHrefTest extends TestCase
17 {
18
19     /**
20      * @see junit.framework.TestCase#getName()
21      */

22     public String JavaDoc getName()
23     {
24         return getClass().getName() + "." + super.getName();
25     }
26
27     /**
28      * Test a simple URL without parameters.
29      */

30     public final void testSimpleHref()
31     {
32         String JavaDoc url = "http://www.displaytag.org/displaytag";
33         Href href = new DefaultHref(url);
34         String JavaDoc newUrl = href.toString();
35         URLAssert.assertEquals(url, newUrl);
36     }
37
38     /**
39      * Test for URLs containing parameters.
40      */

41     public final void testHrefWithParameters()
42     {
43         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2";
44         Href href = new DefaultHref(url);
45         String JavaDoc newUrl = href.toString();
46         URLAssert.assertEquals(url, newUrl);
47     }
48
49     /**
50      * Test for URLs containing parameters without values.
51      */

52     public final void testHrefParamWithoutValue()
53     {
54         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1";
55         Href href = new DefaultHref(url);
56         String JavaDoc newUrl = href.toString();
57         URLAssert.assertEquals(url, newUrl);
58     }
59
60     /**
61      * Test for URLs containing multiple parameters (some of them without values).
62      */

63     public final void testHrefMultipleParamWithoutValue()
64     {
65         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1&param2=2";
66         Href href = new DefaultHref(url);
67         String JavaDoc newUrl = href.toString();
68         URLAssert.assertEquals(url, newUrl);
69     }
70
71     /**
72      * Test for URLs containing parameters with multiple values.
73      */

74     public final void testHrefWithMultipleParameters()
75     {
76         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2&param2=3&param2=4&param2=";
77         Href href = new DefaultHref(url);
78         String JavaDoc newUrl = href.toString();
79         URLAssert.assertEquals(url, newUrl);
80     }
81
82     /**
83      * Test for urls containing anchors.
84      */

85     public final void testHrefWithAnchor()
86     {
87         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp#thisanchor";
88         Href href = new DefaultHref(url);
89         String JavaDoc newUrl = href.toString();
90         URLAssert.assertEquals(url, newUrl);
91     }
92
93     /**
94      * Test href with empty anchor.
95      */

96     public final void testHrefWithEmptyAnchor()
97     {
98         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2#";
99         Href href = new DefaultHref(url);
100         String JavaDoc newUrl = href.toString();
101         URLAssert.assertEquals(url, newUrl);
102     }
103
104     /**
105      * Test for urls containin anchors and parameters.
106      */

107     public final void testHrefWithAnchorAndParameters()
108     {
109         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2#thisanchor";
110         Href href = new DefaultHref(url);
111         String JavaDoc newUrl = href.toString();
112         URLAssert.assertEquals(url, newUrl);
113     }
114
115     /**
116      * Test for urls containing quotes.
117      */

118     public final void testHrefWithQuotes()
119     {
120         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=aquote'test";
121         Href href = new DefaultHref(url);
122         String JavaDoc newUrl = href.toString();
123         URLAssert.assertEquals(url, newUrl);
124     }
125
126     /**
127      * Test the generation of an Href object from another Href.
128      */

129     public final void testHrefCopy()
130     {
131         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2#thisanchor";
132         Href href = new DefaultHref(url);
133         Href copy = (Href) href.clone();
134         URLAssert.assertEquals(copy.toString(), href.toString());
135     }
136
137     /**
138      * Test the clone() implementation.
139      */

140     public final void testClone()
141     {
142         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2#thisanchor";
143         Href href = new DefaultHref(url);
144         Href clone = (Href) href.clone();
145         assertEquals(href, clone);
146
147         clone.addParameter("onlyinclone", "1");
148         assertFalse(href.equals(clone));
149     }
150
151     /**
152      * Tests the equals() implementation.
153      */

154     public final void testEquals()
155     {
156         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2#thisanchor";
157         Href href = new DefaultHref(url);
158         Href href2 = new DefaultHref(url);
159         assertEquals(href, href2);
160     }
161
162     /**
163      * Test for added parameters.
164      */

165     public final void testAddParameter()
166     {
167         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2#thisanchor";
168         Href href = new DefaultHref(url);
169         href.addParameter("param3", "value3");
170         href.addParameter("param4", 4);
171         String JavaDoc newUrl = href.toString();
172         URLAssert.assertEquals(
173             "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2&param3=value3&param4=4#thisanchor",
174             newUrl);
175     }
176
177     /**
178      * test for setParameterMap().
179      */

180     public final void testSetParameterMap()
181     {
182         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp#thisanchor";
183         Href href = new DefaultHref(url);
184
185         Map JavaDoc parameterMap = new HashMap JavaDoc();
186         parameterMap.put("new1", "new1value");
187         parameterMap.put("new2", "new2value");
188         parameterMap.put("new3", null);
189         href.setParameterMap(parameterMap);
190
191         String JavaDoc newUrl = href.toString();
192         URLAssert.assertEquals(
193             "http://www.displaytag.org/displaytag/index.jsp?new1=new1value&new2=new2value&new3=#thisanchor",
194             newUrl);
195     }
196
197     /**
198      * test for addParameterMap().
199      */

200     public final void testAddParameterMap()
201     {
202         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1#thisanchor";
203         Href href = new DefaultHref(url);
204
205         Map JavaDoc parameterMap = new HashMap JavaDoc();
206         parameterMap.put("new1", "new1value");
207         parameterMap.put("new2", "new2value");
208         parameterMap.put("new3", null);
209         href.addParameterMap(parameterMap);
210
211         String JavaDoc newUrl = href.toString();
212         URLAssert.assertEquals(
213             "http://www.displaytag.org/displaytag/index.jsp?param1=1&new1=new1value&new2=new2value&new3=#thisanchor",
214             newUrl);
215
216     }
217
218     /**
219      * test for addParameterMap().
220      */

221     public final void testAddParameterMapMultiValue()
222     {
223         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp";
224         Href href = new DefaultHref(url);
225
226         Map JavaDoc parameterMap = new HashMap JavaDoc();
227         parameterMap.put("param1", new String JavaDoc[]{"à", "<"});
228         href.addParameterMap(parameterMap);
229
230         String JavaDoc newUrl = href.toString();
231         assertEquals("http://www.displaytag.org/displaytag/index.jsp?param1=&agrave;&amp;param1=&lt;", newUrl);
232
233     }
234
235     /**
236      * test for addParameterMap() with overriding parameters.
237      */

238     public final void testAddParameterMapOverridingParameters()
239     {
240         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=original#thisanchor";
241         Href href = new DefaultHref(url);
242
243         Map JavaDoc parameterMap = new HashMap JavaDoc();
244         parameterMap.put("param1", "original");
245         parameterMap.put("new1", "new1value");
246         href.addParameterMap(parameterMap);
247
248         String JavaDoc newUrl = href.toString();
249         URLAssert.assertEquals(
250             "http://www.displaytag.org/displaytag/index.jsp?param1=original&new1=new1value#thisanchor",
251             newUrl);
252
253     }
254
255     /**
256      * test for base url extraction.
257      */

258     public final void testGetBaseUrl()
259     {
260         String JavaDoc url = "http://www.displaytag.org/displaytag/index.jsp?param1=1&param2=2#thisanchor";
261         Href href = new DefaultHref(url);
262         assertEquals(href.getBaseUrl(), "http://www.displaytag.org/displaytag/index.jsp");
263     }
264
265     /**
266      * Complex test.
267      */

268     public final void testComplex()
269     {
270         String JavaDoc url = "http://www.displaytag.org/EProcurement/do/searchWorkflowAction?initiator=AVINASH&wfid="
271             + "&approvedTDate=&initiatedFDate=&status=default&d-3824-p=2&initiatedTDate=04/28/2004"
272             + "&approvedFDate=&method=search&approver=";
273         Href href = new DefaultHref(url);
274         String JavaDoc newUrl = href.toString();
275         URLAssert.assertEquals(url, newUrl);
276     }
277
278     /**
279      * test for url without base.
280      */

281     public final void testNoBaseUrl()
282     {
283         String JavaDoc url = "?param1=1&param2=2#thisanchor";
284         Href href = new DefaultHref(url);
285         assertEquals(href.getBaseUrl(), "");
286         URLAssert.assertEquals(url, href.toString());
287     }
288
289 }
290
Popular Tags