1 27 package org.htmlparser.tests.tagTests; 28 29 import org.htmlparser.Node; 30 import org.htmlparser.PrototypicalNodeFactory; 31 import org.htmlparser.tags.ImageTag; 32 import org.htmlparser.tags.LinkTag; 33 import org.htmlparser.tags.TableColumn; 34 import org.htmlparser.tags.TableRow; 35 import org.htmlparser.tests.ParserTestCase; 36 import org.htmlparser.util.NodeIterator; 37 import org.htmlparser.util.ParserException; 38 import org.htmlparser.util.ParserUtils; 39 40 public class ImageTagTest extends ParserTestCase 41 { 42 static 43 { 44 System.setProperty ("org.htmlparser.tests.tagTests.ImageTagTest", "ImageTagTest"); 45 } 46 47 public ImageTagTest(String name) { 48 super(name); 49 } 50 51 58 public void testImageTag() throws ParserException 59 { 60 createParser("<IMG alt=Google height=115 SRC=\"goo/title_homepage4.gif\" width=305>","http://www.google.com/test/index.html"); 61 parseAndAssertNodeCount(1); 62 assertTrue("Node should be a HTMLImageTag",node[0] instanceof ImageTag); 64 ImageTag imageTag = (ImageTag)node[0]; 65 assertEquals("The image locn","http://www.google.com/test/goo/title_homepage4.gif",imageTag.getImageURL()); 66 } 67 68 75 public void testImageTagBug() throws ParserException 76 { 77 createParser("<IMG alt=Google height=115 SRC=\"../goo/title_homepage4.gif\" width=305>","http://www.google.com/test/"); 78 parseAndAssertNodeCount(1); 79 assertTrue("Node should be a HTMLImageTag",node[0] instanceof ImageTag); 81 ImageTag imageTag = (ImageTag)node[0]; 82 assertEquals("The image locn","http://www.google.com/goo/title_homepage4.gif",imageTag.getImageURL()); 83 } 84 85 92 public void testImageTageBug2() throws ParserException 93 { 94 createParser("<IMG alt=Google height=115 SRC=\"../../goo/title_homepage4.gif\" width=305>","http://www.google.com/test/test/index.html"); 95 parseAndAssertNodeCount(1); 96 assertTrue("Node should be a HTMLImageTag",node[0] instanceof ImageTag); 98 ImageTag imageTag = (ImageTag)node[0]; 99 assertEquals("The image locn","http://www.google.com/goo/title_homepage4.gif",imageTag.getImageURL()); 100 } 101 102 106 public void testImageTagSingleQuoteBug() throws ParserException 107 { 108 createParser("<IMG SRC='abcd.jpg'>","http://www.cj.com/"); 109 parseAndAssertNodeCount(1); 110 assertTrue("Node should be a HTMLImageTag",node[0] instanceof ImageTag); 111 ImageTag imageTag = (ImageTag)node[0]; 112 assertEquals("Image incorrect","http://www.cj.com/abcd.jpg",imageTag.getImageURL()); 113 } 114 115 122 public void testNullImageBug() throws ParserException 123 { 124 createParser("<IMG SRC=>","http://www.google.com/test/index.html"); 125 parseAndAssertNodeCount(1); 126 assertTrue("Node should be a HTMLImageTag",node[0] instanceof ImageTag); 128 ImageTag imageTag = (ImageTag)node[0]; 129 assertStringEquals("The image location","",imageTag.getImageURL()); 130 } 131 132 public void testToHTML() throws ParserException { 133 String img = "<IMG alt=Google height=115 SRC=\"../../goo/title_homepage4.gif\" width=305>"; 134 createParser(img,"http://www.google.com/test/test/index.html"); 135 parseAndAssertNodeCount(1); 136 assertTrue("Node should be a ImageTag",node[0] instanceof ImageTag); 138 ImageTag imageTag = (ImageTag)node[0]; 139 assertStringEquals("toHtml",img,imageTag.toHtml()); 140 assertEquals("Alt","Google",imageTag.getAttribute("alt")); 141 assertEquals("Height","115",imageTag.getAttribute("height")); 142 assertEquals("Width","305",imageTag.getAttribute("width")); 143 } 144 145 149 public ImageTag extractLinkImage (LinkTag link) 150 { 151 Node[] list = ParserUtils.findTypeInNode (link, ImageTag.class); 152 return (0 == list.length ? null : (ImageTag)list[0]); 153 } 154 155 159 public void testMapFollowImg () throws ParserException 160 { 161 String html = "<a HREF=\"Biography/Biography.html\" " 162 + "onMouseOut=\"MM_swapImgRestore()\" " 163 + "onMouseOver=\"MM_swapImage('Image13','','Graphics/SchneiderPic1.gif',1)\">" 164 + "<img name=\"Image13\" border=\"0\" SRC=\"Graphics/SchneiderPic.gif\" " 165 + "width=\"127\" height=\"175\" usemap=\"#Image13Map\" " 166 + "alt=\"Graphics/SchneiderPic.gif\"> <map name=\"Image13Map\">" 167 + "<area shape=\"circle\" coords=\"67,88,66\" HREF=\"Biography/Biography.html\" " 168 + "onClick=\"newWindow('Biography/Biography.html','HTML','menubar=yes,scrollbars=yes,resizable=yes,left=0,top=0'); return false\" target=\"HTML\">" 169 + "</map>" 170 + "</a>"; 171 createParser (html); 172 parseAndAssertNodeCount (1); 173 assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); 174 LinkTag link = (LinkTag)node[0]; 175 ImageTag img = extractLinkImage (link); 176 assertNotNull ("no image tag", img); 177 } 178 179 184 public void testEmptyStringElement () throws ParserException 185 { 186 String html = "<img height=\"1\" width=\"1\" alt=\"\" " 187 + "src=\"http://i.cnn.net/cnn/images/1.gif\"/>"; 188 189 createParser (html); 190 parseAndAssertNodeCount (1); 191 assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag); 192 ImageTag img = (ImageTag)node[0]; 193 assertTrue ("bad source", "http://i.cnn.net/cnn/images/1.gif".equals (img.getImageURL ())); 194 } 195 196 public void testDynamicRelativeImageScan() throws ParserException { 197 createParser("<IMG SRC=\"../abc/def/mypic.jpg\">","http://www.yahoo.com/ghi?abcdefg"); 198 parseAndAssertNodeCount(1); 199 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 200 ImageTag imageTag = (ImageTag)node[0]; 201 assertEquals("Expected Link","http://www.yahoo.com/abc/def/mypic.jpg",imageTag.getImageURL()); 202 } 203 204 207 public void testExtractImageLocnInvertedCommasBug() throws ParserException 208 { 209 String locn = "http://us.a1.yimg.com/us.yimg.com/i/ww/m5v5.gif"; 210 createParser ("<img width=638 height=53 border=0 usemap=\"#m\" SRC=" + locn + " alt=Yahoo>"); 211 parseAndAssertNodeCount(1); 212 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 213 ImageTag imageTag = (ImageTag)node[0]; 214 assertEquals("Expected Image Locn",locn,imageTag.getImageURL()); 215 } 216 217 222 public void testPlaceHolderImageScan() throws ParserException { 223 createParser("<IMG width=1 height=1 alt=\"a\">","http://www.yahoo.com/ghi?abcdefg"); 224 parseAndAssertNodeCount(1); 225 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 226 ImageTag imageTag = (ImageTag)node[0]; 227 assertEquals("Expected Image Locn","",imageTag.getImageURL()); 228 assertEquals("Image width","1",imageTag.getAttribute("WIDTH")); 229 assertEquals("Image height","1",imageTag.getAttribute("HEIGHT")); 230 assertEquals("alt","a",imageTag.getAttribute("ALT")); 231 } 232 233 public void testRelativeImageScan() throws ParserException { 234 createParser("<IMG SRC=\"mypic.jpg\">","http://www.yahoo.com"); 235 parseAndAssertNodeCount(1); 236 assertTrue("Node identified should be ImageTag",node[0] instanceof ImageTag); 237 ImageTag imageTag = (ImageTag)node[0]; 238 assertEquals("Expected Link","http://www.yahoo.com/mypic.jpg",imageTag.getImageURL()); 239 } 240 241 public void testRelativeImageScan2() throws ParserException { 242 createParser("<IMG SRC=\"abc/def/mypic.jpg\">","http://www.yahoo.com"); parseAndAssertNodeCount(1); 244 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 245 ImageTag imageTag = (ImageTag)node[0]; 246 assertEquals("Expected Link","http://www.yahoo.com/abc/def/mypic.jpg",imageTag.getImageURL()); 247 } 248 249 public void testRelativeImageScan3() throws ParserException { 250 createParser("<IMG SRC=\"../abc/def/mypic.jpg\">","http://www.yahoo.com/ghi"); 251 parseAndAssertNodeCount(1); 252 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 253 ImageTag imageTag = (ImageTag)node[0]; 254 assertEquals("Expected Link","http://www.yahoo.com/abc/def/mypic.jpg",imageTag.getImageURL()); 255 } 256 257 261 public void testImageWithSpaces() throws ParserException 262 { 263 createParser("<IMG SRC=\"../abc/def/Hello World.jpg\">","http://www.yahoo.com/ghi"); 264 parseAndAssertNodeCount(1); 265 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 266 ImageTag imageTag = (ImageTag)node[0]; 267 assertEquals("Expected Link","http://www.yahoo.com/abc/def/Hello World.jpg",imageTag.getImageURL()); 268 } 269 270 public void testImageWithNewLineChars() throws ParserException 271 { 272 createParser("<IMG SRC=\"../abc/def/Hello \r\nWorld.jpg\">","http://www.yahoo.com/ghi"); 273 parseAndAssertNodeCount(1); 274 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 275 ImageTag imageTag = (ImageTag)node[0]; 276 String exp = new String ("http://www.yahoo.com/abc/def/Hello World.jpg"); 277 assertStringEquals("Expected Image",exp,imageTag.getImageURL()); 279 } 280 281 284 public void testImageTagsFromYahoo() throws ParserException 285 { 286 createParser("<small><a HREF=s/5926>Air</a>, <a HREF=s/5927>Hotel</a>, <a HREF=s/5928>Vacations</a>, <a HREF=s/5929>Cruises</a></small></td><td align=center><a HREF=\"http://rd.yahoo.com/M=218794.2020165.3500581.220161/D=yahoo_top/S=2716149:NP/A=1041273/?http://adfarm.mediaplex.com/ad/ck/990-1736-1039-211\" target=\"_top\"><img width=230 height=33 SRC=\"http://us.a1.yimg.com/us.yimg.com/a/co/columbiahouse/4for49Freesh_230x33_redx2.gif\" alt=\"\" border=0></a></td><td nowrap align=center width=215>Find your match on<br><a HREF=s/2734><b>Yahoo! Personals</b></a></td></tr><tr><td colspan=3 align=center><input size=30 name=p>\n"+ 287 "<input type=submit value=Search> <a HREF=r/so>advanced search</a></td></tr></table><table border=0 cellspacing=0 cellpadding=3 width=640><tr><td nowrap align=center><table border=0 cellspacing=0 cellpadding=0><tr><td><a HREF=s/5948><img SRC=\"http://us.i1.yimg.com/us.yimg.com/i/ligans/klgs/eet.gif\" width=20 height=20 border=0></a></td><td> <a HREF=s/1048><b>Yahooligans!</b></a> - <a HREF=s/5282>Eet & Ern</a>, <a HREF=s/5283>Games</a>, <a HREF=s/5284>Science</a>, <a HREF=s/5285>Sports</a>, <a HREF=s/5286>Movies</a>, <a HREF=s/1048>more</a> </td><td><a HREF=s/5948><img SRC=\"http://us.i1.yimg.com/us.yimg.com/i/ligans/klgs/ern.gif\" width=20 height=20 border=0></a></td></tr></table></td></tr><tr><td nowrap align=center><small><b>Shop</b> \n","http://www.yahoo.com"); 288 Node [] node = new Node[10]; 289 parser.setNodeFactory (new PrototypicalNodeFactory (new ImageTag ())); 290 int i = 0; 291 Node thisNode; 292 for (NodeIterator e = parser.elements();e.hasMoreNodes();) { 293 thisNode = e.nextNode(); 294 if (thisNode instanceof ImageTag) 295 node[i++] = thisNode; 296 } 297 assertEquals("Number of nodes identified should be 3",3,i); 298 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 299 ImageTag imageTag = (ImageTag)node[0]; 300 assertEquals("Expected Image","http://us.a1.yimg.com/us.yimg.com/a/co/columbiahouse/4for49Freesh_230x33_redx2.gif",imageTag.getImageURL()); 301 ImageTag imageTag2 = (ImageTag)node[1]; 302 assertEquals("Expected Image 2","http://us.i1.yimg.com/us.yimg.com/i/ligans/klgs/eet.gif",imageTag2.getImageURL()); 303 ImageTag imageTag3 = (ImageTag)node[2]; 304 assertEquals("Expected Image 3","http://us.i1.yimg.com/us.yimg.com/i/ligans/klgs/ern.gif",imageTag3.getImageURL()); 305 } 306 307 310 public void testImageTagsFromYahooWithAllScannersRegistered() throws ParserException 311 { 312 createParser( 313 "<tr>" + 314 "<td>" + 315 " <small><a HREF=s/5926>Air</a>, <a HREF=s/5927>Hotel</a>, " + 316 "<a HREF=s/5928>Vacations</a>, <a HREF=s/5929>Cruises</a></small>" + 317 "</td>" + 318 "<td align=center>" + 319 "<a HREF=\"http://rd.yahoo.com/M=218794.2020165.3500581.220161/D=yahoo_top/S=" + 320 "2716149:NP/A=1041273/?http://adfarm.mediaplex.com/ad/ck/990-1736-1039-211\" " + 321 "target=\"_top\"><img width=230 height=33 SRC=\"http://us.a1.yimg.com/us.yimg.com/a/co/" + 322 "columbiahouse/4for49Freesh_230x33_redx2.gif\" alt=\"\" border=0></a>" + 323 "</td>" + 324 "<td nowrap align=center width=215>" + 325 "Find your match on<br><a HREF=s/2734>" + 326 "<b>Yahoo! Personals</b></a>" + 327 "</td>" + 328 "</tr>" + 329 "<tr>" + 330 "<td colspan=3 align=center>" + 331 "<input size=30 " + 332 "name=p>\n" + 333 "</td>" + 334 "</tr>","http://www.yahoo.com",30 335 ); 336 parseAndAssertNodeCount(2); 337 assertType("first node type",TableRow.class,node[0]); 338 TableRow row = (TableRow)node[0]; 339 TableColumn col = row.getColumns()[1]; 340 Node node = col.children().nextNode(); 341 assertType("Node identified should be HTMLLinkTag",LinkTag.class,node); 342 LinkTag linkTag = (LinkTag)node; 343 Node nodeInsideLink = linkTag.children().nextNode(); 344 assertType("Tag within link should be an image tag",ImageTag.class,nodeInsideLink); 345 ImageTag imageTag = (ImageTag)nodeInsideLink; 346 assertStringEquals( 347 "Expected Image", 348 "http://us.a1.yimg.com/us.yimg.com/a/co/columbiahouse/4for49Freesh_230x33_redx2.gif", 349 imageTag.getImageURL() 350 ); 351 } 352 353 357 public void testImageTagOnMultipleLines() throws ParserException { 358 createParser( 359 "<td rowspan=3>" + 360 "<img height=49 \n\n"+ 361 "alt=\"Central Intelligence Agency, Director of Central Intelligence\" \n\n"+ 362 "src=\"graphics/images_home2/cia_banners_template3_01.gif\" \n\n"+ 363 "width=241>" + 364 "</td>", 365 "http://www.cia.gov" 366 ); 367 parseAndAssertNodeCount(1); 368 assertType("node should be", TableColumn.class, node[0]); 369 TableColumn col = (TableColumn)node[0]; 370 Node node = col.children().nextNode(); 371 assertType("node inside column",ImageTag.class,node); 372 ImageTag imageTag = (ImageTag)node; 373 assertEquals("Image location","http://www.cia.gov/graphics/images_home2/cia_banners_template3_01.gif",imageTag.getImageURL()); 375 assertEquals("Alt Value","Central Intelligence Agency, Director of Central Intelligence",imageTag.getAttribute("ALT")); 376 assertEquals("Width","241",imageTag.getAttribute("WIDTH")); 377 assertEquals("Height","49",imageTag.getAttribute("HEIGHT")); 378 } 379 380 public void testDirectRelativeLinks() throws ParserException { 381 createParser("<IMG SRC = \"/images/lines/li065.jpg\">","http://www.cybergeo.presse.fr/REVGEO/ttsavoir/joly.htm"); 382 parseAndAssertNodeCount(1); 383 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 384 ImageTag imageTag = (ImageTag)node[0]; 385 assertEquals("Image Location","http://www.cybergeo.presse.fr/images/lines/li065.jpg",imageTag.getImageURL()); 386 387 } 388 389 393 public void testMissingEqualTo() throws ParserException { 394 createParser("<img src\"/images/spacer.gif\" width=\"1\" height=\"1\" alt=\"\">","http://www.htmlparser.org/subdir1/subdir2"); 395 parseAndAssertNodeCount(1); 396 assertTrue("Node identified should be HTMLImageTag",node[0] instanceof ImageTag); 397 ImageTag imageTag = (ImageTag)node[0]; 398 assertStringEquals("Image Location","http://www.htmlparser.org/images/spacer.gif",imageTag.getImageURL()); 399 assertEquals("Width","1",imageTag.getAttribute("WIDTH")); 400 assertEquals("Height","1",imageTag.getAttribute("HEIGHT")); 401 assertEquals("Alt","",imageTag.getAttribute("ALT")); 402 } 403 } 404
| Popular Tags
|