KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > ExpectedTable


1 /********************************************************************************
2  * Copyright (c) 2001, ThoughtWorks, Inc.
3  * Distributed open-source, see full license under licenses/jwebunit_license.txt
4 **********************************/

5 package net.sourceforge.jwebunit;
6
7 import java.util.ArrayList JavaDoc;
8
9 /**
10  * Represents an expected table for comparison with an actual html table.
11  *
12  * @author Jim Weaver
13  */

14 public class ExpectedTable {
15
16     private ArrayList JavaDoc expectedRows = new ArrayList JavaDoc();
17
18     /**
19      * Construct an expected table without providing any expecteds; they
20      * can be appended subsequently.
21      */

22     public ExpectedTable() {
23     }
24
25     /**
26      * Construct an expected table from a two dimensional array of objects.
27      * Each object's string value will be used with an expected colspan of 1,
28      * unless an object is an {@link net.sourceforge.jwebunit.ExpectedCell}, in
29      * which case its defined value and colspan are used.
30      *
31      * @param expectedValues two-dimensional array representing expected table cells.
32      */

33     public ExpectedTable(Object JavaDoc[][] expectedValues) {
34         appendRows(expectedValues);
35     }
36
37     /**
38      * Append any number of expected rows, represented by a two dimensional
39      * array of objects. Each object's string value will be used with an expected colspan of 1,
40      * unless an object is an {@link net.sourceforge.jwebunit.ExpectedCell}, in
41      * which case its defined value and colspan are used.
42      *
43      * @param newExpectedValues two-dimensional array representing expected table cells.
44      */

45     public void appendRows(Object JavaDoc[][] newExpectedValues) {
46         for (int i = 0; i < newExpectedValues.length; i++) {
47             expectedRows.add(new ExpectedRow(newExpectedValues[i]));
48         }
49     }
50
51     /**
52      * Append another expected table's rows.
53      *
54      * @param exptectedTable expected table whose rows are to be appended.
55      */

56     public void appendRows(ExpectedTable exptectedTable) {
57         expectedRows.addAll(exptectedTable.getExpectedRows());
58     }
59
60     /**
61      * Append a single expected row.
62      *
63      * @param row row to be appended.
64      */

65     public void appendRow(ExpectedRow row) {
66         expectedRows.add(row);
67     }
68
69     /**
70      * Return a two dimensional array of strings which represent the
71      * expected values. Cells which have a colspan other than one will
72      * occupy a number of positions within a row equal to their colspan.
73      * This array is used to compare against the HttpUnit representation
74      * of an actual html table.
75      *
76      */

77     public String JavaDoc[][] getExpectedStrings() {
78         String JavaDoc[][] asStringArray = new String JavaDoc[expectedRows.size()][];
79         for (int i = 0; i < expectedRows.size(); i++) {
80             ExpectedRow expectedRow = (ExpectedRow)expectedRows.get(i);
81             asStringArray[i] = expectedRow.getExpandedColumns();
82         }
83         return asStringArray;
84     }
85
86     /**
87      * Return a brace-delimited, printable version of the expected table
88      * for use in assertion failure output or debugging.
89      */

90     public String JavaDoc toString() {
91         StringBuffer JavaDoc asString = new StringBuffer JavaDoc();
92         String JavaDoc[][] asStringArray = getExpectedStrings();
93         for (int i = 0; i < asStringArray.length; i++) {
94             asString.append("{");
95             Object JavaDoc[] expectedRow = asStringArray[i];
96             for (int j = 0; j < expectedRow.length; j++) {
97                 asString.append("{");
98                 String JavaDoc column = (String JavaDoc) expectedRow[j];
99                 asString.append(column);
100                 asString.append("}");
101             }
102             asString.append("}");
103         }
104         return asString.toString();
105     }
106
107     ArrayList JavaDoc getExpectedRows() {
108         return expectedRows;
109     }
110
111 }
112
Popular Tags