KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > tagTests > ImageTagTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/ImageTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/09/02 02:28:14 $
10
// $Revision: 1.46 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

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 JavaDoc name) {
48         super(name);
49     }
50
51     /**
52      * The bug being reproduced is this : <BR>
53      * &lt;BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR>
54      * vLink=#551a8b&gt;
55      * The above line is incorrectly parsed in that, the BODY tag is not identified.
56      * Creation date: (6/17/2001 4:01:06 PM)
57      */

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         // The node should be an HTMLImageTag
63
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     /**
69      * The bug being reproduced is this : <BR>
70      * &lt;BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR>
71      * vLink=#551a8b&gt;
72      * The above line is incorrectly parsed in that, the BODY tag is not identified.
73      * Creation date: (6/17/2001 4:01:06 PM)
74      */

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         // The node should be an HTMLImageTag
80
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     /**
86      * The bug being reproduced is this : <BR>
87      * &lt;BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR>
88      * vLink=#551a8b&gt;
89      * The above line is incorrectly parsed in that, the BODY tag is not identified.
90      * Creation date: (6/17/2001 4:01:06 PM)
91      */

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         // The node should be an HTMLImageTag
97
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     /**
103      * This bug occurs when there is a null pointer exception thrown while scanning a tag using LinkScanner.
104      * Creation date: (7/1/2001 2:42:13 PM)
105      */

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     /**
116      * The bug being reproduced is this : <BR>
117      * &lt;A HREF=&gt;Something&lt;A&gt;<BR>
118      * vLink=#551a8b&gt;
119      * The above line is incorrectly parsed in that, the BODY tag is not identified.
120      * Creation date: (6/17/2001 4:01:06 PM)
121      */

122     public void testNullImageBug() throws ParserException
123     {
124         createParser("<IMG SRC=>","http://www.google.com/test/index.html");
125         parseAndAssertNodeCount(1);
126         // The node should be an HTMLLinkTag
127
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 JavaDoc 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         // The node should be an ImageTag
137
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     /**
146      * See bug #753003 <IMG> within <A> missed when followed by <MAP>
147      * Not reproducible.
148      */

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     /**
156      * See bug #753003 <IMG> within <A> missed when followed by <MAP>
157      * Not reproducible.
158      */

159     public void testMapFollowImg () throws ParserException
160     {
161         String JavaDoc 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     /**
180      * Test empty attribute.
181      * See bug #755929 Empty string attr. value causes attr parsing to be stopped
182      * and bug #753012 IMG SRC not parsed v1.3 & v1.4
183      */

184     public void testEmptyStringElement () throws ParserException
185     {
186         String JavaDoc 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     /**
205      * This is the reproduction of a bug which causes a null pointer exception
206      */

207     public void testExtractImageLocnInvertedCommasBug() throws ParserException
208     {
209         String JavaDoc 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     /**
218      * This test has been improved to check for params
219      * in the image tag, based on requirement by Annette Doyle.
220      * Thereby an important bug was detected.
221      */

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"); // Register the image scanner
243
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     /**
258      * Test image url which contains spaces in it.
259      * This was actually a bug reported by Sam Joseph (sam@neurogrid.net)
260      */

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 JavaDoc exp = new String JavaDoc("http://www.yahoo.com/abc/def/Hello World.jpg");
277         //assertEquals("Length of image",exp.length(),imageTag.getImageLocation().length());
278
assertStringEquals("Expected Image",exp,imageTag.getImageURL());
279     }
280
281     /**
282      * Test case to reproduce bug reported by Annette
283      */

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> &nbsp; &nbsp; <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> &nbsp; &nbsp; </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>&nbsp;\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     /**
308      * Test case to reproduce bug reported by Annette
309      */

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     /**
354      * This is the reproduction of a bug reported
355      * by Annette Doyle
356      */

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         // Get the data from the node
374
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     /**
390      * Based on a page submitted by Claude Duguay, the image tag has IMG SRC"somefile.jpg" - a missing equal
391      * to sign
392      */

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