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.apache.regexp.RE;
22  import org.apache.regexp.RESyntaxException;
23  import org.junit.Assert;
24  
25  /**
26   * Represents a cell of an html table - a string value spanning an indicated amount of columns.
27   *
28   * @author Jim Weaver
29   * @author Julien Henry
30   */
31  public class Cell {
32  
33    private int colspan;
34  
35    private int rowspan;
36  
37    private String value;
38  
39    /**
40     * Construct a cell with a default colspan/rowspan of 1.
41     *
42     * @param value text expected within the cell.
43     */
44    public Cell(String value) {
45      this(value, 1, 1);
46    }
47  
48    /**
49     * Construct a cell with a specified colspan.
50     *
51     * @param value text expected within the cell.
52     * @param colspan number of columns the cell is expected to span.
53     * @param rowspan number of rows the cell is expected to span.
54     */
55    public Cell(String value, int colspan, int rowspan) {
56      this.value = value;
57      this.colspan = colspan;
58      this.rowspan = rowspan;
59    }
60  
61    /**
62     * @return the colspan for this cell.
63     */
64    public int getColspan() {
65      return colspan;
66    }
67  
68    /**
69     * @return the rowspan for this cell.
70     */
71    public int getRowspan() {
72      return rowspan;
73    }
74  
75    /**
76     * @return the text for the cell.
77     */
78    public final String getValue() {
79      return value;
80    }
81  
82    /**
83     * Assert that the current cell equals given one. Check text, colspan and rowspan.
84     *
85     * @param c given cell
86     */
87    public void assertEquals(Cell c) {
88      Assert.assertTrue(c.getValue() + " do not equal " + this.getValue(),
89        this.getValue().equals(c.getValue()));
90      Assert.assertTrue("Expected colspan was " + c.getColspan()
91        + " but was " + this.getColspan(), this.getColspan() == c
92        .getColspan());
93      Assert.assertTrue("Expected rowspan was " + c.getRowspan()
94        + " but was " + this.getRowspan(), this.getRowspan() == c
95        .getRowspan());
96    }
97  
98    /**
99     * Assert that the current cell matches given one. Check colspan and rowspan. Regexp is in text of given cell.
100    *
101    * @param c given cell
102    */
103   public void assertMatch(Cell c) {
104     RE re = getRE(c.getValue());
105     Assert.assertTrue(c.getValue() + " do not match " + this.getValue(), re
106       .match(this.getValue()));
107     Assert.assertTrue("Expected colspan was " + c.getColspan()
108       + " but was " + this.getColspan(), this.getColspan() == c
109       .getColspan());
110     Assert.assertTrue("Expected rowspan was " + c.getRowspan()
111       + " but was " + this.getRowspan(), this.getRowspan() == c
112       .getRowspan());
113   }
114 
115   /**
116    * Check if the current cell contains given text.
117    *
118    * @param text given text.
119    * @return true if the current cell contains given text.
120    */
121   public boolean equals(String text) {
122     return this.getValue().equals(text);
123   }
124 
125   /**
126    * Check if the current cell matches given text.
127    *
128    * @param regexp given regexp.
129    * @return true if the current cell matches given text.
130    */
131   public boolean match(String regexp) {
132     RE re = getRE(regexp);
133     return re.match(this.getValue());
134   }
135 
136   /**
137    * Create a regexp.
138    *
139    * @param regexp regexp pattern
140    * @return regexp object
141    */
142   private RE getRE(String regexp) {
143     RE re = null;
144     try {
145       re = new RE(regexp, RE.MATCH_SINGLELINE);
146     } catch (RESyntaxException e) {
147       Assert.fail(e.toString());
148     }
149     return re;
150   }
151 }