KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > utilTests > BeanTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests/BeanTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:32 $
10
// $Revision: 1.50 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests.utilTests;
28
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileWriter JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37 import java.io.ObjectOutputStream JavaDoc;
38 import java.io.PrintWriter JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.util.Vector JavaDoc;
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 JavaDoc name)
60     {
61         super (name);
62     }
63
64     protected byte[] pickle (Object JavaDoc object)
65         throws
66             IOException JavaDoc
67     {
68         ByteArrayOutputStream JavaDoc bos;
69         ObjectOutputStream JavaDoc oos;
70         byte[] ret;
71
72         bos = new ByteArrayOutputStream JavaDoc ();
73         oos = new ObjectOutputStream JavaDoc (bos);
74         oos.writeObject (object);
75         oos.close ();
76         ret = bos.toByteArray ();
77
78         return (ret);
79     }
80
81     protected Object JavaDoc unpickle (byte[] data)
82         throws
83             IOException JavaDoc,
84             ClassNotFoundException JavaDoc
85     {
86         ByteArrayInputStream JavaDoc bis;
87         ObjectInputStream JavaDoc ois;
88         Object JavaDoc ret;
89
90         bis = new ByteArrayInputStream JavaDoc (data);
91         ois = new ObjectInputStream JavaDoc (bis);
92         ret = ois.readObject ();
93         ois.close ();
94
95         return (ret);
96     }
97
98     /**
99      * Makes sure that the bean returns text when passed the html.
100      */

101     protected void check (StringBean bean, String JavaDoc html, String JavaDoc text)
102     {
103         String JavaDoc path;
104         File JavaDoc file;
105         PrintWriter JavaDoc out;
106         String JavaDoc string;
107
108         path = System.getProperty ("user.dir");
109         if (!path.endsWith (File.separator))
110             path += File.separator;
111         file = new File JavaDoc (path + "delete_me.html");
112         try
113         {
114             out = new PrintWriter JavaDoc (new FileWriter JavaDoc (file));
115             out.print (html);
116             out.close ();
117             bean.setURL (file.getAbsolutePath ());
118             string = bean.getStrings ();
119         }
120         catch (Exception JavaDoc e)
121         {
122             fail (e.toString ());
123             string = null; // never reached
124
}
125         finally
126         {
127             file.delete ();
128         }
129         assertStringEquals ("stringbean text differs", text, string);
130     }
131
132     public void testZeroArgPageConstructor ()
133         throws
134             IOException JavaDoc,
135             ClassNotFoundException JavaDoc,
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 JavaDoc,
149             ClassNotFoundException JavaDoc,
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 JavaDoc,
163             ClassNotFoundException JavaDoc,
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 JavaDoc,
177             ClassNotFoundException JavaDoc,
178             ParserException
179     {
180         Parser parser;
181         Vector JavaDoc 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 JavaDoc (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 JavaDoc,
205             ClassNotFoundException JavaDoc,
206             ParserException
207     {
208         Parser parser;
209         Vector JavaDoc 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 JavaDoc (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 JavaDoc,
233             ClassNotFoundException JavaDoc,
234             ParserException
235     {
236         StringBean sb;
237         String JavaDoc 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 JavaDoc,
256             ClassNotFoundException JavaDoc,
257             ParserException
258     {
259         LinkBean lb;
260         URL JavaDoc[] links;
261         byte[] data;
262         URL JavaDoc[] 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 JavaDoc hit[] = new Boolean JavaDoc[1];
286
287         sb = new StringBean ();
288         hit[0] = Boolean.FALSE;
289         sb.addPropertyChangeListener (
290             new PropertyChangeListener JavaDoc ()
291             {
292                 public void propertyChange (PropertyChangeEvent JavaDoc 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 JavaDoc hit[] = new Boolean JavaDoc[1];
317
318         lb = new LinkBean ();
319         hit[0] = Boolean.FALSE;
320         lb.addPropertyChangeListener (
321             new PropertyChangeListener JavaDoc ()
322             {
323                 public void propertyChange (PropertyChangeEvent JavaDoc 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     /**
339      * Test no text returns empty string.
340      */

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     /**
359      * Test multiple whitespace returns empty string.
360      */

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     /**
383      * Test text preceded or followed by whitespace returns just text.
384      */

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     /**
412      * Test text including a "pre" tag
413      */

414     public void testOutputWithPreTags() {
415         StringBean sb;
416         sb = new StringBean ();
417         String JavaDoc sampleCode = "public class Product {}";
418         check (sb, "<body><pre>"+sampleCode+"</pre></body>", sampleCode);
419     }
420
421     /**
422      * Test text including a "script" tag
423      */

424     public void testOutputWithScriptTags() {
425         StringBean sb;
426         sb = new StringBean ();
427
428         String JavaDoc 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     /*
440      * Test output with pre and any tag.
441      */

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     /*
454      * Test output with pre and any tag and text.
455      */

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     /*
468      * Test output with pre and any tag and text.
469      */

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     /*
482      * Test output without pre and any tag and text.
483      */

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     /**
496      * Test output with pre and script tags
497      */

498     public void xtestOutputWithPreAndScriptTags() {
499         StringBean sb;
500         sb = new StringBean ();
501
502         String JavaDoc 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