001: /******************************************************************************
002: * jWebUnit project (http://jwebunit.sourceforge.net) *
003: * Distributed open-source, see full license under LICENCE.txt *
004: ******************************************************************************/package net.sourceforge.jwebunit.html;
005:
006: import java.util.ArrayList;
007:
008: import junit.framework.Assert;
009:
010: /**
011: * Represents an expected table for comparison with an actual html table.
012: *
013: * @author Jim Weaver
014: * @author Julien Henry
015: */
016: public class Table {
017:
018: private ArrayList rows = new ArrayList();
019:
020: /**
021: * Construct a table without providing any contents; they can be appended subsequently.
022: */
023: public Table() {
024: }
025:
026: /**
027: * Construct a table from a two dimensional array of objects. Each object's string value will be used with a colspan
028: * of 1, unless an object is an {@link net.sourceforge.jwebunit.html.Cell}, in which case its defined value and
029: * colspan are used.
030: *
031: * @param values two-dimensional array representing table cells.
032: */
033: public Table(Object[][] values) {
034: appendRows(values);
035: }
036:
037: /**
038: * Append any number of rows, represented by a two dimensional array of objects. Each object's string value will be
039: * used with a colspan of 1, unless an object is an {@link net.sourceforge.jwebunit.html.Cell}, in which case its
040: * defined value and colspan are used.
041: *
042: * @param newExpectedValues two-dimensional array representing expected table cells.
043: */
044: public void appendRows(Object[][] newExpectedValues) {
045: for (int i = 0; i < newExpectedValues.length; i++) {
046: rows.add(new Row(newExpectedValues[i]));
047: }
048: }
049:
050: /**
051: * Append another table's rows.
052: *
053: * @param table table whose rows are to be appended.
054: */
055: public void appendRows(Table table) {
056: rows.addAll(table.getRows());
057: }
058:
059: /**
060: * Append a single expected row.
061: *
062: * @param row row to be appended.
063: */
064: public void appendRow(Row row) {
065: rows.add(row);
066: }
067:
068: public int getRowCount() {
069: return getRows().size();
070: }
071:
072: public ArrayList getRows() {
073: return rows;
074: }
075:
076: public boolean hasText(String text) {
077: for (int i = 0; i < getRowCount(); i++) {
078: Row row = (Row) getRows().get(i);
079: if (row.hasText(text))
080: return true;
081: }
082: return false;
083: }
084:
085: public boolean hasMatch(String regexp) {
086: for (int i = 0; i < getRowCount(); i++) {
087: Row row = (Row) getRows().get(i);
088: if (row.hasMatch(regexp))
089: return true;
090: }
091: return false;
092: }
093:
094: public void assertEquals(Table t) {
095: Assert.assertTrue("Row count are not equal", this .getRows()
096: .size() == t.getRows().size());
097: for (int i = 0; i < this .getRows().size(); i++) {
098: ((Row) this .getRows().get(i)).assertEquals((Row) t
099: .getRows().get(i));
100: }
101: }
102:
103: public void assertSubTableEquals(int startRow, Table t) {
104: Table sub = new Table();
105: if (startRow + t.getRowCount() > this .getRowCount())
106: Assert
107: .fail("Expected rows ["
108: + t.getRowCount()
109: + "] larger than actual rows in range being compared"
110: + " [" + (this .getRowCount() - startRow)
111: + "].");
112: for (int i = startRow; i < startRow + t.getRowCount(); i++) {
113: sub.appendRow((Row) this .getRows().get(i));
114: }
115: sub.assertEquals(t);
116: }
117:
118: public void assertMatch(Table t) {
119: Assert.assertTrue("Row count are not equal", this .getRows()
120: .size() == t.getRows().size());
121: for (int i = 0; i < this .getRows().size(); i++) {
122: ((Row) this .getRows().get(i)).assertMatch((Row) t.getRows()
123: .get(i));
124: }
125: }
126:
127: public void assertSubTableMatch(int startRow, Table t) {
128: Table sub = new Table();
129: if (startRow + t.getRowCount() > this .getRowCount())
130: Assert
131: .fail("Expected rows ["
132: + t.getRowCount()
133: + "] larger than actual rows in range being compared"
134: + " [" + (this .getRowCount() - startRow)
135: + "].");
136: for (int i = startRow; i < startRow + t.getRowCount(); i++) {
137: sub.appendRow((Row) this.getRows().get(i));
138: }
139: sub.assertMatch(t);
140: }
141:
142: }
|