KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > lexerTests > AttributeTests


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/lexerTests/AttributeTests.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/18 21:31:22 $
10
// $Revision: 1.19 $
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.lexerTests;
28
29 import java.util.Vector JavaDoc;
30 import org.htmlparser.Node;
31
32 import org.htmlparser.Attribute;
33 import org.htmlparser.PrototypicalNodeFactory;
34 import org.htmlparser.Tag;
35 import org.htmlparser.lexer.PageAttribute;
36 import org.htmlparser.nodes.TagNode;
37 import org.htmlparser.tags.ImageTag;
38 import org.htmlparser.tags.LinkTag;
39 import org.htmlparser.tests.ParserTestCase;
40 import org.htmlparser.util.NodeIterator;
41 import org.htmlparser.util.ParserException;
42
43 public class AttributeTests extends ParserTestCase
44 {
45     static
46     {
47         System.setProperty ("org.htmlparser.tests.lexerTests.AttributeTests", "AttributeTests");
48     }
49
50     private static final boolean JSP_TESTS_ENABLED = false;
51     private Tag tag;
52     private Vector JavaDoc attributes;
53
54     public AttributeTests (String JavaDoc name) {
55         super(name);
56     }
57
58     public void getParameterTableFor(String JavaDoc tagContents)
59     {
60         getParameterTableFor (tagContents, false);
61     }
62
63     public void getParameterTableFor(String JavaDoc tagContents, boolean dump)
64     {
65         String JavaDoc html;
66         NodeIterator iterator;
67         Node node;
68
69         html = "<" + tagContents + ">";
70         createParser (html);
71         parser.setNodeFactory (new PrototypicalNodeFactory (true));
72         try
73         {
74             iterator = parser.elements ();
75             node = iterator.nextNode ();
76             if (node instanceof Tag)
77             {
78                 tag = (Tag)node;
79                 attributes = tag.getAttributesEx ();
80                 if (dump)
81                 {
82                     for (int i = 0; i < attributes.size (); i++)
83                     {
84                         System.out.print ("Attribute #" + i);
85                         Attribute attribute = (Attribute)attributes.elementAt (i);
86                         if (null != attribute.getName ())
87                             System.out.print (" Name: '" + attribute.getName () + "'");
88                         if (null != attribute.getAssignment ())
89                             System.out.print (" Assignment: '" + attribute.getAssignment () + "'");
90                         if (0 != attribute.getQuote ())
91                             System.out.print (" Quote: " + attribute.getQuote ());
92                         if (null != attribute.getValue ())
93                             System.out.print (" Value: '" + attribute.getValue () + "'");
94                         System.out.println ();
95                     }
96                     System.out.println ();
97                 }
98             }
99             else
100                 attributes = null;
101             String JavaDoc string = node.toHtml ();
102             assertEquals ("toHtml differs", html, string);
103             assertTrue ("shouldn't be any more nodes", !iterator.hasMoreNodes ());
104         }
105         catch (ParserException pe)
106         {
107             fail (pe.getMessage ());
108         }
109     }
110
111     /**
112      * Test constructors.
113      */

114     public void testConstructors ()
115     {
116         Vector JavaDoc attributes;
117         Tag tag;
118         String JavaDoc html;
119
120         attributes = new Vector JavaDoc ();
121          // String, null
122
attributes.add (new Attribute ("wombat", null));
123         // String
124
attributes.add (new Attribute (" "));
125         // String, String
126
attributes.add (new Attribute ("label", "The civil war."));
127         attributes.add (new Attribute (" "));
128         // String, String, String
129
attributes.add (new Attribute ("frameborder", "= ", "no"));
130         attributes.add (new Attribute (" "));
131         // String String, String, char
132
attributes.add (new Attribute ("name", "=", "topFrame", '"'));
133         tag = new TagNode (null, 0, 0, attributes);
134         html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">";
135         assertStringEquals ("tag contents", html, tag.toHtml ());
136     }
137
138     /**
139      * Test bean properties.
140      */

141     public void testProperties ()
142     {
143         Attribute attribute;
144         Attribute space;
145         Vector JavaDoc attributes;
146         Tag tag;
147         String JavaDoc html;
148
149         attributes = new Vector JavaDoc ();
150         attribute = new Attribute ();
151         attribute.setName ("wombat");
152         assertTrue ("should be standalone", attribute.isStandAlone ());
153         assertTrue ("should not be whitespace", !attribute.isWhitespace ());
154         assertTrue ("should not be valued", !attribute.isValued ());
155         assertTrue ("should not be empty", !attribute.isEmpty ());
156         attributes.add (attribute);
157         space = new Attribute ();
158         space.setValue (" ");
159         assertTrue ("should not be standalone", !space.isStandAlone ());
160         assertTrue ("should be whitespace", space.isWhitespace ());
161         assertTrue ("should be valued", space.isValued ());
162         assertTrue ("should not be empty", !space.isEmpty ());
163         attributes.add (space);
164         attribute = new Attribute ();
165         attribute.setName ("label");
166         attribute.setAssignment ("=");
167         attribute.setRawValue ("The civil war.");
168         assertTrue ("should not be standalone", !attribute.isStandAlone ());
169         assertTrue ("should not be whitespace", !attribute.isWhitespace ());
170         assertTrue ("should be valued", attribute.isValued ());
171         assertTrue ("should not be empty", !attribute.isEmpty ());
172         attributes.add (attribute);
173         attributes.add (space);
174         attribute = new Attribute ();
175         attribute.setName ("frameborder");
176         attribute.setAssignment ("= ");
177         attribute.setRawValue ("no");
178         attributes.add (attribute);
179         attributes.add (space);
180         attribute = new Attribute ();
181         attribute.setName ("name");
182         attribute.setAssignment ("=");
183         attribute.setValue ("topFrame");
184         attribute.setQuote ('"');
185         assertTrue ("should not be standalone", !attribute.isStandAlone ());
186         assertTrue ("should not be whitespace", !attribute.isWhitespace ());
187         assertTrue ("should be valued", attribute.isValued ());
188         assertTrue ("should not be empty", !attribute.isEmpty ());
189         attributes.add (attribute);
190         tag = new TagNode (null, 0, 0, attributes);
191         html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">";
192         assertStringEquals ("tag contents", html, tag.toHtml ());
193     }
194
195     /**
196      * Test constructors.
197      */

198     public void testConstructors2 ()
199     {
200         Vector JavaDoc attributes;
201         Tag tag;
202         String JavaDoc html;
203
204         attributes = new Vector JavaDoc ();
205          // String, null
206
attributes.add (new PageAttribute ("wombat", null));
207         // String
208
attributes.add (new PageAttribute (" "));
209         // String, String
210
attributes.add (new PageAttribute ("label", "The civil war."));
211         attributes.add (new PageAttribute (" "));
212         // String, String, String
213
attributes.add (new PageAttribute ("frameborder", "= ", "no"));
214         attributes.add (new PageAttribute (" "));
215         // String String, String, char
216
attributes.add (new PageAttribute ("name", "=", "topFrame", '"'));
217         tag = new TagNode (null, 0, 0, attributes);
218         html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">";
219         assertStringEquals ("tag contents", html, tag.toHtml ());
220     }
221
222     /**
223      * Test bean properties.
224      */

225     public void testProperties2 ()
226     {
227         Attribute attribute;
228         Attribute space;
229         Vector JavaDoc attributes;
230         Tag tag;
231         String JavaDoc html;
232
233         attributes = new Vector JavaDoc ();
234         attribute = new PageAttribute ();
235         attribute.setName ("wombat");
236         assertTrue ("should be standalone", attribute.isStandAlone ());
237         assertTrue ("should not be whitespace", !attribute.isWhitespace ());
238         assertTrue ("should not be valued", !attribute.isValued ());
239         assertTrue ("should not be empty", !attribute.isEmpty ());
240         attributes.add (attribute);
241         space = new PageAttribute ();
242         space.setValue (" ");
243         assertTrue ("should not be standalone", !space.isStandAlone ());
244         assertTrue ("should be whitespace", space.isWhitespace ());
245         assertTrue ("should be valued", space.isValued ());
246         assertTrue ("should not be empty", !space.isEmpty ());
247         attributes.add (space);
248         attribute = new PageAttribute ();
249         attribute.setName ("label");
250         attribute.setAssignment ("=");
251         attribute.setRawValue ("The civil war.");
252         assertTrue ("should not be standalone", !attribute.isStandAlone ());
253         assertTrue ("should not be whitespace", !attribute.isWhitespace ());
254         assertTrue ("should be valued", attribute.isValued ());
255         assertTrue ("should not be empty", !attribute.isEmpty ());
256         attributes.add (attribute);
257         attributes.add (space);
258         attribute = new PageAttribute ();
259         attribute.setName ("frameborder");
260         attribute.setAssignment ("= ");
261         attribute.setRawValue ("no");
262         assertTrue ("should not be standalone", !attribute.isStandAlone ());
263         assertTrue ("should not be whitespace", !attribute.isWhitespace ());
264         assertTrue ("should be valued", attribute.isValued ());
265         assertTrue ("should not be empty", !attribute.isEmpty ());
266          attributes.add (attribute);
267         attributes.add (space);
268         attribute = new PageAttribute ();
269         attribute.setName ("name");
270         attribute.setAssignment ("=");
271         attribute.setValue ("topFrame");
272         attribute.setQuote ('"');
273         assertTrue ("should not be standalone", !attribute.isStandAlone ());
274         assertTrue ("should not be whitespace", !attribute.isWhitespace ());
275         assertTrue ("should be valued", attribute.isValued ());
276         assertTrue ("should not be empty", !attribute.isEmpty ());
277          attributes.add (attribute);
278         tag = new TagNode (null, 0, 0, attributes);
279         html = "<wombat label=\"The civil war.\" frameborder= no name=\"topFrame\">";
280         assertStringEquals ("tag contents", html, tag.toHtml ());
281     }
282
283     /**
284      * Test simple value.
285      */

286     public void testParseParameters() {
287         getParameterTableFor("a b = \"c\"");
288         assertEquals("Value","c",((Attribute)(attributes.elementAt (2))).getValue ());
289     }
290
291     /**
292      * Test quote value.
293      */

294     public void testParseTokenValues() {
295         getParameterTableFor("a b = \"'\"");
296         assertEquals("Value","'",((Attribute)(attributes.elementAt (2))).getValue ());
297     }
298
299     /**
300      * Test empty value.
301      */

302     public void testParseEmptyValues() {
303         getParameterTableFor("a b = \"\"");
304         assertEquals("Value","",((Attribute)(attributes.elementAt (2))).getValue ());
305     }
306
307     /**
308      * Test no equals or whitespace.
309      * This might be reason for another rule, since another interpretation
310      * would have an attribute called B with a value of "C".
311      */

312     public void testParseMissingEqual() {
313         getParameterTableFor("a b\"c\"");
314         assertEquals("NameC", "b\"c\"", ((Attribute)(attributes.elementAt (2))).getName ());
315     }
316
317     /**
318      * Test multiple attributes.
319      */

320     public void testTwoParams(){
321         getParameterTableFor("PARAM NAME=\"Param1\" VALUE=\"Somik\"");
322         assertEquals("Param1","Param1",((Attribute)(attributes.elementAt (2))).getValue ());
323         assertEquals("Somik","Somik",((Attribute)(attributes.elementAt (4))).getValue ());
324     }
325
326     /**
327      * Test unquoted attributes.
328      */

329     public void testPlainParams(){
330         getParameterTableFor("PARAM NAME=Param1 VALUE=Somik");
331         assertEquals("Param1","Param1",((Attribute)(attributes.elementAt (2))).getValue ());
332         assertEquals("Somik","Somik",((Attribute)(attributes.elementAt (4))).getValue ());
333     }
334
335     /**
336      * Test standalone attribute.
337      */

338     public void testValueMissing() {
339         getParameterTableFor("INPUT type=\"checkbox\" name=\"Authorize\" value=\"Y\" checked");
340         assertEquals("Name of Tag","INPUT",((Attribute)(attributes.elementAt (0))).getName ());
341         assertEquals("Type","checkbox",((Attribute)(attributes.elementAt (2))).getValue ());
342         assertEquals("Name","Authorize",((Attribute)(attributes.elementAt (4))).getValue ());
343         assertEquals("Value","Y",((Attribute)(attributes.elementAt (6))).getValue ());
344         assertEquals("Checked",null,((Attribute)(attributes.elementAt (8))).getValue ());
345     }
346
347     /**
348      * This is a simulation of a bug reported by Dhaval Udani - wherein
349      * a space before the end of the tag causes a problem - there is a key
350      * in the table with just a space in it and an empty value
351      */

352     public void testIncorrectSpaceKeyBug() {
353         getParameterTableFor("TEXTAREA name=\"Remarks\" ");
354         // There should only be two keys..
355
assertEquals("There should only be two attributes",4,attributes.size());
356         // The first key is name
357
assertEquals("Expected name","TEXTAREA",((Attribute)(attributes.elementAt (0))).getName ());
358         assertEquals("Expected value 1", "Remarks",((Attribute)(attributes.elementAt (2))).getValue ());
359     }
360
361     /**
362      * Test empty attribute.
363      */

364     public void testNullTag(){
365         getParameterTableFor("INPUT type=");
366         assertEquals("Name of Tag","INPUT",((Attribute)(attributes.elementAt (0))).getName ());
367         assertNull("Type",((Attribute)(attributes.elementAt (2))).getValue ());
368     }
369
370     /**
371      * Test attribute containing an equals sign.
372      */

373     public void testAttributeWithSpuriousEqualTo() {
374         getParameterTableFor(
375             "a class=rlbA HREF=/news/866201.asp?0sl=-32"
376         );
377         assertStringEquals(
378             "href",
379             "/news/866201.asp?0sl=-32",
380             ((Attribute)(attributes.elementAt (4))).getValue ()
381         );
382     }
383
384     /**
385      * Test attribute containing a question mark.
386      */

387     public void testQuestionMarksInAttributes() {
388         getParameterTableFor(
389             "a HREF=\"mailto:sam@neurogrid.com?subject=Site Comments\""
390         );
391         assertStringEquals(
392             "href",
393             "mailto:sam@neurogrid.com?subject=Site Comments",
394             ((Attribute)(attributes.elementAt (2))).getValue ()
395         );
396         assertStringEquals(
397             "tag name",
398             "a",
399             ((Attribute)(attributes.elementAt (0))).getName ()
400         );
401     }
402
403     /**
404      * Check that an empty tag is considered a string node.
405      * Believe it or not Moi (vincent_aumont) wants htmlparser to parse a text file
406      * containing something that looks nearly like a tag:
407      * <pre>
408      * "basic_string&lt;char, string_char_traits&lt;char&gt;, &lt;&gt;&gt;::basic_string()"
409      * </pre>
410      * This was throwing a null pointer exception when the empty &lt;&gt; was encountered.
411      * Bug #725420 NPE in StringBean.visitTag
412      **/

413     public void testEmptyTag () {
414         getParameterTableFor("");
415         assertNull ("<> is not a tag",attributes);
416     }
417
418     /**
419      * Test attributes when they contain scriptlets.
420      * Submitted by Cory Seefurth
421      * See also feature request #725376 Handle script in attributes.
422      */

423     public void testJspWithinAttributes() {
424         if (JSP_TESTS_ENABLED)
425         {
426             getParameterTableFor(
427                 "a HREF=\"<%=Application(\"sURL\")%>/literature/index.htm"
428             );
429             assertStringEquals(
430                 "href",
431                 "<%=Application(\"sURL\")%>/literature/index.htm",
432                 ((Attribute)(attributes.elementAt (2))).getValue ()
433             );
434         }
435     }
436
437     /**
438      * Test Script in attributes.
439      * See feature request #725376 Handle script in attributes.
440      */

441     public void testScriptedTag () {
442         getParameterTableFor("body onLoad=defaultStatus=''");
443         String JavaDoc name = ((Attribute)(attributes.elementAt (0))).getName ();
444         assertNotNull ("No Tag.TAGNAME", name);
445         assertStringEquals("tag name parsed incorrectly", "body", name);
446         String JavaDoc value = ((Attribute)(attributes.elementAt (2))).getValue ();
447         assertStringEquals ("parameter parsed incorrectly", "defaultStatus=''", value);
448     }
449
450     /**
451      * Test that stand-alone attributes are kept that way, rather than being
452      * given empty values.
453      * -Joe Robins, 6/19/03
454      */

455     public void testStandaloneAttribute ()
456     {
457         getParameterTableFor ("INPUT DISABLED");
458         assertStringEquals("Standalone attribute not parsed","DISABLED",((Attribute)(attributes.elementAt (2))).getName ());
459         assertNull ("Standalone attribute has non-null value",((Attribute)(attributes.elementAt (2))).getValue ());
460     }
461
462     /**
463      * Test missing value.
464      */

465     public void testMissingAttribute ()
466     {
467         getParameterTableFor ("INPUT DISABLED=");
468         assertStringEquals("Empty attribute has no attribute","DISABLED",((Attribute)(attributes.elementAt (2))).getName ());
469         assertEquals ("Attribute has non-blank value",null,((Attribute)(attributes.elementAt (2))).getValue ());
470     }
471
472     /**
473      * Test Rule 1.
474      * See discussion in Bug#891058 Bug in lexer. regarding alternate interpretations.
475      */

476     public void testRule1 ()
477     {
478         getParameterTableFor ("tag att = other=fred");
479         assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ());
480         assertEquals ("Attribute has wrong value", "other=fred", ((Attribute)(attributes.elementAt (2))).getValue ());
481         for (int i = 0; i < attributes.size (); i++)
482             assertTrue ("No attribute should be called =", !((Attribute)(attributes.elementAt (2))).getName ().equals ("="));
483     }
484
485     /**
486      * Test Rule 2.
487      */

