1 19 20 33 package org.htmlparser.tests.scannersTests; 34 35 import java.util.*; 36 import junit.framework.TestSuite; 37 import org.htmlparser.scanners.LabelScanner; 38 import org.htmlparser.tags.LabelTag; 39 import org.htmlparser.tests.ParserTestCase; 40 import org.htmlparser.util.ParserException; 41 42 public class LabelScannerTest extends ParserTestCase 43 { 44 45 public LabelScannerTest(String name) 46 { 47 super(name); 48 } 49 public void testSimpleLabels() throws ParserException 50 { 51 createParser("<label>This is a label tag</label>"); 52 LabelScanner labelScanner = new LabelScanner("-l"); 53 parser.addScanner(labelScanner); 54 parseAndAssertNodeCount(1); 55 assertTrue(node[0] instanceof LabelTag); 56 LabelTag labelTag = (LabelTag) node[0]; 58 assertEquals( 59 "Label", 60 "This is a label tag", 61 labelTag.getChildrenHTML()); 62 assertEquals("Label", "This is a label tag", labelTag.getLabel()); 63 assertStringEquals( 64 "Label", 65 "<LABEL>This is a label tag</LABEL>", 66 labelTag.toHtml()); 67 assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner()); 68 } 69 70 public void testLabelWithJspTag() throws ParserException 71 { 72 createParser("<label><%=labelValue%></label>"); 73 parser.registerScanners(); 74 LabelScanner labelScanner = new LabelScanner("-l"); 75 parser.addScanner(labelScanner); 76 parseAndAssertNodeCount(1); 77 assertTrue(node[0] instanceof LabelTag); 78 LabelTag labelTag = (LabelTag) node[0]; 80 assertStringEquals( 81 "Label", 82 "<LABEL><%=labelValue%></LABEL>", 83 labelTag.toHtml()); 84 assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner()); 85 } 86 87 public void testLabelWithOtherTags() throws ParserException 88 { 89 createParser("<label><span>Span within label</span></label>"); 90 parser.registerScanners(); 91 LabelScanner labelScanner = new LabelScanner("-l"); 92 parser.addScanner(labelScanner); 93 parseAndAssertNodeCount(1); 94 assertTrue(node[0] instanceof LabelTag); 95 LabelTag labelTag = (LabelTag) node[0]; 97 assertEquals("Label value", "Span within label", labelTag.getLabel()); 98 assertStringEquals( 99 "Label", 100 "<LABEL><SPAN>Span within label</SPAN></LABEL>", 101 labelTag.toHtml()); 102 assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner()); 103 } 104 105 public void testLabelWithManyCompositeTags() throws ParserException 106 { 107 createParser("<label><span>Jane <b> Doe </b> Smith</span></label>"); 108 parser.registerScanners(); 109 LabelScanner labelScanner = new LabelScanner("-l"); 110 parser.addScanner(labelScanner); 111 parseAndAssertNodeCount(1); 112 assertTrue(node[0] instanceof LabelTag); 113 LabelTag labelTag = (LabelTag) node[0]; 114 assertEquals( 115 "Label value", 116 "<SPAN>Jane <B> Doe </B> Smith</SPAN>", 117 labelTag.getChildrenHTML()); 118 assertEquals("Label value", "Jane Doe Smith", labelTag.getLabel()); 119 assertStringEquals( 120 "Label", 121 "<LABEL><SPAN>Jane <B> Doe </B> Smith</SPAN></LABEL>", 122 labelTag.toHtml()); 123 assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner()); 124 } 125 126 public void testLabelsID() throws ParserException 127 { 128 createParser("<label>John Doe</label>"); 129 parser.registerScanners(); 130 LabelScanner labelScanner = new LabelScanner("-l"); 131 parser.addScanner(labelScanner); 132 parseAndAssertNodeCount(1); 133 assertTrue(node[0] instanceof LabelTag); 134 135 LabelTag labelTag = (LabelTag) node[0]; 136 assertStringEquals( 137 "Label", 138 "<LABEL>John Doe</LABEL>", 139 labelTag.toHtml()); 140 Hashtable attr = labelTag.getAttributes(); 141 assertNull("ID", attr.get("id")); 142 } 143 144 public void testNestedLabels() throws ParserException 145 { 146 createParser("<label id=\"attr1\"><label>Jane Doe"); 147 parser.registerScanners(); 148 LabelScanner labelScanner = new LabelScanner("-l"); 149 parser.addScanner(labelScanner); 150 parseAndAssertNodeCount(2); 151 assertTrue(node[0] instanceof LabelTag); 152 assertTrue(node[1] instanceof LabelTag); 153 154 LabelTag labelTag = (LabelTag) node[0]; 155 assertStringEquals( 156 "Label", 157 "<LABEL ID=\"attr1\" ></LABEL>", 158 labelTag.toHtml()); 159 labelTag = (LabelTag) node[1]; 160 assertStringEquals( 161 "Label", 162 "<LABEL>Jane Doe</LABEL>", 163 labelTag.toHtml()); 164 Hashtable attr = labelTag.getAttributes(); 165 assertNull("ID", attr.get("id")); 166 } 167 168 public void testNestedLabels2() throws ParserException 169 { 170 String testHTML = 171 new String ( 172 "<LABEL value=\"Google Search\">Google</LABEL>" 173 + "<LABEL value=\"AltaVista Search\">AltaVista" 174 + "<LABEL value=\"Lycos Search\"></LABEL>" 175 + "<LABEL>Yahoo!</LABEL>" 176 + "<LABEL>\nHotmail</LABEL>" 177 + "<LABEL value=\"ICQ Messenger\">" 178 + "<LABEL>Mailcity\n</LABEL>" 179 + "<LABEL>\nIndiatimes\n</LABEL>" 180 + "<LABEL>\nRediff\n</LABEL>\n" 181 + "<LABEL>Cricinfo" 182 + "<LABEL value=\"Microsoft Passport\">" 183 + "<LABEL value=\"AOL\"><SPAN>AOL</SPAN></LABEL>" 184 + "<LABEL value=\"Time Warner\">Time <B>Warner <SPAN>AOL </SPAN>Inc.</B>"); 185 createParser(testHTML); 186 LabelScanner labelScanner = new LabelScanner("-l"); 188 parser.addScanner(labelScanner); 189 parseAndAssertNodeCount(13); 190 191 198 LabelTag LabelTag; 199 LabelTag = (LabelTag) node[0]; 200 assertStringEquals( 201 "HTML String", 202 "<LABEL VALUE=\"Google Search\">Google</LABEL>", 203 LabelTag.toHtml()); 204 LabelTag = (LabelTag) node[1]; 205 assertStringEquals( 206 "HTML String", 207 "<LABEL VALUE=\"AltaVista Search\">AltaVista</LABEL>", 208 LabelTag.toHtml()); 209 LabelTag = (LabelTag) node[2]; 210 assertStringEquals( 211 "HTML String", 212 "<LABEL VALUE=\"Lycos Search\"></LABEL>", 213 LabelTag.toHtml()); 214 LabelTag = (LabelTag) node[3]; 215 assertStringEquals( 216 "HTML String", 217 "<LABEL>Yahoo!</LABEL>", 218 LabelTag.toHtml()); 219 LabelTag = (LabelTag) node[4]; 220 assertStringEquals( 221 "HTML String", 222 "<LABEL>\r\nHotmail</LABEL>", 223 LabelTag.toHtml()); 224 LabelTag = (LabelTag) node[5]; 225 assertStringEquals( 226 "HTML String", 227 "<LABEL VALUE=\"ICQ Messenger\"></LABEL>", 228 LabelTag.toHtml()); 229 LabelTag = (LabelTag) node[6]; 230 assertStringEquals( 231 "HTML String", 232 "<LABEL>Mailcity\r\n</LABEL>", 233 LabelTag.toHtml()); 234 LabelTag = (LabelTag) node[7]; 235 assertStringEquals( 236 "HTML String", 237 "<LABEL>\r\nIndiatimes\r\n</LABEL>", 238 LabelTag.toHtml()); 239 LabelTag = (LabelTag) node[8]; 240 assertStringEquals( 241 "HTML String", 242 "<LABEL>\r\nRediff\r\n</LABEL>", 243 LabelTag.toHtml()); 244 LabelTag = (LabelTag) node[9]; 245 assertStringEquals( 246 "HTML String", 247 "<LABEL>Cricinfo</LABEL>", 248 LabelTag.toHtml()); 249 LabelTag = (LabelTag) node[10]; 250 assertStringEquals( 251 "HTML String", 252 "<LABEL VALUE=\"Microsoft Passport\"></LABEL>", 253 LabelTag.toHtml()); 254 LabelTag = (LabelTag) node[11]; 255 assertStringEquals( 256 "HTML String", 257 "<LABEL VALUE=\"AOL\"><SPAN>AOL</SPAN></LABEL>", 258 LabelTag.toHtml()); 259 LabelTag = (LabelTag) node[12]; 260 assertStringEquals( 261 "HTML String", 262 "<LABEL VALUE=\"Time Warner\">Time <B>Warner <SPAN>AOL </SPAN>Inc.</B></LABEL>", 263 LabelTag.toHtml()); 264 } 265 266 public static TestSuite suite() 267 { 268 return new TestSuite(LabelScannerTest.class); 269 } 270 271 public static void main(String [] args) 272 { 273 new junit.awtui.TestRunner().start( 274 new String [] { LabelScannerTest.class.getName()}); 275 } 276 277 } 278 | Popular Tags |