1 package org.displaytag.util; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import junit.framework.TestCase; 7 8 import org.displaytag.test.URLAssert; 9 10 11 16 public class DefaultHrefTest extends TestCase 17 { 18 19 22 public String getName() 23 { 24 return getClass().getName() + "." + super.getName(); 25 } 26 27 30 public final void testSimpleHref() 31 { 32 String url = "http://www.displaytag.org/displaytag"; 33 Href href = new DefaultHref(url); 34 String newUrl = href.toString(); 35 URLAssert.assertEquals(url, newUrl); 36 } 37 38 41 public final void testHrefWithParameters() 42 { 43 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2"; 44 Href href = new DefaultHref(url); 45 String newUrl = href.toString(); 46 URLAssert.assertEquals(url, newUrl); 47 } 48 49 52 public final void testHrefParamWithoutValue() 53 { 54 String url = "http://www.displaytag.org/displaytag/index.jsp?param1"; 55 Href href = new DefaultHref(url); 56 String newUrl = href.toString(); 57 URLAssert.assertEquals(url, newUrl); 58 } 59 60 63 public final void testHrefMultipleParamWithoutValue() 64 { 65 String url = "http://www.displaytag.org/displaytag/index.jsp?param1¶m2=2"; 66 Href href = new DefaultHref(url); 67 String newUrl = href.toString(); 68 URLAssert.assertEquals(url, newUrl); 69 } 70 71 74 public final void testHrefWithMultipleParameters() 75 { 76 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2¶m2=3¶m2=4¶m2="; 77 Href href = new DefaultHref(url); 78 String newUrl = href.toString(); 79 URLAssert.assertEquals(url, newUrl); 80 } 81 82 85 public final void testHrefWithAnchor() 86 { 87 String url = "http://www.displaytag.org/displaytag/index.jsp#thisanchor"; 88 Href href = new DefaultHref(url); 89 String newUrl = href.toString(); 90 URLAssert.assertEquals(url, newUrl); 91 } 92 93 96 public final void testHrefWithEmptyAnchor() 97 { 98 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2#"; 99 Href href = new DefaultHref(url); 100 String newUrl = href.toString(); 101 URLAssert.assertEquals(url, newUrl); 102 } 103 104 107 public final void testHrefWithAnchorAndParameters() 108 { 109 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2#thisanchor"; 110 Href href = new DefaultHref(url); 111 String newUrl = href.toString(); 112 URLAssert.assertEquals(url, newUrl); 113 } 114 115 118 public final void testHrefWithQuotes() 119 { 120 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=aquote'test"; 121 Href href = new DefaultHref(url); 122 String newUrl = href.toString(); 123 URLAssert.assertEquals(url, newUrl); 124 } 125 126 129 public final void testHrefCopy() 130 { 131 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2#thisanchor"; 132 Href href = new DefaultHref(url); 133 Href copy = (Href) href.clone(); 134 URLAssert.assertEquals(copy.toString(), href.toString()); 135 } 136 137 140 public final void testClone() 141 { 142 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=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 154 public final void testEquals() 155 { 156 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2#thisanchor"; 157 Href href = new DefaultHref(url); 158 Href href2 = new DefaultHref(url); 159 assertEquals(href, href2); 160 } 161 162 165 public final void testAddParameter() 166 { 167 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2#thisanchor"; 168 Href href = new DefaultHref(url); 169 href.addParameter("param3", "value3"); 170 href.addParameter("param4", 4); 171 String newUrl = href.toString(); 172 URLAssert.assertEquals( 173 "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2¶m3=value3¶m4=4#thisanchor", 174 newUrl); 175 } 176 177 180 public final void testSetParameterMap() 181 { 182 String url = "http://www.displaytag.org/displaytag/index.jsp#thisanchor"; 183 Href href = new DefaultHref(url); 184 185 Map parameterMap = new HashMap (); 186 parameterMap.put("new1", "new1value"); 187 parameterMap.put("new2", "new2value"); 188 parameterMap.put("new3", null); 189 href.setParameterMap(parameterMap); 190 191 String 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 200 public final void testAddParameterMap() 201 { 202 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1#thisanchor"; 203 Href href = new DefaultHref(url); 204 205 Map parameterMap = new HashMap (); 206 parameterMap.put("new1", "new1value"); 207 parameterMap.put("new2", "new2value"); 208 parameterMap.put("new3", null); 209 href.addParameterMap(parameterMap); 210 211 String 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 221 public final void testAddParameterMapMultiValue() 222 { 223 String url = "http://www.displaytag.org/displaytag/index.jsp"; 224 Href href = new DefaultHref(url); 225 226 Map parameterMap = new HashMap (); 227 parameterMap.put("param1", new String []{"à", "<"}); 228 href.addParameterMap(parameterMap); 229 230 String newUrl = href.toString(); 231 assertEquals("http://www.displaytag.org/displaytag/index.jsp?param1=à&param1=<", newUrl); 232 233 } 234 235 238 public final void testAddParameterMapOverridingParameters() 239 { 240 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=original#thisanchor"; 241 Href href = new DefaultHref(url); 242 243 Map parameterMap = new HashMap (); 244 parameterMap.put("param1", "original"); 245 parameterMap.put("new1", "new1value"); 246 href.addParameterMap(parameterMap); 247 248 String 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 258 public final void testGetBaseUrl() 259 { 260 String url = "http://www.displaytag.org/displaytag/index.jsp?param1=1¶m2=2#thisanchor"; 261 Href href = new DefaultHref(url); 262 assertEquals(href.getBaseUrl(), "http://www.displaytag.org/displaytag/index.jsp"); 263 } 264 265 268 public final void testComplex() 269 { 270 String 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 newUrl = href.toString(); 275 URLAssert.assertEquals(url, newUrl); 276 } 277 278 281 public final void testNoBaseUrl() 282 { 283 String url = "?param1=1¶m2=2#thisanchor"; 284 Href href = new DefaultHref(url); 285 assertEquals(href.getBaseUrl(), ""); 286 URLAssert.assertEquals(url, href.toString()); 287 } 288 289 } 290 | Popular Tags |