1 27 package org.htmlparser.tests.parserHelperTests; 28 29 import org.htmlparser.PrototypicalNodeFactory; 30 import org.htmlparser.Remark; 31 import org.htmlparser.Tag; 32 import org.htmlparser.Text; 33 import org.htmlparser.tests.ParserTestCase; 34 import org.htmlparser.util.ParserException; 35 36 public class RemarkNodeParserTest extends ParserTestCase 37 { 38 static 39 { 40 System.setProperty ("org.htmlparser.tests.parserHelperTests.RemarkParserTest", "RemarkParserTest"); 41 } 42 43 public RemarkNodeParserTest (String name) { 44 super(name); 45 } 46 47 63 public void testRemarkBug() throws ParserException 64 { 65 createParser( 66 "<!-- saved from url=(0022)http://internet.e-mail -->\n"+ 67 "<HTML>\n"+ 68 "<HEAD><META name=\"title\" content=\"Training Introduction\">\n"+ 69 "<META name=\"subject\" content=\"\">\n"+ 70 "<!--\n"+ 71 " Whats gonna happen now ?\n"+ 72 "-->\n"+ 73 "<TEST>\n"+ 74 "</TEST>\n"); 75 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 76 parseAndAssertNodeCount(15); 77 assertTrue("First node should be a Remark",node[0] instanceof Remark); 79 Remark Remark = (Remark)node[0]; 80 assertEquals("Text of the Remark #1"," saved from url=(0022)http://internet.e-mail ",Remark.getText()); 81 assertTrue("Tenth node should be a Remark",node[9] instanceof Remark); 83 Remark = (Remark)node[9]; 84 assertEquals("Text of the Remark #10","\n Whats gonna happen now ?\n",Remark.getText()); 85 } 86 87 public void testToPlainTextString() throws ParserException { 88 createParser( 89 "<!-- saved from url=(0022)http://internet.e-mail -->\n"+ 90 "<HTML>\n"+ 91 "<HEAD><META name=\"title\" content=\"Training Introduction\">\n"+ 92 "<META name=\"subject\" content=\"\">\n"+ 93 "<!--\n"+ 94 " Whats gonna happen now ?\n"+ 95 "-->\n"+ 96 "<TEST>\n"+ 97 "</TEST>\n"); 98 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 99 parseAndAssertNodeCount(15); 100 assertTrue("First node should be a Remark",node[0] instanceof Remark); 102 Remark Remark = (Remark)node[0]; 103 assertEquals("Plain Text of the Remark #1"," saved from url=(0022)http://internet.e-mail ",Remark.toPlainTextString()); 104 assertTrue("Tenth node should be a Remark",node[9] instanceof Remark); 106 Remark = (Remark)node[9]; 107 assertEquals("Plain Text of the Remark #10","\n Whats gonna happen now ?\n",Remark.getText()); 108 109 } 110 111 public void testToRawString() throws ParserException { 112 createParser( 113 "<!-- saved from url=(0022)http://internet.e-mail -->\n"+ 114 "<HTML>\n"+ 115 "<HEAD><META name=\"title\" content=\"Training Introduction\">\n"+ 116 "<META name=\"subject\" content=\"\">\n"+ 117 "<!--\n"+ 118 " Whats gonna happen now ?\n"+ 119 "-->\n"+ 120 "<TEST>\n"+ 121 "</TEST>\n"); 122 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 123 parseAndAssertNodeCount(15); 124 assertTrue("First node should be a Remark",node[0] instanceof Remark); 126 Remark Remark = (Remark)node[0]; 127 assertStringEquals("Raw String of the Remark #1","<!-- saved from url=(0022)http://internet.e-mail -->",Remark.toHtml()); 128 assertTrue("Tenth node should be a Remark",node[9] instanceof Remark); 130 Remark = (Remark)node[9]; 131 assertStringEquals("Raw String of the Remark #6","<!--\n Whats gonna happen now ?\n-->",Remark.toHtml()); 132 } 133 134 public void testNonRemark() throws ParserException { 135 createParser(" <![endif]>"); 136 parseAndAssertNodeCount(2); 137 assertTrue("First node should be a string node",node[0] instanceof Text); 139 assertTrue("Second node should be a Tag",node[1] instanceof Tag); 140 Text stringNode = (Text)node[0]; 141 Tag tag = (Tag)node[1]; 142 assertEquals("Text contents"," ",stringNode.getText()); 143 assertEquals("Tag Contents","![endif]",tag.getText()); 144 145 } 146 147 153 public void testRemarkWithBlankLine() throws ParserException { 154 createParser("<!--\n"+ 155 "\n"+ 156 "-->"); 157 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 158 parseAndAssertNodeCount(1); 159 assertTrue("Node should be a Remark",node[0] instanceof Remark); 160 Remark Remark = (Remark)node[0]; 161 assertEquals("Expected contents","\n\n",Remark.getText()); 162 163 } 164 165 170 public void testRemarkWithNothing() throws ParserException { 171 createParser("<!-->"); 172 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 173 parseAndAssertNodeCount(1); 174 assertTrue("Node should be a Remark",node[0] instanceof Remark); 175 Remark Remark = (Remark)node[0]; 176 assertEquals("Expected contents","",Remark.getText()); 177 178 } 179 180 187 public void testTagWithinRemark() throws ParserException { 188 createParser("<!-- \n"+ 189 "<A>\n"+ 190 "bcd -->"); 191 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 192 parseAndAssertNodeCount(1); 193 assertTrue("Node should be a Remark",node[0] instanceof Remark); 194 Remark Remark = (Remark)node[0]; 195 assertStringEquals("Expected contents"," \n<A>\nbcd ",Remark.getText()); 196 197 } 198 199 207 public void testInvalidTag() throws ParserException { 208 createParser("<!\n"+ 209 "-\n"+ 210 "-\n"+ 211 "ssd -->"); 212 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 213 parseAndAssertNodeCount(1); 214 assertTrue("Node should be a Tag but was "+node[0],node[0] instanceof Tag); 215 Tag tag = (Tag)node[0]; 216 assertStringEquals("Expected contents","!\n"+ 217 "-\n"+ 218 "-\n"+ 219 "ssd --",tag.getText()); 220 } 221 222 226 public void testDashesInComment() throws ParserException{ 227 createParser("<!-- -- -->"); 228 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 229 parseAndAssertNodeCount(1); 230 assertTrue("Node should be a Remark but was "+node[0],node[0] instanceof Remark); 231 Remark Remark = (Remark)node[0]; 232 assertEquals("Remark Node contents"," -- ",Remark.getText()); 233 } 234 235 236 258 261 public void testSingleComment () 262 throws 263 ParserException 264 { 265 createParser( 266 "<HTML>\n" 267 + "<HEAD>\n" 268 + "<TITLE>HTML Comment Test</TITLE>\n" 269 + "</HEAD>\n" 270 + "<BODY>\n" 271 + "<!-- Id: html-sgml.sgm,v 1.5 1995/05/26 21:29:50 connolly Exp -->\n" 272 + "</BODY>\n" 273 + "</HTML>\n" 274 ); 275 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 276 parseAndAssertNodeCount(18); 277 assertTrue("Node should be a Remark but was "+node[12],node[12] instanceof Remark); 278 Remark Remark = (Remark)node[12]; 279 assertEquals("Remark Node contents"," Id: html-sgml.sgm,v 1.5 1995/05/26 21:29:50 connolly Exp ",Remark.getText()); 280 } 281 282 285 public void testDoubleComment () 286 throws 287 ParserException 288 { 289 createParser( 290 "<HTML>\n" 291 + "<HEAD>\n" 292 + "<TITLE>HTML Comment Test</TITLE>\n" 293 + "</HEAD>\n" 294 + "<BODY>\n" 295 + "<!-- another -- -- comment -->\n" 296 + "</BODY>\n" 297 + "</HTML>\n" 298 ); 299 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 300 parseAndAssertNodeCount(18); 301 assertTrue("Node should be a Remark but was "+node[12],node[12] instanceof Remark); 302 Remark Remark = (Remark)node[12]; 303 assertEquals("Remark Node contents"," another -- -- comment ",Remark.getText()); 304 } 305 306 309 public void testEmptyComment () 310 throws 311 ParserException 312 { 313 createParser( 314 "<HTML>\n" 315 + "<HEAD>\n" 316 + "<TITLE>HTML Comment Test 'testEmptyComment'</TITLE>\n" 317 + "</HEAD>\n" 318 + "<BODY>\n" 319 + "<!>\n" 320 + "</BODY>\n" 321 + "</HTML>\n" 322 ); 323 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 324 parseAndAssertNodeCount(18); 325 assertTrue("Node should be a Remark but was "+node[12],node[12] instanceof Remark); 326 Remark Remark = (Remark)node[12]; 327 assertEquals("Remark Node contents","",Remark.getText()); 328 } 329 330 357 362 public void testExclamationComment () 363 throws 364 ParserException 365 { 366 createParser ( 367 "<html>\n" 368 + "<head>\n" 369 + "<title>foobar</title>\n" 370 + "</head>\n" 371 + "<body>\n" 372 + "<!-- foobar --!>\n" 373 + "</body>\n" 374 + "</html>\n" 375 ); 376 parser.setNodeFactory (new PrototypicalNodeFactory (true)); 377 parseAndAssertNodeCount (18); 378 assertTrue("Node should be a Remark but was " + node[12], node[12] instanceof Remark); 379 assertStringEquals ("remark text", "<!-- foobar --!>", node[12].toHtml ()); 380 } 381 382 } 383 | Popular Tags |