1 27 package org.htmlparser.tests.utilTests; 28 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.File ; 34 import java.io.FileWriter ; 35 import java.io.IOException ; 36 import java.io.ObjectInputStream ; 37 import java.io.ObjectOutputStream ; 38 import java.io.PrintWriter ; 39 import java.net.URL ; 40 import java.util.Vector ; 41 42 import org.htmlparser.Node; 43 import org.htmlparser.Parser; 44 import org.htmlparser.beans.LinkBean; 45 import org.htmlparser.beans.StringBean; 46 import org.htmlparser.lexer.Lexer; 47 import org.htmlparser.lexer.Page; 48 import org.htmlparser.tests.*; 49 import org.htmlparser.util.NodeIterator; 50 import org.htmlparser.util.ParserException; 51 52 public class BeanTest extends ParserTestCase 53 { 54 static 55 { 56 System.setProperty ("org.htmlparser.tests.utilTests.BeanTest", "BeanTest"); 57 } 58 59 public BeanTest (String name) 60 { 61 super (name); 62 } 63 64 protected byte[] pickle (Object object) 65 throws 66 IOException 67 { 68 ByteArrayOutputStream bos; 69 ObjectOutputStream oos; 70 byte[] ret; 71 72 bos = new ByteArrayOutputStream (); 73 oos = new ObjectOutputStream (bos); 74 oos.writeObject (object); 75 oos.close (); 76 ret = bos.toByteArray (); 77 78 return (ret); 79 } 80 81 protected Object unpickle (byte[] data) 82 throws 83 IOException , 84 ClassNotFoundException 85 { 86 ByteArrayInputStream bis; 87 ObjectInputStream ois; 88 Object ret; 89 90 bis = new ByteArrayInputStream (data); 91 ois = new ObjectInputStream (bis); 92 ret = ois.readObject (); 93 ois.close (); 94 95 return (ret); 96 } 97 98 101 protected void check (StringBean bean, String html, String text) 102 { 103 String path; 104 File file; 105 PrintWriter out; 106 String string; 107 108 path = System.getProperty ("user.dir"); 109 if (!path.endsWith (File.separator)) 110 path += File.separator; 111 file = new File (path + "delete_me.html"); 112 try 113 { 114 out = new PrintWriter (new FileWriter (file)); 115 out.print (html); 116 out.close (); 117 bean.setURL (file.getAbsolutePath ()); 118 string = bean.getStrings (); 119 } 120 catch (Exception e) 121 { 122 fail (e.toString ()); 123 string = null; } 125 finally 126 { 127 file.delete (); 128 } 129 assertStringEquals ("stringbean text differs", text, string); 130 } 131 132 public void testZeroArgPageConstructor () 133 throws 134 IOException , 135 ClassNotFoundException , 136 ParserException 137 { 138 Page page; 139 byte[] data; 140 141 page = new Page (); 142 data = pickle (page); 143 page = (Page)unpickle (data); 144 } 145 146 public void testZeroArgLexerConstructor () 147 throws 148 IOException , 149 ClassNotFoundException , 150 ParserException 151 { 152 Lexer lexer; 153 byte[] data; 154 155 lexer = new Lexer (); 156 data = pickle (lexer); 157 lexer = (Lexer)unpickle (data); 158 } 159 160 public void testZeroArgParserConstructor () 161 throws 162 IOException , 163 ClassNotFoundException , 164 ParserException 165 { 166 Parser parser; 167 byte[] data; 168 169 parser = new Parser (); 170 data = pickle (parser); 171 parser = (Parser)unpickle (data); 172 } 173 174 public void testSerializable () 175 throws 176 IOException , 177 ClassNotFoundException , 178 ParserException 179 { 180 Parser parser; 181 Vector vector; 182 NodeIterator enumeration; 183 byte[] data; 184 185 parser = new Parser ("http://htmlparser.sourceforge.net/test/example.html"); 186 enumeration = parser.elements (); 187 vector = new Vector (50); 188 while (enumeration.hasMoreNodes ()) 189 vector.addElement (enumeration.nextNode ()); 190 191 data = pickle (parser); 192 parser = (Parser)unpickle (data); 193 194 enumeration = parser.elements (); 195 while (enumeration.hasMoreNodes ()) 196 assertEquals ( 197 "Nodes before and after serialization differ", 198 ((Node)vector.remove (0)).toHtml (), 199 enumeration.nextNode ().toHtml ()); 200 } 201 202 public void testSerializableScanners () 203 throws 204 IOException , 205 ClassNotFoundException , 206 ParserException 207 { 208 Parser parser; 209 Vector vector; 210 NodeIterator enumeration; 211 byte[] data; 212 213 parser = new Parser ("http://htmlparser.sourceforge.net/test/example.html"); 214 enumeration = parser.elements (); 215 vector = new Vector (50); 216 while (enumeration.hasMoreNodes ()) 217 vector.addElement (enumeration.nextNode ()); 218 219 data = pickle (parser); 220 parser = (Parser)unpickle (data); 221 222 enumeration = parser.elements (); 223 while (enumeration.hasMoreNodes ()) 224 assertEquals ( 225 "Nodes before and after serialization differ", 226 ((Node)vector.remove (0)).toHtml (), 227 enumeration.nextNode ().toHtml ()); 228 } 229 230 public void testSerializableStringBean () 231 throws 232 IOException , 233 ClassNotFoundException , 234 ParserException 235 { 236 StringBean sb; 237 String text; 238 byte[] data; 239 240 sb = new StringBean (); 241 sb.setURL ("http://htmlparser.sourceforge.net/test/example.html"); 242 text = sb.getStrings (); 243 244 data = pickle (sb); 245 sb = (StringBean)unpickle (data); 246 247 assertEquals ( 248 "Strings before and after serialization differ", 249 text, 250 sb.getStrings ()); 251 } 252 253 public void testSerializableLinkBean () 254 throws 255 IOException , 256 ClassNotFoundException , 257 ParserException 258 { 259 LinkBean lb; 260 URL [] links; 261 byte[] data; 262 URL [] links2; 263 264 lb = new LinkBean (); 265 lb.setURL ("http://htmlparser.sourceforge.net/test/example.html"); 266 links = lb.getLinks (); 267 268 data = pickle (lb); 269 lb = (LinkBean)unpickle (data); 270 271 links2 = lb.getLinks (); 272 assertEquals ("Number of links after serialization differs", links.length, links2.length); 273 for (int i = 0; i < links.length; i++) 274 { 275 assertEquals ( 276 "Links before and after serialization differ", 277 links[i], 278 links2[i]); 279 } 280 } 281 282 public void testStringBeanListener () 283 { 284 final StringBean sb; 285 final Boolean hit[] = new Boolean [1]; 286 287 sb = new StringBean (); 288 hit[0] = Boolean.FALSE; 289 sb.addPropertyChangeListener ( 290 new PropertyChangeListener () 291 { 292 public void propertyChange (PropertyChangeEvent event) 293 { 294 if (event.getSource ().equals (sb)) 295 if (event.getPropertyName ().equals (StringBean.PROP_STRINGS_PROPERTY)) 296 hit[0] = Boolean.TRUE; 297 } 298 }); 299 300 hit[0] = Boolean.FALSE; 301 sb.setURL ("http://htmlparser.sourceforge.net/test/example.html"); 302 assertTrue ( 303 "Strings property change not fired for URL change", 304 hit[0].booleanValue ()); 305 306 hit[0] = Boolean.FALSE; 307 sb.setLinks (true); 308 assertTrue ( 309 "Strings property change not fired for links change", 310 hit[0].booleanValue ()); 311 } 312 313 public void testLinkBeanListener () 314 { 315 final LinkBean lb; 316 final Boolean hit[] = new Boolean [1]; 317 318 lb = new LinkBean (); 319 hit[0] = Boolean.FALSE; 320 lb.addPropertyChangeListener ( 321 new PropertyChangeListener () 322 { 323 public void propertyChange (PropertyChangeEvent event) 324 { 325 if (event.getSource ().equals (lb)) 326 if (event.getPropertyName ().equals (LinkBean.PROP_LINKS_PROPERTY)) 327 hit[0] = Boolean.TRUE; 328 } 329 }); 330 331 hit[0] = Boolean.FALSE; 332 lb.setURL ("http://htmlparser.sourceforge.net/test/example.html"); 333 assertTrue ( 334 "Links property change not fired for URL change", 335 hit[0].booleanValue ()); 336 } 337 338 341 public void testCollapsed1 () 342 { 343 StringBean sb; 344 345 sb = new StringBean (); 346 sb.setLinks (false); 347 sb.setReplaceNonBreakingSpaces (true); 348 sb.setCollapse (false); 349 check (sb, "<html><head></head><body></body></html>", ""); 350 check (sb, "<html><head></head><body> </body></html>", " "); 351 check (sb, "<html><head></head><body>\t</body></html>", "\t"); 352 sb.setCollapse (true); 353 check (sb, "<html><head></head><body></body></html>", ""); 354 check (sb, "<html><head></head><body> </body></html>", ""); 355 check (sb, "<html><head></head><body>\t</body></html>", ""); 356 } 357 358 361 public void testCollapsed2 () 362 { 363 StringBean sb; 364 365 sb = new StringBean (); 366 sb.setLinks (false); 367 sb.setReplaceNonBreakingSpaces (true); 368 sb.setCollapse (false); 369 check (sb, "<html><head></head><body> </body></html>", " "); 370 check (sb, "<html><head></head><body>\t\t</body></html>", "\t\t"); 371 check (sb, "<html><head></head><body> \t\t</body></html>", " \t\t"); 372 check (sb, "<html><head></head><body>\t \t</body></html>", "\t \t"); 373 check (sb, "<html><head></head><body>\t\t </body></html>", "\t\t "); 374 sb.setCollapse (true); 375 check (sb, "<html><head></head><body> </body></html>", ""); 376 check (sb, "<html><head></head><body>\t\t</body></html>", ""); 377 check (sb, "<html><head></head><body> \t\t</body></html>", ""); 378 check (sb, "<html><head></head><body>\t \t</body></html>", ""); 379 check (sb, "<html><head></head><body>\t\t </body></html>", ""); 380 } 381 382 385 public void testCollapsed3 () 386 { 387 StringBean sb; 388 389 sb = new StringBean (); 390 sb.setLinks (false); 391 sb.setReplaceNonBreakingSpaces (true); 392 sb.setCollapse (false); 393 check (sb, "<html><head></head><body>x </body></html>", "x "); 394 check (sb, "<html><head></head><body>x\t\t</body></html>", "x\t\t"); 395 check (sb, "<html><head></head><body>x \t\t</body></html>", "x \t\t"); 396 check (sb, "<html><head></head><body>x\t \t</body></html>", "x\t \t"); 397 check (sb, "<html><head></head><body>x\t\t </body></html>", "x\t\t "); 398 sb.setCollapse (true); 399 check (sb, "<html><head></head><body>x </body></html>", "x"); 400 check (sb, "<html><head></head><body>x\t\t</body></html>", "x"); 401 check (sb, "<html><head></head><body>x \t\t</body></html>", "x"); 402 check (sb, "<html><head></head><body>x\t \t</body></html>", "x"); 403 check (sb, "<html><head></head><body>x\t\t </body></html>", "x"); 404 check (sb, "<html><head></head><body> x</body></html>", "x"); 405 check (sb, "<html><head></head><body>\t\tx</body></html>", "x"); 406 check (sb, "<html><head></head><body> \t\tx</body></html>", "x"); 407 check (sb, "<html><head></head><body>\t \tx</body></html>", "x"); 408 check (sb, "<html><head></head><body>\t\t x</body></html>", "x"); 409 } 410 411 414 public void testOutputWithPreTags() { 415 StringBean sb; 416 sb = new StringBean (); 417 String sampleCode = "public class Product {}"; 418 check (sb, "<body><pre>"+sampleCode+"</pre></body>", sampleCode); 419 } 420 421 424 public void testOutputWithScriptTags() { 425 StringBean sb; 426 sb = new StringBean (); 427 428 String sampleScript = 429 "<script language=\"javascript\">\r\n" 430 + "if(navigator.appName.indexOf(\"Netscape\") != -1)\r\n" 431 + " document.write ('xxx');\r\n" 432 + "else\r\n" 433 + " document.write ('yyy');\r\n" 434 + "</script>\r\n"; 435 436 check (sb, "<body>"+sampleScript+"</body>", ""); 437 } 438 439 442 public void testOutputWithPreAndAnyTag() 443 { 444 StringBean sb; 445 446 sb = new StringBean (); 447 sb.setLinks (false); 448 sb.setReplaceNonBreakingSpaces (true); 449 sb.setCollapse (false); 450 check (sb, "<html><head></head><body><pre><hello></pre></body></html>", ""); 451 } 452 453 456 public void testOutputWithPreAndAnyTagPlusText() 457 { 458 StringBean sb; 459 460 sb = new StringBean (); 461 sb.setLinks (false); 462 sb.setReplaceNonBreakingSpaces (true); 463 sb.setCollapse (false); 464 check (sb, "<html><head></head><body><pre><hello>dogfood</hello></pre></body></html>", "dogfood"); 465 } 466 467 470 public void testOutputWithPreAndAnyTagPlusTextWithWhitespace() 471 { 472 StringBean sb; 473 474 sb = new StringBean (); 475 sb.setLinks (false); 476 sb.setReplaceNonBreakingSpaces (true); 477 sb.setCollapse (true); 478 check (sb, "<html><head></head><body><pre><hello>dog food</hello></pre></body></html>", "dog food"); 479 } 480 481 484 public void testOutputWithoutPreAndAnyTagPlusTextWithWhitespace() 485 { 486 StringBean sb; 487 488 sb = new StringBean (); 489 sb.setLinks (false); 490 sb.setReplaceNonBreakingSpaces (true); 491 sb.setCollapse (true); 492 check (sb, "<html><head></head><body><hello>dog food</hello></body></html>", "dog food"); 493 } 494 495 498 public void xtestOutputWithPreAndScriptTags() { 499 StringBean sb; 500 sb = new StringBean (); 501 502 String sampleScript = 503 "<script language=\"javascript\">\r\n" 504 + "if(navigator.appName.indexOf(\"Netscape\") != -1)\r\n" 505 + " document.write ('xxx');\r\n" 506 + "else\r\n" 507 + " document.write ('yyy');\r\n" 508 + "</script>\r\n"; 509 510 check (sb, "<body><pre>"+sampleScript+"</pre></body>", sampleScript); 511 } 512 513 } 514 515 | Popular Tags |