1 19 20 33 package org.htmlparser.tests.parserHelperTests; 34 35 import java.util.Hashtable ; 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 table; 48 49 public AttributeParserTest(String name) 50 { 51 super(name); 52 } 53 54 protected void setUp() 55 { 56 parser = new AttributeParser(); 57 } 58 59 public void getParameterTableFor(String 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 120 public void testIncorrectSpaceKeyBug() 121 { 122 getParameterTableFor("TEXTAREA name=\"Remarks\" "); 123 assertEquals("There should only be two keys", 2, table.size()); 125 String key1 = "NAME"; 127 String value1 = (String ) table.get(key1); 128 assertEquals("Expected value 1", "Remarks", value1); 129 String 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 ) 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 ) table.get("HREF")); 156 assertStringEquals("tag name", "A", (String ) table.get(Tag.TAGNAME)); 157 } 158 159 168 public void testEmptyTag() 169 { 170 getParameterTableFor(""); 171 assertNotNull("No Tag.TAGNAME", table.get(Tag.TAGNAME)); 172 } 173 174 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 ) table.get("HREF")); 192 } 193 } 194 195 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 name = (String ) table.get(Tag.TAGNAME); 209 assertNotNull("No Tag.TAGNAME", name); 210 assertStringEquals("tag name parsed incorrectly", "BODY", name); 211 String value = (String ) table.get("ONLOAD"); 212 assertStringEquals( 213 "parameter parsed incorrectly", 214 "defaultStatus=''", 215 value); 216 } 217 } 218 } 219 | Popular Tags |