KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > tagTests > TableTagTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2003 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/TableTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:31 $
10
// $Revision: 1.2 $
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.tagTests;
28
29 import org.htmlparser.Node;
30 import org.htmlparser.Parser;
31 import org.htmlparser.tags.Html;
32 import org.htmlparser.tags.TableColumn;
33 import org.htmlparser.tags.TableRow;
34 import org.htmlparser.tags.TableTag;
35 import org.htmlparser.tests.ParserTestCase;
36 import org.htmlparser.util.NodeIterator;
37 import org.htmlparser.util.ParserException;
38
39 public class TableTagTest extends ParserTestCase
40 {
41     static
42     {
43         System.setProperty ("org.htmlparser.tests.tagTests.TableTagTest", "TableTagTest");
44     }
45
46
47     public TableTagTest (String JavaDoc name)
48     {
49         super(name);
50     }
51     
52     private String JavaDoc createHtmlWithTable() {
53         return
54         "<table width=\"100.0%\" align=\"Center\" cellpadding=\"5.0\" cellspacing=\"0.0\" border=\"0.0\">"+
55         " <tr>" +
56         " <td border=\"0.0\" valign=\"Top\" colspan=\"5\">" +
57         " <img SRC=\"file:/c:/data/dev/eclipse_workspace/ShoppingServerTests/resources/pictures/fishbowl.jpg\" width=\"446.0\" height=\"335.0\" />" +
58         " </td>" +
59         " <td border=\"0.0\" align=\"Justify\" valign=\"Top\" colspan=\"7\">" +
60         " <span>The best way to improve your refactoring skills is to practice cleaning up poorly-designed code. And we've got just the thing: code we custom-designed to reek of over 90% of the code smells identified in the refactoring literature. This poorly designed code functions correctly, which you can verify by running a full suite of tests against it. Your challenge is to identify the smells in this code, determining which refactoring(s) can help you clean up the smells and implement the refactorings to arrive at a well-designed new version of the code that continues to pass its unit tests. This exercise takes place using our popular class fishbowl. There is a lot to learn from this challenge, so we recommend that you spend as much time on it as possible.&#013;<br /></span>" +
61         " </td>" +
62         " </tr>" +
63         "</table>";
64     }
65
66     public void testScan() throws ParserException
67     {
68         createParser(createHtmlWithTable());
69         parseAndAssertNodeCount(1);
70         assertTrue(node[0] instanceof TableTag);
71         TableTag tableTag = (TableTag)node[0];
72         assertEquals("rows",1,tableTag.getRowCount());
73         TableRow row = tableTag.getRow(0);
74         assertEquals("columns in row 1",2,row.getColumnCount());
75         assertEquals("table width","100.0%",tableTag.getAttribute("WIDTH"));
76     }
77
78     public void testErroneousTables() throws ParserException {
79         createParser(
80             "<HTML>\n"+
81                 "<table border>\n"+
82                     "<tr>\n"+
83                         "<td>Head1</td>\n"+
84                         "<td>Val1</td>\n"+
85                     "</tr>\n"+
86                     "<tr>\n"+
87                         "<td>Head2</td>\n"+
88                         "<td>Val2</td>\n"+
89                     "</tr>\n"+
90                     "<tr>\n"+
91                         "<td>\n"+
92                             "<table border>\n"+
93                                 "<tr>\n"+
94                                     "<td>table2 Head1</td>\n"+
95                                     "<td>table2 Val1</td>\n"+
96                                 "</tr>\n"+
97                             "</table>\n"+
98                         "</td>\n"+
99                     "</tr>\n"+
100             "</BODY>\n"+
101             "</HTML>"
102         );
103         parseAndAssertNodeCount(1);
104         assertType("only tag should be a HTML tag", Html.class,node[0]);
105         Html html = (Html)node[0];
106         assertEquals("html tag should have 4 children", 4, html.getChildCount ());
107         assertType("second tag",TableTag.class,html.getChild (1));
108         TableTag table = (TableTag)html.getChild (1);
109         assertEquals("rows",3,table.getRowCount());
110         TableRow tr = table.getRow(2);
111         assertEquals("columns",1,tr.getColumnCount());
112         TableColumn td = tr.getColumns()[0];
113         Node node = td.childAt(1);
114         assertType("node",TableTag.class,node);
115         TableTag table2 = (TableTag)node;
116         assertEquals("second table row count",1,table2.getRowCount());
117         tr = table2.getRow(0);
118         assertEquals("second table col count",2,tr.getColumnCount());
119     }
120
121     /**
122      * Test many unclosed tags (causes heavy recursion).
123      * See feature request #729259 Increase maximum recursion depth.
124      * Only perform this test if it's version 1.4 or higher.
125      */

126     public void testRecursionDepth () throws ParserException
127     {
128         Parser parser;
129         String JavaDoc url = "http://htmlparser.sourceforge.net/test/badtable2.html";
130
131         parser = new Parser (url);
132         for (NodeIterator e = parser.elements();e.hasMoreNodes();)
133             e.nextNode();
134         // Note: The test will throw a StackOverFlowException,
135
// so we are successful if we get to here...
136
assertTrue ("Crash", true);
137     }
138
139     /**
140      * See bug #742254 Nested <TR> &<TD> tags should not be allowed
141      */

142     public void testUnClosed1 () throws ParserException
143     {
144         createParser ("<TABLE><TR><TR></TR></TABLE>");
145         parseAndAssertNodeCount (1);
146         String JavaDoc s = node[0].toHtml ();
147         assertEquals ("Unclosed","<TABLE><TR></TR><TR></TR></TABLE>",s);
148     }
149
150     /**
151      * See bug #742254 Nested <TR> &<TD> tags should not be allowed
152      */

153     public void testUnClosed2 () throws ParserException
154     {
155         createParser ("<TABLE><TR><TD><TD></TD></TR></TABLE>");
156         parseAndAssertNodeCount (1);
157         String JavaDoc s = node[0].toHtml ();
158         assertEquals ("Unclosed","<TABLE><TR><TD></TD><TD></TD></TR></TABLE>",s);
159     }
160
161     /**
162      * See bug #742254 Nested <TR> &<TD> tags should not be allowed
163      */

164     public void testUnClosed3 () throws ParserException
165     {
166         createParser ("<TABLE><TR><TD>blah blah</TD><TR><TD>blah blah</TD></TR></TABLE>");
167         parseAndAssertNodeCount (1);
168         String JavaDoc s = node[0].toHtml ();
169         assertEquals ("Unclosed","<TABLE><TR><TD>blah blah</TD></TR><TR><TD>blah blah</TD></TR></TABLE>",s);
170     }
171
172     /**
173      * See bug #750117 StackOverFlow while Node-Iteration
174      * Not reproducible.
175      */

176     public void testOverFlow () throws ParserException
177     {
178         parser =
179             new Parser(
180                 "http://www.sec.gov/Archives/edgar/data/30554/000089322002000287/w57038e10-k.htm"
181             );
182         for (NodeIterator e = parser.elements(); e.hasMoreNodes(); )
183             e.nextNode();
184     }
185 }
186
Popular Tags