KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > parserHelperTests > AttributeParserTest


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/parserHelperTests/AttributeParserTest.java,v 1.2 2004/02/10 13:41:10 woolfel Exp $
2
/*
3  * ====================================================================
4  * Copyright 2002-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20 // The developers of JMeter and Apache are greatful to the developers
21
// of HTMLParser for giving Apache Software Foundation a non-exclusive
22
// license. The performance benefits of HTMLParser are clear and the
23
// users of JMeter will benefit from the hard work the HTMLParser
24
// team. For detailed information about HTMLParser, the project is
25
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
26
//
27
// HTMLParser was originally created by Somik Raha in 2000. Since then
28
// a healthy community of users has formed and helped refine the
29
// design so that it is able to tackle the difficult task of parsing
30
// dirty HTML. Derrick Oswald is the current lead developer and was kind
31
// enough to assist JMeter.
32

33 package org.htmlparser.tests.parserHelperTests;
34
35 import java.util.Hashtable JavaDoc;
36
37 import org.htmlparser.Parser;
38 import org.htmlparser.parserHelper.AttributeParser;
39 import org.htmlparser.tags.Tag;
40 import org.htmlparser.tags.data.TagData;
41 import org.htmlparser.tests.ParserTestCase;
42
43 public class AttributeParserTest extends ParserTestCase
44 {
45     private AttributeParser parser;
46     private Tag tag;
47     private Hashtable JavaDoc table;
48
49     public AttributeParserTest(String JavaDoc name)
50     {
51         super(name);
52     }
53
54     protected void setUp()
55     {
56         parser = new AttributeParser();
57     }
58
59     public void getParameterTableFor(String JavaDoc tagContents)
60     {
61         tag = new Tag(new TagData(0, 0, tagContents, ""));
62         table = parser.parseAttributes(tag);
63
64     }
65
66     public void testParseParameters()
67     {
68         getParameterTableFor("a b = \"c\"");
69         assertEquals("Value", "c", table.get("B"));
70     }
71
72     public void testParseTokenValues()
73     {
74         getParameterTableFor("a b = \"'\"");
75         assertEquals("Value", "'", table.get("B"));
76     }
77
78     public void testParseEmptyValues()
79     {
80         getParameterTableFor("a b = \"\"");
81         assertEquals("Value", "", table.get("B"));
82     }
83
84     public void testParseMissingEqual()
85     {
86         getParameterTableFor("a b\"c\"");
87         assertEquals("ValueB", "", table.get("B"));
88
89     }
90
91     public void testTwoParams()
92     {
93         getParameterTableFor("PARAM NAME=\"Param1\" VALUE=\"Somik\">\n");
94         assertEquals("Param1", "Param1", table.get("NAME"));
95         assertEquals("Somik", "Somik", table.get("VALUE"));
96     }
97
98     public void testPlainParams()
99     {
100         getParameterTableFor("PARAM NAME=Param1 VALUE=Somik");
101         assertEquals("Param1", "Param1", table.get("NAME"));
102         assertEquals("Somik", "Somik", table.get("VALUE"));
103     }
104
105     public void testValueMissing()
106     {
107         getParameterTableFor("INPUT type=\"checkbox\" name=\"Authorize\" value=\"Y\" checked");
108         assertEquals("Name of Tag", "INPUT", table.get(Tag.TAGNAME));
109         assertEquals("Type", "checkbox", table.get("TYPE"));
110         assertEquals("Name", "Authorize", table.get("NAME"));
111         assertEquals("Value", "Y", table.get("VALUE"));
112         assertEquals("Checked", "", table.get("CHECKED"));
113     }
114
115     /**
116      * This is a simulation of a bug reported by Dhaval Udani - wherein
117      * a space before the end of the tag causes a problem - there is a key
118      * in the table with just a space in it and an empty value
119      */

120     public void testIncorrectSpaceKeyBug()
121     {
122         getParameterTableFor("TEXTAREA name=\"Remarks\" ");
123         // There should only be two keys..
124
assertEquals("There should only be two keys", 2, table.size());
125         // The first key is name
126
String JavaDoc key1 = "NAME";
127         String JavaDoc value1 = (String JavaDoc) table.get(key1);
128         assertEquals("Expected value 1", "Remarks", value1);
129         String JavaDoc key2 = Tag.TAGNAME;
130         assertEquals("Expected Value 2", "TEXTAREA", table.get(key2));
131     }
132
133     public void testNullTag()
134     {
135         getParameterTableFor("INPUT type=");
136         assertEquals("Name of Tag", "INPUT", table.get(Tag.TAGNAME));
137         assertEquals("Type", "", table.get("TYPE"));
138     }
139
140     public void testAttributeWithSpuriousEqualTo()
141     {
142         getParameterTableFor("a class=rlbA HREF=/news/866201.asp?0sl=-32");
143         assertStringEquals(
144             "href",
145             "/news/866201.asp?0sl=-32",
146             (String JavaDoc) table.get("HREF"));
147     }
148
149     public void testQuestionMarksInAttributes()
150     {
151         getParameterTableFor("a HREF=\"mailto:sam@neurogrid.com?subject=Site Comments\"");
152         assertStringEquals(
153             "href",
154             "mailto:sam@neurogrid.com?subject=Site Comments",
155             (String JavaDoc) table.get("HREF"));
156         assertStringEquals("tag name", "A", (String JavaDoc) table.get(Tag.TAGNAME));
157     }
158
159     /**
160      * Believe it or not Moi (vincent_aumont) wants htmlparser to parse a text file
161      * containing something that looks nearly like a tag:
162      * <pre>
163      * " basic_string&lt;char, string_char_traits&lt;char&gt;, &lt;&gt;&gt;::basic_string()"
164      * </pre>
165      * This was throwing a null pointer exception when the empty &lt;&gt; was encountered.
166      * Bug #725420 NPE in StringBean.visitTag
167      **/

168     public void testEmptyTag()
169     {
170         getParameterTableFor("");
171         assertNotNull("No Tag.TAGNAME", table.get(Tag.TAGNAME));
172     }
173
174     /**
175      * Test attributes when they contain scriptlets.
176      * Submitted by Cory Seefurth
177      * See also feature request #725376 Handle script in attributes.
178      * Only perform this test if it's version 1.4 or higher.
179      */

180     public void testJspWithinAttributes()
181     {
182         Parser parser;
183
184         parser = new Parser();
185         if (1.4 <= Parser.getVersionNumber())
186         {
187             getParameterTableFor("a HREF=\"<%=Application(\"sURL\")%>/literature/index.htm");
188             assertStringEquals(
189                 "href",
190                 "<%=Application(\"sURL\")%>/literature/index.htm",
191                 (String JavaDoc) table.get("HREF"));
192         }
193     }
194
195     /**
196      * Test Script in attributes.
197      * See feature request #725376 Handle script in attributes.
198      * Only perform this test if it's version 1.4 or higher.
199      */

200     public void testScriptedTag()
201     {
202         Parser parser;
203
204         parser = new Parser();
205         if (1.4 <= Parser.getVersionNumber())
206         {
207             getParameterTableFor("body onLoad=defaultStatus=''");
208             String JavaDoc name = (String JavaDoc) table.get(Tag.TAGNAME);
209             assertNotNull("No Tag.TAGNAME", name);
210             assertStringEquals("tag name parsed incorrectly", "BODY", name);
211             String JavaDoc value = (String JavaDoc) table.get("ONLOAD");
212             assertStringEquals(
213                 "parameter parsed incorrectly",
214                 "defaultStatus=''",
215                 value);
216         }
217     }
218 }
219
Popular Tags