Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/htmlparser/tests/visitorsTests/TagFindingVisitorTest.java


1   // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/visitorsTests/TagFindingVisitorTest.java,v 1.2 2004/02/11 02:16:59 woolfel Exp $
2   /*
3    * ====================================================================
4    * Copyright 2002-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * 
18   */
19  
20  // The developers of JMeter and Apache are greatful to the developers
21  // of HTMLParser for giving Apache Software Foundation a non-exclusive
22  // license. The performance benefits of HTMLParser are clear and the
23  // users of JMeter will benefit from the hard work the HTMLParser
24  // team. For detailed information about HTMLParser, the project is
25  // hosted on sourceforge at http://htmlparser.sourceforge.net/.
26  //
27  // HTMLParser was originally created by Somik Raha in 2000. Since then
28  // a healthy community of users has formed and helped refine the
29  // design so that it is able to tackle the difficult task of parsing
30  // dirty HTML. Derrick Oswald is the current lead developer and was kind
31  // enough to assist JMeter.
32  
33  package org.htmlparser.tests.visitorsTests;
34  
35  import org.htmlparser.Node;
36  import org.htmlparser.tags.Tag;
37  import org.htmlparser.tests.ParserTestCase;
38  import org.htmlparser.visitors.TagFindingVisitor;
39  
40  public class TagFindingVisitorTest extends ParserTestCase
41  {
42      private String html =
43          "<HTML><HEAD><TITLE>This is the Title</TITLE></HEAD>"
44              + "<BODY>Hello World, this is an excellent parser</BODY>"
45              + "<UL><LI><LI></UL>"
46              + "<A href=\"http://www.industriallogic.com\">Industrial Logic</a>"
47              + "</HTML>";
48  
49      public TagFindingVisitorTest(String name)
50      {
51          super(name);
52      }
53  
54      public void setUp()
55      {
56          createParser(html);
57      }
58  
59      public void testTagFound() throws Exception
60      {
61          TagFindingVisitor visitor =
62              new TagFindingVisitor(new String[] { "HEAD" });
63          parser.visitAllNodesWith(visitor);
64          assertEquals("HEAD found", 1, visitor.getTagCount(0));
65      }
66  
67      public void testTagsFound() throws Exception
68      {
69          TagFindingVisitor visitor =
70              new TagFindingVisitor(new String[] { "LI" });
71          parser.visitAllNodesWith(visitor);
72          assertEquals("LI tags found", 2, visitor.getTagCount(0));
73      }
74  
75      public void testMultipleTags() throws Exception
76      {
77          TagFindingVisitor visitor =
78              new TagFindingVisitor(new String[] { "LI", "BODY", "UL", "A" });
79          parser.visitAllNodesWith(visitor);
80          assertEquals("LI tags found", 2, visitor.getTagCount(0));
81          assertEquals("BODY tag found", 1, visitor.getTagCount(1));
82          assertEquals("UL tag found", 1, visitor.getTagCount(2));
83          assertEquals("A tag found", 1, visitor.getTagCount(3));
84      }
85  
86      public void testEndTags() throws Exception
87      {
88          TagFindingVisitor visitor =
89              new TagFindingVisitor(
90                  new String[] { "LI", "BODY", "UL", "A" },
91                  true);
92          parser.visitAllNodesWith(visitor);
93          assertEquals("LI tags found", 2, visitor.getTagCount(0));
94          assertEquals("BODY tag found", 1, visitor.getTagCount(1));
95          assertEquals("UL tag found", 1, visitor.getTagCount(2));
96          assertEquals("A tag found", 1, visitor.getTagCount(3));
97          assertEquals("BODY end tag found", 1, visitor.getEndTagCount(1));
98      }
99  
100     public void assertTagNameShouldBe(
101         String message,
102         Node node,
103         String expectedTagName)
104     {
105         Tag tag = (Tag) node;
106         assertStringEquals(message, expectedTagName, tag.getTagName());
107     }
108 }