488     public void testRule2 ()
489     {
490         getParameterTableFor ("tag att =value other=fred");
491         assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ());
492         assertEquals ("Attribute has wrong value", "value", ((Attribute)(attributes.elementAt (2))).getValue ());
493         for (int i = 0; i < attributes.size (); i++)
494             assertTrue ("No attribute should be called =value", !((Attribute)(attributes.elementAt (2))).getName ().equals ("=value"));
495         assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (4))).getName ());
496         assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (4))).getValue ());
497     }
498
499     /**
500      * Test Rule 3.
501      */

502     public void testRule3 ()
503     {
504         getParameterTableFor ("tag att= \"value\" other=fred");
505         assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ());
506         assertEquals ("Attribute has wrong value", "value", ((Attribute)(attributes.elementAt (2))).getValue ());
507         for (int i = 0; i < attributes.size (); i++)
508             assertTrue ("No attribute should be called \"value\"", !((Attribute)(attributes.elementAt (2))).getName ().equals ("\"value\""));
509         assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (4))).getName ());
510         assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (4))).getValue ());
511     }
512
513     /**
514      * Test Rule 4.
515      * See discussion in Bug#891058 Bug in lexer. regarding alternate interpretations.
516      */

517     public void testRule4 ()
518     {
519         getParameterTableFor ("tag att=\"va\"lue\" other=fred");
520         assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ());
521         assertEquals ("Attribute has wrong value", "va", ((Attribute)(attributes.elementAt (2))).getValue ());
522         for (int i = 0; i < attributes.size (); i++)
523             assertTrue ("No attribute should be called va\"lue", !((Attribute)(attributes.elementAt (2))).getName ().equals ("va\"lue"));
524         assertStringEquals("Attribute missing","att",((Attribute)(attributes.elementAt (2))).getName ());
525         assertStringEquals("Attribute not parsed","lue\"",((Attribute)(attributes.elementAt (3))).getName ());
526         assertNull ("Attribute has wrong value", ((Attribute)(attributes.elementAt (3))).getValue ());
527         assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (5))).getName ());
528         assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (5))).getValue ());
529     }
530
531     /**
532      * Test Rule 5.
533      * See discussion in Bug#891058 Bug in lexer. regarding alternate interpretations.
534      */

