KickJava   Java API By Example, From Geeks To Geeks.

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


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/StringFindingVisitorTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:57 $
10
// $Revision: 1.16 $
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.StringFindingVisitor;
31
32 public class StringFindingVisitorTest extends ParserTestCase {
33
34     static
35     {
36         System.setProperty ("org.htmlparser.tests.visitorsTests.StringFindingVisitorTest", "StringFindingVisitorTest");
37     }
38
39     private static final String JavaDoc HTML =
40         "<HTML><HEAD><TITLE>This is the Title</TITLE>" +
41         "</HEAD><BODY>Hello World, this is an excellent parser</BODY></HTML>";
42
43     private static final String JavaDoc HTML_TO_SEARCH =
44         "<HTML><HEAD><TITLE>test</TITLE></HEAD>\n"+
45         "<BODY><H1>This is a test page</H1>\n"+
46         "Writing tests is good for code. Testing is a good\n"+
47         "philosophy. Test driven development is even better.\n";
48
49     public StringFindingVisitorTest(String JavaDoc name) {
50         super(name);
51     }
52
53     public void testSimpleStringFind() throws Exception JavaDoc {
54         createParser(HTML);
55         StringFindingVisitor visitor = new StringFindingVisitor("Hello");
56         parser.visitAllNodesWith(visitor);
57         assertTrue("Hello found", visitor.stringWasFound());
58     }
59
60     public void testStringNotFound() throws Exception JavaDoc {
61         createParser(HTML);
62         StringFindingVisitor visitor = new StringFindingVisitor("industrial logic");
63         parser.visitAllNodesWith(visitor);
64         assertTrue("industrial logic should not have been found", !visitor.stringWasFound());
65     }
66
67     public void testStringInTagNotFound() throws Exception JavaDoc {
68         createParser(HTML);
69         StringFindingVisitor visitor = new StringFindingVisitor("HTML");
70         parser.visitAllNodesWith(visitor);
71         assertTrue("HTML should not have been found", !visitor.stringWasFound());
72     }
73
74     public void testStringFoundInSingleStringNode() throws Exception JavaDoc {
75         createParser("this is some text!");
76         StringFindingVisitor visitor = new StringFindingVisitor("text");
77         parser.visitAllNodesWith(visitor);
78         assertTrue("text should be found", visitor.stringWasFound());
79     }
80
81     public void testStringFoundCount() throws Exception JavaDoc {
82         createParser(HTML);
83         StringFindingVisitor visitor = new StringFindingVisitor("is");
84         parser.visitAllNodesWith(visitor);
85         assertEquals("# times 'is' was found", 2, visitor.stringFoundCount());
86
87         visitor = new StringFindingVisitor("and");
88         parser.visitAllNodesWith(visitor);
89         assertEquals("# times 'and' was found", 0, visitor.stringFoundCount());
90     }
91
92     public void testStringFoundMultipleTimes() throws Exception JavaDoc {
93         createParser(HTML_TO_SEARCH);
94         StringFindingVisitor visitor = new StringFindingVisitor("TEST");
95         visitor.doMultipleSearchesWithinStrings();
96         parser.visitAllNodesWith(visitor);
97         assertEquals("TEST found", 5, visitor.stringFoundCount());
98     }
99
100
101
102 }
103
Popular Tags