1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.html;
20
21 import org.junit.Assert;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30
31
32 public class Table {
33
34 private List<Row> rows = new ArrayList<Row>();
35
36
37
38
39 public Table() {
40 }
41
42
43
44
45
46
47
48
49 public Table(Object[][] values) {
50 appendRows(values);
51 }
52
53
54
55
56
57
58
59
60 public void appendRows(Object[][] newExpectedValues) {
61 for (int i = 0; i < newExpectedValues.length; i++) {
62 rows.add(new Row(newExpectedValues[i]));
63 }
64 }
65
66
67
68
69
70
71 public void appendRows(Table table) {
72 rows.addAll(table.getRows());
73 }
74
75
76
77
78
79
80 public void appendRow(Row row) {
81 rows.add(row);
82 }
83
84 public int getRowCount() {
85 return getRows().size();
86 }
87
88 public List<Row> getRows() {
89 return rows;
90 }
91
92 public boolean hasText(String text) {
93 for (int i = 0; i < getRowCount(); i++) {
94 Row row = (Row) getRows().get(i);
95 if (row.hasText(text))
96 return true;
97 }
98 return false;
99 }
100
101 public boolean hasMatch(String regexp) {
102 for (int i = 0; i < getRowCount(); i++) {
103 Row row = (Row) getRows().get(i);
104 if (row.hasMatch(regexp))
105 return true;
106 }
107 return false;
108 }
109
110 public void assertEquals(Table t) {
111 Assert.assertTrue("Row count are not equal", this.getRows().size() == t
112 .getRows().size());
113 for (int i = 0; i < this.getRows().size(); i++) {
114 ((Row) this.getRows().get(i))
115 .assertEquals((Row) t.getRows().get(i));
116 }
117 }
118
119 public void assertSubTableEquals(int startRow, Table t) {
120 Table sub = new Table();
121 if (startRow + t.getRowCount() > this.getRowCount())
122 Assert.fail("Expected rows [" + t.getRowCount()
123 + "] larger than actual rows in range being compared"
124 + " [" + (this.getRowCount() - startRow) + "].");
125 for (int i = startRow; i < startRow + t.getRowCount(); i++) {
126 sub.appendRow((Row) this.getRows().get(i));
127 }
128 sub.assertEquals(t);
129 }
130
131 public void assertMatch(Table t) {
132 Assert.assertTrue("Row count are not equal", this.getRows().size() == t
133 .getRows().size());
134 for (int i = 0; i < this.getRows().size(); i++) {
135 ((Row) this.getRows().get(i)).assertMatch((Row) t.getRows().get(i));
136 }
137 }
138
139 public void assertSubTableMatch(int startRow, Table t) {
140 Table sub = new Table();
141 if (startRow + t.getRowCount() > this.getRowCount())
142 Assert.fail("Expected rows [" + t.getRowCount()
143 + "] larger than actual rows in range being compared"
144 + " [" + (this.getRowCount() - startRow) + "].");
145 for (int i = startRow; i < startRow + t.getRowCount(); i++) {
146 sub.appendRow((Row) this.getRows().get(i));
147 }
148 sub.assertMatch(t);
149 }
150
151 }