535     public void testRule5 ()
536     {
537         getParameterTableFor ("tag att='va'lue' other=fred");
538         assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ());
539         assertEquals ("Attribute has wrong value", "va", ((Attribute)(attributes.elementAt (2))).getValue ());
540         for (int i = 0; i < attributes.size (); i++)
541             assertTrue ("No attribute should be called va'lue", !((Attribute)(attributes.elementAt (2))).getName ().equals ("va'lue"));
542         assertStringEquals("Attribute not parsed","lue'",((Attribute)(attributes.elementAt (3))).getName ());
543         assertNull ("Attribute has wrong value", ((Attribute)(attributes.elementAt (3))).getValue ());
544         assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (5))).getName ());
545         assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (5))).getValue ());
546     }
547     
548     /**
549      * Test for lost attributes.
550      * see bug #778781 SRC-attribute suppression in IMG-tags
551      * & #753012 IMG SRC not parsed v1.3 & v1.4
552      * & #755929 Empty string attr. value causes attr parsing to be stopped
553      * & #778781 SRC-attribute suppression in IMG-tags
554      * & #832530 empty attribute causes parser to fail
555      * & #851882 zero length alt tag causes bug in ImageScanner
556      *
557      * HTML before parse:
558      * <img SRC="images/first" alt="first">"
559      * <img SRC="images/second" alt="">
560      * <img alt="third" SRC="images/third">
561      * <img alt="" SRC="images/fourth">
562      *
563      * HTML after parse:
564      * <IMG ALT="first" SRC="images/first">
565      * <IMG ALT="" SRC="images/second">
566      * <IMG ALT="third" SRC="images/third">
567      * <IMG ALT="">
568      */

