KickJava   Java API By Example, From Geeks To Geeks.

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


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/HtmlPageTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:02 $
10
// $Revision: 1.20 $
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.Text;
31 import org.htmlparser.tags.TableColumn;
32 import org.htmlparser.tags.TableRow;
33 import org.htmlparser.tags.TableTag;
34 import org.htmlparser.tests.ParserTestCase;
35 import org.htmlparser.util.NodeList;
36 import org.htmlparser.visitors.HtmlPage;
37
38 public class HtmlPageTest extends ParserTestCase {
39
40     static
41     {
42         System.setProperty ("org.htmlparser.tests.visitorsTests.HtmlPageTest", "HtmlPageTest");
43     }
44
45     private static final String JavaDoc SIMPLE_PAGE =
46         "<html>" +
47             "<head>" +
48                 "<title>Welcome to the HTMLParser website</title>" +
49             "</head>" +
50             "<body>" +
51                 "Welcome to HTMLParser" +
52             "</body>" +
53         "</html>";
54
55     private static final String JavaDoc guts =
56         "Welcome to HTMLParser" +
57         "<table>" +
58             "<tr>" +
59                 "<td>cell 1</td>" +
60                 "<td>cell 2</td>" +
61             "</tr>" +
62         "</table>";
63
64     private static final String JavaDoc PAGE_WITH_TABLE =
65         "<html>" +
66             "<head>" +
67                 "<title>Welcome to the HTMLParser website</title>" +
68             "</head>" +
69             "<body>" +
70                 guts +
71             "</body>" +
72         "</html>";
73
74     public HtmlPageTest(String JavaDoc name) {
75         super(name);
76     }
77
78     public void testCreateSimplePage() throws Exception JavaDoc {
79         createParser(
80             SIMPLE_PAGE
81         );
82         HtmlPage page = new HtmlPage(parser);
83         parser.visitAllNodesWith(page);
84         assertStringEquals(
85             "title",
86             "Welcome to the HTMLParser website",
87             page.getTitle()
88         );
89         NodeList bodyNodes = page.getBody();
90         assertEquals("number of nodes in body",1,bodyNodes.size());
91         Node node = bodyNodes.elementAt(0);
92         assertTrue("expected stringNode but was "+node.getClass().getName(),
93             node instanceof Text
94         );
95         assertStringEquals(
96             "body contents",
97             "Welcome to HTMLParser",
98             page.getBody().asString()
99         );
100     }
101
102     public void testCreatePageWithTables() throws Exception JavaDoc {
103         createParser(
104             PAGE_WITH_TABLE
105         );
106         HtmlPage page = new HtmlPage(parser);
107         parser.visitAllNodesWith(page);
108         NodeList bodyNodes = page.getBody();
109         assertEquals("number of nodes in body",2,bodyNodes.size());
110         assertXmlEquals("body html", guts, bodyNodes.toHtml());
111         TableTag tables [] = page.getTables();
112         assertEquals("number of tables",1,tables.length);
113         assertEquals("number of rows",1,tables[0].getRowCount());
114         TableRow row = tables[0].getRow(0);
115         assertEquals("number of columns",2,row.getColumnCount());
116         TableColumn [] col = row.getColumns();
117         assertEquals("column contents","cell 1",col[0].toPlainTextString());
118         assertEquals("column contents","cell 2",col[1].toPlainTextString());
119     }
120 }
121
Popular Tags