KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > visitorsTests > TextExtractingVisitorTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/visitorsTests/TextExtractingVisitorTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:57 $
10
// $Revision: 1.15 $
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.visitorsTests;
28
29 import org.htmlparser.tests.ParserTestCase;
30 import org.htmlparser.visitors.TextExtractingVisitor;
31
32 public class TextExtractingVisitorTest extends ParserTestCase {
33
34     static
35     {
36         System.setProperty ("org.htmlparser.tests.visitorsTests.TextExtractingVisitorTest", "TextExtractingVisitorTest");
37     }
38
39     public TextExtractingVisitorTest(String JavaDoc name) {
40         super(name);
41     }
42
43     public void testSimpleVisit() throws Exception JavaDoc {
44         createParser("<HTML><HEAD><TITLE>Hello World</TITLE></HEAD></HTML>");
45         TextExtractingVisitor visitor = new TextExtractingVisitor();
46         parser.visitAllNodesWith(visitor);
47         assertStringEquals(
48             "extracted text",
49             "Hello World",
50             visitor.getExtractedText()
51         );
52     }
53
54     public void testSimpleVisitWithRegisteredScanners() throws Exception JavaDoc {
55         createParser("<HTML><HEAD><TITLE>Hello World</TITLE></HEAD></HTML>");
56         TextExtractingVisitor visitor = new TextExtractingVisitor();
57         parser.visitAllNodesWith(visitor);
58         assertStringEquals(
59             "extracted text",
60             "Hello World",
61             visitor.getExtractedText()
62         );
63     }
64
65     public void testVisitHtmlWithSpecialChars() throws Exception JavaDoc {
66         createParser("<BODY>Hello World&nbsp;&nbsp;</BODY>");
67         TextExtractingVisitor visitor = new TextExtractingVisitor();
68         parser.visitAllNodesWith(visitor);
69         assertStringEquals(
70             "extracted text",
71             "Hello World ",
72             visitor.getExtractedText()
73         );
74     }
75
76     public void testVisitHtmlWithPreTags() throws Exception JavaDoc {
77         createParser(
78             "Some text with &nbsp;<pre>this &nbsp; should be preserved</pre>"
79         );
80         TextExtractingVisitor visitor = new TextExtractingVisitor();
81         parser.visitAllNodesWith(visitor);
82         assertStringEquals(
83             "extracted text",
84             "Some text with this &nbsp; should be preserved",
85             visitor.getExtractedText()
86         );
87     }
88 }
89
Popular Tags