KickJava   Java API By Example, From Geeks To Geeks.

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


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/TagFindingVisitorTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/02 00:49:31 $
10
// $Revision: 1.18 $
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.Node;
30 import org.htmlparser.Tag;
31 import org.htmlparser.tests.ParserTestCase;
32 import org.htmlparser.visitors.TagFindingVisitor;
33
34 public class TagFindingVisitorTest extends ParserTestCase {
35
36     static
37     {
38         System.setProperty ("org.htmlparser.tests.visitorsTests.TagFindingVisitorTest", "TagFindingVisitorTest");
39     }
40
41     private String JavaDoc html =
42         "<HTML><HEAD><TITLE>This is the Title</TITLE></HEAD>" +
43         "<BODY>Hello World, this is an excellent parser</BODY>" +
44         "<UL><LI><LI></UL>" +
45         "<A HREF=\"http://www.industriallogic.com\">Industrial Logic</a>" +
46         "</HTML>";
47
48     public TagFindingVisitorTest(String JavaDoc name) {
49         super(name);
50     }
51
52     public void setUp() {
53         createParser(html);
54     }
55
56     public void testTagFound() throws Exception JavaDoc {
57         TagFindingVisitor visitor = new TagFindingVisitor(new String JavaDoc[] {"HEAD"});
58         parser.visitAllNodesWith(visitor);
59         assertEquals("HEAD found", 1, visitor.getTagCount(0));
60     }
61
62     public void testTagsFound() throws Exception JavaDoc {
63         TagFindingVisitor visitor = new TagFindingVisitor(new String JavaDoc [] {"LI"});
64         parser.visitAllNodesWith(visitor);
65         assertEquals("LI tags found", 2, visitor.getTagCount(0));
66     }
67
68     public void testMultipleTags() throws Exception JavaDoc {
69         TagFindingVisitor visitor =
70             new TagFindingVisitor(
71                 new String JavaDoc [] {
72                     "LI","BODY","UL","A"
73                 }
74             );
75         parser.visitAllNodesWith(visitor);
76         assertEquals("LI tags found", 2, visitor.getTagCount(0));
77         assertEquals("BODY tag found", 1, visitor.getTagCount(1));
78         assertEquals("UL tag found", 1, visitor.getTagCount(2));
79         assertEquals("A tag found", 1, visitor.getTagCount(3));
80     }
81
82     public void testEndTags() throws Exception JavaDoc {
83         TagFindingVisitor visitor =
84             new TagFindingVisitor(
85                 new String JavaDoc [] {
86                     "LI","BODY","UL","A"
87                 },
88                 true
89             );
90         parser.visitAllNodesWith(visitor);
91         assertEquals("LI tags found", 2, visitor.getTagCount(0));
92         assertEquals("BODY tag found", 1, visitor.getTagCount(1));
93         assertEquals("UL tag found", 1, visitor.getTagCount(2));
94         assertEquals("A tag found", 1, visitor.getTagCount(3));
95         assertEquals("BODY end tag found", 1, visitor.getEndTagCount(1));
96     }
97
98
99     public void assertTagNameShouldBe(String JavaDoc message, Node node, String JavaDoc expectedTagName) {
100         Tag tag = (Tag)node;
101         assertStringEquals(message,expectedTagName,tag.getTagName());
102     }
103 }
104
105
Popular Tags