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 a row of an html table.
28   *
29   * @author Jim Weaver
30   * @author Julien Henry
31   */
32  public class Row {
33  
34    private List<Cell> cells = new ArrayList<Cell>();
35  
36    public Row() {
37    }
38  
39    /**
40     * Construct a row from an array of objects which specify the cells of the row. If an object in the array is an
41     * {@link net.sourceforge.jwebunit.html.Cell}, it is directly added to the cells of the row, otherwise an
42     * {@link net.sourceforge.jwebunit.html.Cell} is created from the toString() value of the object and an assumed
43     * colspan of 1.
44     *
45     * @param rowCells objects representing the row's cells.
46     */
47    public Row(Object[] rowCells) {
48      appendCells(rowCells);
49    }
50  
51    public void appendCells(Object[] rowCells) {
52      for (int i = 0; i < rowCells.length; i++) {
53        Object column = rowCells[i];
54        if (column instanceof Cell) {
55          this.cells.add((Cell) column);
56        } else {
57          this.cells.add(new Cell(column.toString()));
58        }
59      }
60    }
61  
62    public void appendCell(Cell cell) {
63      cells.add(cell);
64    }
65  
66    public void appendCell(String cellText) {
67      cells.add(new Cell(cellText));
68    }
69  
70    public List<Cell> getCells() {
71      return cells;
72    }
73  
74    public int getCellCount() {
75      return cells.size();
76    }
77  
78    public boolean hasText(String text) {
79      for (int i = 0; i < getCellCount(); i++) {
80        Cell c = (Cell) getCells().get(i);
81        if (c.equals(text))
82          return true;
83      }
84      return false;
85    }
86  
87    public boolean hasMatch(String regexp) {
88      for (int i = 0; i < getCellCount(); i++) {
89        Cell c = (Cell) getCells().get(i);
90        if (c.match(regexp))
91          return true;
92      }
93      return false;
94    }
95  
96    public void assertEquals(Row r) {
97      Assert.assertTrue("Cell count are not equal",
98        this.getCells().size() == r.getCells().size());
99      for (int i = 0; i < this.getCells().size(); i++) {
100       ((Cell) this.getCells().get(i)).assertEquals((Cell) r.getCells()
101         .get(i));
102     }
103   }
104 
105   public void assertMatch(Row r) {
106     Assert.assertTrue("Cell count are not equal",
107       this.getCells().size() == r.getCells().size());
108     for (int i = 0; i < this.getCells().size(); i++) {
109       ((Cell) this.getCells().get(i)).assertMatch((Cell) r.getCells()
110         .get(i));
111     }
112   }
113 }