1 19 20 33 package org.htmlparser.tests.utilTests; 34 import org.htmlparser.tags.LinkTag; 35 import org.htmlparser.tests.ParserTestCase; 36 import org.htmlparser.util.LinkProcessor; 37 import org.htmlparser.util.ParserException; 38 39 public class HTMLLinkProcessorTest extends ParserTestCase 40 { 41 private LinkProcessor lp; 42 43 public HTMLLinkProcessorTest(String name) 44 { 45 super(name); 46 } 47 48 protected void setUp() 49 { 50 lp = new LinkProcessor(); 51 } 52 53 public void testIsURL() 54 { 55 String resourceLoc1 = "http://someurl.com"; 56 String resourceLoc2 = "myfilehttp.dat"; 57 assertTrue( 58 resourceLoc1 + " should be a url", 59 LinkProcessor.isURL(resourceLoc1)); 60 assertTrue( 61 resourceLoc2 + " should not be a url", 62 !LinkProcessor.isURL(resourceLoc2)); 63 String resourceLoc3 = 64 "file://localhost/D:/java/jdk1.3/docs/api/overview-summary.html"; 65 assertTrue( 66 resourceLoc3 + " should be a url", 67 LinkProcessor.isURL(resourceLoc3)); 68 69 } 70 71 public void testFixSpaces() 72 { 73 String url = 74 "http://htmlparser.sourceforge.net/test/This is a Test Page.html"; 75 String fixedURL = LinkProcessor.fixSpaces(url); 76 int index = fixedURL.indexOf(" "); 77 assertEquals( 78 "Expected", 79 "http://htmlparser.sourceforge.net/test/This%20is%20a%20Test%20Page.html", 80 fixedURL); 81 } 82 83 87 public void testLinkWithNoSlashes() throws Exception 88 { 89 createParser("<A HREF=\".foo.txt\">Foo</A>", "http://www.oygevalt.com"); 90 parser.registerScanners(); 91 parseAndAssertNodeCount(1); 92 assertTrue(node[0] instanceof LinkTag); 93 LinkTag linkTag = (LinkTag) node[0]; 94 assertStringEquals( 95 "link", 96 "http://www.oygevalt.com/foo.txt", 97 linkTag.getLink()); 98 assertEquals("link", "Foo", linkTag.getLinkText()); 99 } 100 106 static final String baseURI = "http://a/b/c/d;p?q"; 108 110 134 public void test1() throws ParserException 135 { 136 assertEquals( 137 "test1 failed", 138 "https:h", 139 (new LinkProcessor()).extract("https:h", baseURI)); 140 } 141 public void test2() throws ParserException 142 { 143 assertEquals( 144 "test2 failed", 145 "http://a/b/c/g", 146 (new LinkProcessor()).extract("g", baseURI)); 147 } 148 public void test3() throws ParserException 149 { 150 assertEquals( 151 "test3 failed", 152 "http://a/b/c/g", 153 (new LinkProcessor()).extract("./g", baseURI)); 154 } 155 public void test4() throws ParserException 156 { 157 assertEquals( 158 "test4 failed", 159 "http://a/b/c/g/", 160 (new LinkProcessor()).extract("g/", baseURI)); 161 } 162 public void test5() throws ParserException 163 { 164 assertEquals( 165 "test5 failed", 166 "http://a/g", 167 (new LinkProcessor()).extract("/g", baseURI)); 168 } 169 public void test6() throws ParserException 170 { 171 assertEquals( 172 "test6 failed", 173 "http://g", 174 (new LinkProcessor()).extract("//g", baseURI)); 175 } 176 public void test7() throws ParserException 177 { 178 assertEquals( 179 "test7 failed", 180 "http://a/b/c/?y", 181 (new LinkProcessor()).extract("?y", baseURI)); 182 } 183 public void test8() throws ParserException 184 { 185 assertEquals( 186 "test8 failed", 187 "http://a/b/c/g?y", 188 (new LinkProcessor()).extract("g?y", baseURI)); 189 } 190 public void test9() throws ParserException 191 { 192 assertEquals( 193 "test9 failed", 194 "https:h", 195 (new LinkProcessor()).extract("https:h", baseURI)); 196 } 197 public void test10() throws ParserException 198 { 199 assertEquals( 200 "test10 failed", 201 "https:h", 202 (new LinkProcessor()).extract("https:h", baseURI)); 203 } 204 public void test11() throws ParserException 206 { 207 assertEquals( 208 "test11 failed", 209 "http://a/b/c/g#s", 210 (new LinkProcessor()).extract("g#s", baseURI)); 211 } 212 public void test12() throws ParserException 213 { 214 assertEquals( 215 "test12 failed", 216 "http://a/b/c/g?y#s", 217 (new LinkProcessor()).extract("g?y#s", baseURI)); 218 } 219 public void test13() throws ParserException 220 { 221 assertEquals( 222 "test13 failed", 223 "http://a/b/c/;x", 224 (new LinkProcessor()).extract(";x", baseURI)); 225 } 226 public void test14() throws ParserException 227 { 228 assertEquals( 229 "test14 failed", 230 "http://a/b/c/g;x", 231 (new LinkProcessor()).extract("g;x", baseURI)); 232 } 233 public void test15() throws ParserException 234 { 235 assertEquals( 236 "test15 failed", 237 "http://a/b/c/g;x?y#s", 238 (new LinkProcessor()).extract("g;x?y#s", baseURI)); 239 } 240 public void test16() throws ParserException 241 { 242 assertEquals( 243 "test16 failed", 244 "http://a/b/c/", 245 (new LinkProcessor()).extract(".", baseURI)); 246 } 247 public void test17() throws ParserException 248 { 249 assertEquals( 250 "test17 failed", 251 "http://a/b/c/", 252 (new LinkProcessor()).extract("./", baseURI)); 253 } 254 public void test18() throws ParserException 255 { 256 assertEquals( 257 "test18 failed", 258 "http://a/b/", 259 (new LinkProcessor()).extract("..", baseURI)); 260 } 261 public void test19() throws ParserException 262 { 263 assertEquals( 264 "test19 failed", 265 "http://a/b/", 266 (new LinkProcessor()).extract("../", baseURI)); 267 } 268 public void test20() throws ParserException 269 { 270 assertEquals( 271 "test20 failed", 272 "http://a/b/g", 273 (new LinkProcessor()).extract("../g", baseURI)); 274 } 275 public void test21() throws ParserException 276 { 277 assertEquals( 278 "test21 failed", 279 "http://a/", 280 (new LinkProcessor()).extract("../..", baseURI)); 281 } 282 public void test22() throws ParserException 283 { 284 assertEquals( 285 "test22 failed", 286 "http://a/g", 287 (new LinkProcessor()).extract("../../g", baseURI)); 288 } 289 290 354 public void test23() throws ParserException 363 { 364 assertEquals( 365 "test23 failed", 366 "http://a/g", 367 (new LinkProcessor()).extract("../../../g", baseURI)); 368 } 369 public void test24() throws ParserException 370 { 371 assertEquals( 372 "test24 failed", 373 "http://a/g", 374 (new LinkProcessor()).extract("../../../../g", baseURI)); 375 } 376 public void test25() throws ParserException 377 { 378 assertEquals( 379 "test25 failed", 380 "http://a/./g", 381 (new LinkProcessor()).extract("/./g", baseURI)); 382 } 383 public void test26() throws ParserException 384 { 385 assertEquals( 386 "test26 failed", 387 "http://a/../g", 388 (new LinkProcessor()).extract("/../g", baseURI)); 389 } 390 public void test27() throws ParserException 391 { 392 assertEquals( 393 "test27 failed", 394 "http://a/b/c/g.", 395 (new LinkProcessor()).extract("g.", baseURI)); 396 } 397 public void test28() throws ParserException 398 { 399 assertEquals( 400 "test28 failed", 401 "http://a/b/c/.g", 402 (new LinkProcessor()).extract(".g", baseURI)); 403 } 404 public void test29() throws ParserException 405 { 406 assertEquals( 407 "test29 failed", 408 "http://a/b/c/g..", 409 (new LinkProcessor()).extract("g..", baseURI)); 410 } 411 public void test30() throws ParserException 412 { 413 assertEquals( 414 "test30 failed", 415 "http://a/b/c/..g", 416 (new LinkProcessor()).extract("..g", baseURI)); 417 } 418 public void test31() throws ParserException 419 { 420 assertEquals( 421 "test31 failed", 422 "http://a/b/g", 423 (new LinkProcessor()).extract("./../g", baseURI)); 424 } 425 public void test32() throws ParserException 426 { 427 assertEquals( 428 "test32 failed", 429 "http://a/b/c/g/", 430 (new LinkProcessor()).extract("./g/.", baseURI)); 431 } 432 public void test33() throws ParserException 433 { 434 assertEquals( 435 "test33 failed", 436 "http://a/b/c/g/h", 437 (new LinkProcessor()).extract("g/./h", baseURI)); 438 } 439 public void test34() throws ParserException 440 { 441 assertEquals( 442 "test34 failed", 443 "http://a/b/c/h", 444 (new LinkProcessor()).extract("g/../h", baseURI)); 445 } 446 public void test35() throws ParserException 447 { 448 assertEquals( 449 "test35 failed", 450 "http://a/b/c/g;x=1/y", 451 (new LinkProcessor()).extract("g;x=1/./y", baseURI)); 452 } 453 public void test36() throws ParserException 454 { 455 assertEquals( 456 "test36 failed", 457 "http://a/b/c/y", 458 (new LinkProcessor()).extract("g;x=1/../y", baseURI)); 459 } 460 public void test37() throws ParserException 461 { 462 assertEquals( 463 "test37 failed", 464 "http://a/b/c/g?y/./x", 465 (new LinkProcessor()).extract("g?y/./x", baseURI)); 466 } 467 public void test38() throws ParserException 468 { 469 assertEquals( 470 "test38 failed", 471 "http://a/b/c/g?y/../x", 472 (new LinkProcessor()).extract("g?y/../x", baseURI)); 473 } 474 public void test39() throws ParserException 475 { 476 assertEquals( 477 "test39 failed", 478 "http://a/b/c/g#s/./x", 479 (new LinkProcessor()).extract("g#s/./x", baseURI)); 480 } 481 public void test40() throws ParserException 482 { 483 assertEquals( 484 "test40 failed", 485 "http://a/b/c/g#s/../x", 486 (new LinkProcessor()).extract("g#s/../x", baseURI)); 487 } 488 public void test41() throws ParserException 493 { 494 assertEquals( 495 "test41 failed", 496 "http://a/b/c/g", 497 (new LinkProcessor()).extract("http:g", baseURI)); 498 } 499 } 500 | Popular Tags |