569     public void testSrcAndAlt () throws ParserException
570     {
571         String JavaDoc html = "<img SRC=\"images/first\" alt=\"first\">";
572
573         createParser (html);
574         parseAndAssertNodeCount (1);
575         assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);
576         ImageTag img = (ImageTag)node[0];
577         assertTrue ("bad source", "images/first".equals (img.getImageURL ()));
578         assertTrue ("bad alt", "first".equals (img.getAttribute ("alt")));
579         assertStringEquals ("toHtml()", html, img.toHtml ());
580     }
581
582     /**
583      * see bug #778781 SRC-attribute suppression in IMG-tags
584      */

585     public void testSrcAndEmptyAlt () throws ParserException
586     {
587         String JavaDoc html = "<img SRC=\"images/second\" alt=\"\">";
588
589         createParser (html);
590         parseAndAssertNodeCount (1);
591         assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);
592         ImageTag img = (ImageTag)node[0];
593         assertTrue ("bad source", "images/second".equals (img.getImageURL ()));
594         assertTrue ("bad alt", "".equals (img.getAttribute ("alt")));
595         assertStringEquals ("toHtml()", html, img.toHtml ());
596     }
597
598     /**
599      * see bug #778781 SRC-attribute suppression in IMG-tags
600      */

601     public void testAltAndSrc () throws ParserException
602     {
603         String JavaDoc html = "<img alt=\"third\" SRC=\"images/third\">";
604
605         createParser (html);
606         parseAndAssertNodeCount (1);
607         assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);
608         ImageTag img = (ImageTag)node[0];
609         assertTrue ("bad source", "images/third".equals (img.getImageURL ()));
610         assertTrue ("bad alt", "third".equals (img.getAttribute ("alt")));
611         assertStringEquals ("toHtml()", html, img.toHtml ());
612     }
613
614     /**
615      * see bug #778781 SRC-attribute suppression in IMG-tags
616      */

