KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > scannersTests > TableScannerTest


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/scannersTests/TableScannerTest.java,v 1.2 2004/02/11 02:16:58 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.scannersTests;
34
35 import org.htmlparser.Node;
36 import org.htmlparser.Parser;
37 import org.htmlparser.scanners.TableScanner;
38 import org.htmlparser.tags.TableColumn;
39 import org.htmlparser.tags.TableRow;
40 import org.htmlparser.tags.TableTag;
41 import org.htmlparser.tests.ParserTestCase;
42 import org.htmlparser.util.NodeIterator;
43 import org.htmlparser.util.ParserException;
44
45 public class TableScannerTest extends ParserTestCase
46 {
47
48     public TableScannerTest(String JavaDoc name)
49     {
50         super(name);
51     }
52
53     private String JavaDoc createHtmlWithTable()
54     {
55         return "<table width=\"100.0%\" align=\"Center\" cellpadding=\"5.0\" cellspacing=\"0.0\" border=\"0.0\">"
56             + " <tr>"
57             + " <td border=\"0.0\" valign=\"Top\" colspan=\"5\">"
58             + " <img SRC=\"file:/c:/data/dev/eclipse_workspace/ShoppingServerTests/resources/pictures/fishbowl.jpg\" width=\"446.0\" height=\"335.0\" />"
59             + " </td>"
60             + " <td border=\"0.0\" align=\"Justify\" valign=\"Top\" colspan=\"7\">"
61             + " <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>"
62             + " </td>"
63             + " </tr>"
64             + "</table>";
65     }
66
67     public void testScan() throws Exception JavaDoc
68     {
69         createParser(createHtmlWithTable());
70         parser.addScanner(new TableScanner(parser));
71         parseAndAssertNodeCount(1);
72         assertTrue(node[0] instanceof TableTag);
73         TableTag tableTag = (TableTag) node[0];
74         assertEquals("rows", 1, tableTag.getRowCount());
75         TableRow row = tableTag.getRow(0);
76         assertEquals("columns in row 1", 2, row.getColumnCount());
77         assertEquals("table width", "100.0%", tableTag.getAttribute("WIDTH"));
78     }
79
80     public void testErroneousTables() throws ParserException
81     {
82         createParser(
83             "<HTML>\n"
84                 + "<table border>\n"
85                 + "<tr>\n"
86                 + "<td>Head1</td>\n"
87                 + "<td>Val1</td>\n"
88                 + "</tr>\n"
89                 + "<tr>\n"
90                 + "<td>Head2</td>\n"
91                 + "<td>Val2</td>\n"
92                 + "</tr>\n"
93                 + "<tr>\n"
94                 + "<td>\n"
95                 + "<table border>\n"
96                 + "<tr>\n"
97                 + "<td>table2 Head1</td>\n"
98                 + "<td>table2 Val1</td>\n"
99                 + "</tr>\n"
100                 + "</table>\n"
101                 + "</td>\n"
102                 + "</tr>\n"
103                 + "</BODY>\n"
104                 + "</HTML>");
105         parser.registerScanners();
106         parseAndAssertNodeCount(4);
107         assertType("second tag", TableTag.class, node[1]);
108         TableTag table = (TableTag) node[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(0);
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         if (1.4 <= Parser.getVersionNumber())
133         {
134             parser.registerScanners();
135             for (NodeIterator e = parser.elements(); e.hasMoreNodes();)
136                 e.nextNode();
137             // Note: The test will throw a StackOverFlowException,
138
// so we are successful if we get to here...
139
assertTrue("Crash", true);
140         }
141     }
142
143     /**
144      * See bug #742254 Nested <TR> &<TD> tags should not be allowed
145      */

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

158     public void testUnClosed2() throws ParserException
159     {
160         createParser("<TABLE><TR><TD><TD></TD></TR></TABLE>");
161         parser.registerScanners();
162         parseAndAssertNodeCount(1);
163         String JavaDoc s = node[0].toHtml();
164         assertEquals(
165             "Unclosed",
166             "<TABLE><TR><TD></TD><TD></TD></TR></TABLE>",
167             s);
168     }
169
170     /**
171      * See bug #742254 Nested <TR> &<TD> tags should not be allowed
172      */

173     public void testUnClosed3() throws ParserException
174     {
175         createParser("<TABLE><TR><TD>blah blah</TD><TR><TD>blah blah</TD></TR></TABLE>");
176         parser.registerScanners();
177         parseAndAssertNodeCount(1);
178         String JavaDoc s = node[0].toHtml();
179         assertEquals(
180             "Unclosed",
181             "<TABLE><TR><TD>blah blah</TD></TR><TR><TD>blah blah</TD></TR></TABLE>",
182             s);
183     }
184 }
185
Popular Tags