View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
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   * Represents an expected table for comparison with an actual html table.
28   *
29   * @author Jim Weaver
30   * @author Julien Henry
31   */
32  public class Table {
33  
34    private List<Row> rows = new ArrayList<Row>();
35  
36    /**
37     * Construct a table without providing any contents; they can be appended subsequently.
38     */
39    public Table() {
40    }
41  
42    /**
43     * Construct a table from a two dimensional array of objects. Each object's string value will be used with a colspan
44     * of 1, unless an object is an {@link net.sourceforge.jwebunit.html.Cell}, in which case its defined value and
45     * colspan are used.
46     *
47     * @param values two-dimensional array representing table cells.
48     */
49    public Table(Object[][] values) {
50      appendRows(values);
51    }
52  
53    /**
54     * Append any number of rows, represented by a two dimensional array of objects. Each object's string value will be
55     * used with a colspan of 1, unless an object is an {@link net.sourceforge.jwebunit.html.Cell}, in which case its
56     * defined value and colspan are used.
57     *
58     * @param newExpectedValues two-dimensional array representing expected table cells.
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     * Append another table's rows.
68     *
69     * @param table table whose rows are to be appended.
70     */
71    public void appendRows(Table table) {
72      rows.addAll(table.getRows());
73    }
74  
75    /**
76     * Append a single expected row.
77     *
78     * @param row row to be appended.
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 }