617     public void testEmptyAltAndSrc () throws ParserException
618     {
619         String JavaDoc html = "<img alt=\"\" SRC=\"images/third\">";
620
621         createParser (html);
622         parseAndAssertNodeCount (1);
623         assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);
624         ImageTag img = (ImageTag)node[0];
625         assertTrue ("bad source", "images/third".equals (img.getImageURL ()));
626         assertTrue ("bad alt", "".equals (img.getAttribute ("alt")));
627         assertStringEquals ("toHtml()", html, img.toHtml ());
628     }
629
630     /**
631      * see bug #911565 isValued() and isNull() don't work
632      */

633     public void testPredicates () throws ParserException
634     {
635         String JavaDoc html1 = "<img alt=\"\" SRC=\"images/third\" readonly>";
636         String JavaDoc html2 = "<img SRC=\"images/third\" readonly alt=\"\">";
637         String JavaDoc html3 = "<img readonly alt=\"\" SRC=\"images/third\">";
638         String JavaDoc htmls[] = { html1, html2, html3 };
639
640         for (int i = 0; i < htmls.length; i++)
641         {
642             createParser (htmls[i]);
643             parseAndAssertNodeCount (1);
644             assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);
645             ImageTag img = (ImageTag)node[0];
646             Attribute src = img.getAttributeEx ("src");
647             Attribute alt = img.getAttributeEx ("alt");
648             Attribute readonly = img.getAttributeEx ("readonly");
649             assertTrue ("src whitespace", !src.isWhitespace ());
650             assertTrue ("src not valued", src.isValued ());
651             assertTrue ("src empty", !src.isEmpty ());
652             assertTrue ("src standalone", !src.isStandAlone ());
653             assertTrue ("alt whitespace", !alt.isWhitespace ());
654             assertTrue ("alt valued", !alt.isValued ());
655             assertTrue ("alt empty", !alt.isEmpty ());
656             assertTrue ("alt standalone", !alt.isStandAlone ());
657             assertTrue ("readonly whitespace", !readonly.isWhitespace ());
658             assertTrue ("readonly valued", !readonly.isValued ());
659             assertTrue ("readonly empty", !readonly.isEmpty ());
660             assertTrue ("readonly not standalone", readonly.isStandAlone ());
661             // try assigning the name and checking again
662
src.setName ("SRC");
663             assertTrue ("setName() failed", "SRC=\"images/third\"".equals (src.toString ()));
664             assertTrue ("src whitespace", !src.isWhitespace ());
665             assertTrue ("src not valued", src.isValued ());
666             assertTrue ("src empty", !src.isEmpty ());
667             assertTrue ("src standalone", !src.isStandAlone ());
668             alt.setName ("ALT");
669             assertTrue ("setName() failed", "ALT=\"\"".equals (alt.toString ()));
670             assertTrue ("alt whitespace", !alt.isWhitespace ());
671             assertTrue ("alt valued", !alt.isValued ());
672             assertTrue ("alt empty", !alt.isEmpty ());
673             assertTrue ("alt standalone", !alt.isStandAlone ());
674             readonly.setName ("READONLY");
675             assertTrue ("setName() failed", "READONLY".equals (readonly.toString ()));
676             assertTrue ("readonly whitespace", !readonly.isWhitespace ());
677             assertTrue ("readonly valued", !readonly.isValued ());
678             assertTrue ("readonly empty", !readonly.isEmpty ());
679             assertTrue ("readonly not standalone", readonly.isStandAlone ());
680             // try assigning the assignment and checking again
681
src.setAssignment (" = ");
682             assertTrue ("setAssignment() failed", "SRC = \"images/third\"".equals (src.toString ()));
683             assertTrue ("src whitespace", !src.isWhitespace ());
684             assertTrue ("src not valued", src.isValued ());
685             assertTrue ("src empty", !src.isEmpty ());
686             assertTrue ("src standalone", !src.isStandAlone ());
687             alt.setAssignment (" = ");
688             assertTrue ("setAssignment() failed", "ALT = \"\"".equals (alt.toString ()));
689             assertTrue ("alt whitespace", !alt.isWhitespace ());
690             assertTrue ("alt valued", !alt.isValued ());
691             assertTrue ("alt empty", !alt.isEmpty ());
692             assertTrue ("alt standalone", !alt.isStandAlone ());
693             readonly.setAssignment ("=");
694             assertTrue ("setAssignment() failed", "READONLY=".equals (readonly.toString ()));
695             assertTrue ("readonly whitespace", !readonly.isWhitespace ());
696             assertTrue ("readonly valued", !readonly.isValued ());
697             assertTrue ("readonly not empty", readonly.isEmpty ());
698             assertTrue ("readonly standalone", !readonly.isStandAlone ());
699             // try assigning the value and checking again
700
createParser (htmls[i]);
701             parseAndAssertNodeCount (1);
702             assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);
703             img = (ImageTag)node[0];
704             src = img.getAttributeEx ("src");
705             alt = img.getAttributeEx ("alt");
706             readonly = img.getAttributeEx ("readonly");
707             src.setValue ("cgi-bin/redirect");
708             assertTrue ("setValue() failed", "src=\"cgi-bin/redirect\"".equals (src.toString ()));
709             assertTrue ("src whitespace", !src.isWhitespace ());
710             assertTrue ("src not valued", src.isValued ());
711             assertTrue ("src empty", !src.isEmpty ());
712             assertTrue ("src standalone", !src.isStandAlone ());
713             alt.setValue ("no image");
714             assertTrue ("setValue() failed", "alt=\"no image\"".equals (alt.toString ()));
715             assertTrue ("alt whitespace", !alt.isWhitespace ());
716             assertTrue ("alt not valued", alt.isValued ());
717             assertTrue ("alt empty", !alt.isEmpty ());
718             assertTrue ("alt standalone", !alt.isStandAlone ());
719             readonly.setValue ("true"); // this may be bogus, really need to set assignment too, see below
720
assertTrue ("setValue() failed", "readonlytrue".equals (readonly.toString ()));
721             assertTrue ("readonly whitespace", !readonly.isWhitespace ());
722             assertTrue ("readonly not valued", readonly.isValued ());
723             assertTrue ("readonly empty", !readonly.isEmpty ());
724             assertTrue ("readonly standalone", !readonly.isStandAlone ());
725             readonly.setAssignment ("=");
726             assertTrue ("setAssignment() failed", "readonly=true".equals (readonly.toString ()));
727             assertTrue ("readonly whitespace", !readonly.isWhitespace ());
728             assertTrue ("readonly not valued", readonly.isValued ());
729             assertTrue ("readonly empty", !readonly.isEmpty ());
730             assertTrue ("readonly standalone", !readonly.isStandAlone ());
731         }
732     }
733
734     /**
735      * see bug #911565 isValued() and isNull() don't work
736      */

