KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlTableTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import com.gargoylesoftware.htmlunit.WebTestCase;
44
45 /**
46  * Tests for HtmlTable
47  *
48  * @version $Revision: 1.11 $
49  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
50  */

51 public class HtmlTableTest extends WebTestCase {
52     /**
53      * Create an instance
54      *
55      * @param name The name of the test
56      */

57     public HtmlTableTest( final String name ) {
58         super( name );
59     }
60
61
62     /**
63      * Test getTableCell(int,int)
64      *
65      * @exception Exception If the test fails
66      */

67     public void testGetTableCell() throws Exception {
68         final String htmlContent
69             = "<html><head><title>foo</title></head><body>"
70             + "<table id='table1' summary='Test table'>"
71             + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>"
72             + "<tr><td colspan='2'>cell3</td></tr>"
73             + "</table>"
74             + "</body></html>";
75         final HtmlPage page = loadPage(htmlContent);
76         
77         final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" );
78
79         final HtmlTableCell cell1 = table.getCellAt( 0, 0 );
80         assertEquals( "cell1 contents", "cell1", cell1.asText() );
81
82         final HtmlTableCell cell2 = table.getCellAt( 0, 1 );
83         assertEquals( "cell2 contents", "cell2", cell2.asText() );
84
85         final HtmlTableCell cell3 = table.getCellAt( 1, 0 );
86         assertEquals( "cell3 contents", "cell3", cell3.asText() );
87         assertSame( "cells (1,0) and (1,1)", cell3, table.getCellAt( 1, 1 ) );
88
89         final HtmlTableCell cell4 = table.getCellAt( 0, 2 );
90         assertEquals( "cell4 contents", "cell4", cell4.asText() );
91         assertSame( "cells (0,2) and (1,2)", cell4, table.getCellAt( 1, 2 ) );
92     }
93
94
95     /**
96      * Test getTableCell(int,int) for a cell that doesn't exist
97      *
98      * @exception Exception If the test fails
99      */

100     public void testGetTableCell_NotFound()
101         throws Exception {
102
103         final String htmlContent
104             = "<html><head><title>foo</title></head><body>"
105             + "<table id='table1' summary='Test table'>"
106             + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>"
107             + "<tr><td colspan='2'>cell3</td></tr>"
108             + "</table>"
109             + "</body></html>";
110         final HtmlPage page = loadPage(htmlContent);
111         
112         final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" );
113
114         final HtmlTableCell cell = table.getCellAt( 99, 0 );
115         assertNull( "cell", cell );
116     }
117
118
119     /**
120      * @throws Exception if the test fails
121      */

122     public void testGetTableRows()
123         throws Exception {
124
125         final String htmlContent
126             = "<html><head><title>foo</title></head><body>"
127             + "<table id='table1'>"
128             + "<tr id='row1'><td>cell1</td></tr>"
129             + "<tr id='row2'><td>cell2</td></tr>"
130             + "<tr id='row3'><td>cell3</td></tr>"
131             + "<tr id='row4'><td>cell4</td></tr>"
132             + "<tr id='row5'><td>cell5</td></tr>"
133             + "<tr id='row6'><td>cell6</td></tr>"
134             + "</table>"
135             + "</body></html>";
136         final HtmlPage page = loadPage(htmlContent);
137         
138         final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" );
139
140         final List expectedRows = new ArrayList();
141         expectedRows.add( table.getRowById( "row1" ) );
142         expectedRows.add( table.getRowById( "row2" ) );
143         expectedRows.add( table.getRowById( "row3" ) );
144         expectedRows.add( table.getRowById( "row4" ) );
145         expectedRows.add( table.getRowById( "row5" ) );
146         expectedRows.add( table.getRowById( "row6" ) );
147
148         assertEquals( expectedRows, table.getRows() );
149     }
150
151
152     /**
153      * @throws Exception if the test fails
154      */

155     public void testGetTableRows_WithHeadBodyFoot()
156         throws Exception {
157
158         final String htmlContent
159             = "<html><head><title>foo</title></head><body>"
160             + "<table id='table1'>"
161             + "<thead>"
162             + " <tr id='row1'><td>cell1</td></tr>"
163             + " <tr id='row2'><td>cell2</td></tr>"
164             + "</thead>"
165             + "<tbody>"
166             + " <tr id='row3'><td>cell3</td></tr>"
167             + " <tr id='row4'><td>cell4</td></tr>"
168             + "</tbody>"
169             + "<tfoot>"
170             + " <tr id='row5'><td>cell5</td></tr>"
171             + " <tr id='row6'><td>cell6</td></tr>"
172             + "</tfoot>"
173             + "</table>"
174             + "</body></html>";
175         final HtmlPage page = loadPage(htmlContent);
176
177         final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" );
178
179         final List expectedRows = new ArrayList();
180         expectedRows.add( table.getRowById( "row1" ) );
181         expectedRows.add( table.getRowById( "row2" ) );
182         expectedRows.add( table.getRowById( "row3" ) );
183         expectedRows.add( table.getRowById( "row4" ) );
184         expectedRows.add( table.getRowById( "row5" ) );
185         expectedRows.add( table.getRowById( "row6" ) );
186
187         assertEquals( expectedRows, table.getRows() );
188     }
189
190
191     /**
192      * @throws Exception if the test fails
193      */

194     public void testRowGroupings_AllDefined()
195         throws Exception {
196
197         final String htmlContent
198             = "<html><head><title>foo</title></head><body>"
199             + "<table id='table1'>"
200             + "<thead>"
201             + " <tr id='row1'><td>cell1</td></tr>"
202             + " <tr id='row2'><td>cell2</td></tr>"
203             + "</thead>"
204             + "<tbody>"
205             + " <tr id='row3'><td>cell3</td></tr>"
206             + "</tbody>"
207             + "<tbody>"
208             + " <tr id='row4'><td>cell4</td></tr>"
209             + "</tbody>"
210             + "<tfoot>"
211             + " <tr id='row5'><td>cell5</td></tr>"
212             + " <tr id='row6'><td>cell6</td></tr>"
213             + "</tfoot>"
214             + "</table>"
215             + "</body></html>";
216         final HtmlPage page = loadPage(htmlContent);
217
218         final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" );
219
220         assertNotNull( table.getHeader() );
221         assertNotNull( table.getFooter() );
222         assertEquals( 2, table.getBodies().size() );
223     }
224
225
226     /**
227      * Check to ensure that the proper numbers of tags show up. Note that an extra tbody
228      * will be inserted to be in compliance with the common browsers.
229      * @throws Exception if the test fails
230      */

231     public void testRowGroupings_NoneDefined()
232         throws Exception {
233
234         final String htmlContent
235             = "<html><head><title>foo</title></head><body>"
236             + "<table id='table1'>"
237             + " <tr id='row1'><td>cell1</td></tr>"
238             + " <tr id='row2'><td>cell2</td></tr>"
239             + " <tr id='row3'><td>cell3</td></tr>"
240             + " <tr id='row4'><td>cell4</td></tr>"
241             + " <tr id='row5'><td>cell5</td></tr>"
242             + " <tr id='row6'><td>cell6</td></tr>"
243             + "</table>"
244             + "</body></html>";
245         final HtmlPage page = loadPage(htmlContent);
246
247         final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" );
248
249         assertEquals( null, table.getHeader() );
250         assertEquals( null, table.getFooter() );
251         assertEquals( 1, table.getBodies().size() );
252     }
253
254
255     /**
256      * @throws Exception if the test fails
257      */

258     public void testGetCaptionText()
259         throws Exception {
260
261         final String htmlContent
262             = "<html><head><title>foo</title></head><body>"
263             + "<table id='table1' summary='Test table'>"
264             + "<caption>MyCaption</caption>"
265             + "<tr><td>cell1</td><td>cell2</td><td rowspan='2'>cell4</td></tr>"
266             + "<tr><td colspan='2'>cell3</td></tr>"
267             + "</table>"
268             + "</body></html>";
269         final HtmlPage page = loadPage(htmlContent);
270
271         final HtmlTable table = ( HtmlTable )page.getHtmlElementById( "table1" );
272
273         assertEquals("MyCaption", table.getCaptionText());
274     }
275
276     /**
277      * The common browsers will automatically insert tbody tags around the table rows if
278      * one wasn't specified. Ensure that we do this too. Also ensure that extra ones
279      * aren't inserted if a tbody was already there.
280      * @throws Exception if the test fails
281      */

282     public void testInsertionOfTbodyTags() throws Exception {
283
284         final String htmlContent
285             = "<html><head><title>foo</title></head><body>"
286             + "<table>"
287             + "<tr><td id='cell1'>cell1</td></tr>"
288             + "</table>"
289             + "<table><tbody>"
290             + "<tr><td id='cell2'>cell1</td></tr>"
291             + "</tbody></table>"
292             + "</body></html>";
293         final HtmlPage page = loadPage(htmlContent);
294
295         // Check that a <tbody> was inserted properly
296
final HtmlTableDataCell cell1 = ( HtmlTableDataCell )page.getHtmlElementById( "cell1" );
297         assertInstanceOf( cell1.getParentNode(), HtmlTableRow.class );
298         assertInstanceOf( cell1.getParentNode().getParentNode(), HtmlTableBody.class );
299         assertInstanceOf( cell1.getParentNode().getParentNode().getParentNode(), HtmlTable.class );
300
301         // Check that the existing <tbody> wasn't messed up.
302
final HtmlTableDataCell cell2 = ( HtmlTableDataCell )page.getHtmlElementById( "cell2" );
303         assertInstanceOf( cell2.getParentNode(), HtmlTableRow.class );
304         assertInstanceOf( cell2.getParentNode().getParentNode(), HtmlTableBody.class );
305         assertInstanceOf( cell2.getParentNode().getParentNode().getParentNode(), HtmlTable.class );
306     }
307 }
308
309
Popular Tags