737     public void testSetQuote () throws ParserException
738     {
739         String JavaDoc html = "<img alt=\"\" SRC=\"images/third\" toast>";
740
741         createParser (html);
742         parseAndAssertNodeCount (1);
743         assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);
744         ImageTag img = (ImageTag)node[0];
745         Attribute src = img.getAttributeEx ("src");
746         src.setQuote ('\0');
747         assertTrue ("setQuote('\\0') failed", "src=images/third".equals (src.toString ()));
748         src.setQuote ('\'');
749         assertTrue ("setQuote('\\'') failed", "src='images/third'".equals (src.toString ()));
750     }
751     
752     /**
753      * see bug #979893 Not Parsing all Attributes
754      */

755     public void testNoSpace () throws ParserException
756     {
757         String JavaDoc id = "A19012_00002";
758         String JavaDoc rawid = "\"" + id + "\"";
759         String JavaDoc cls = "BuyLink";
760         String JavaDoc rawcls = "\"" + cls + "\"";
761         String JavaDoc href = "http://www.someplace.com/buyme.html";
762         String JavaDoc rawhref = "\"" + href + "\"";
763         String JavaDoc html = "<a id=" + rawid + /* no space */ "class=" + rawcls + " HREF=" + rawhref + ">Pick me.</a>";
764         createParser (html);
765         parseAndAssertNodeCount (1);
766         assertTrue ("Node should be an LinkTag", node[0] instanceof LinkTag);
767         LinkTag link = (LinkTag)node[0];
768         Vector JavaDoc attributes = link.getAttributesEx ();
769         assertEquals ("Incorrect number of attributes", 6, attributes.size ());
770         assertStringEquals ("id wrong", rawid, link.getAttributeEx ("id").getRawValue ());
771         assertStringEquals ("class wrong", rawcls, link.getAttributeEx ("class").getRawValue ());
772         assertStringEquals ("href wrong", rawhref, link.getAttributeEx ("href").getRawValue ());
773         assertStringEquals ("id wrong", id, link.getAttributeEx ("id").getValue ());
774         assertStringEquals ("class wrong", cls, link.getAttributeEx ("class").getValue ());
775         assertStringEquals ("href wrong", href, link.getAttributeEx ("href").getValue ());
776     }
777 }
778
Popular Tags