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 junit.framework.Assert;
007:
008: import org.apache.regexp.RE;
009: import org.apache.regexp.RESyntaxException;
010:
011: /**
012: * Represents a cell of an html table - a string value spanning an indicated amount of columns.
013: *
014: * @author Jim Weaver
015: * @author Julien Henry
016: */
017: public class Cell {
018:
019: private int colspan;
020:
021: private int rowspan;
022:
023: private String value;
024:
025: /**
026: * Construct a cell with a default colspan/rowspan of 1.
027: *
028: * @param value text expected within the cell.
029: */
030: public Cell(String value) {
031: this (value, 1, 1);
032: }
033:
034: /**
035: * Construct a cell with a specified colspan.
036: *
037: * @param value text expected within the cell.
038: * @param colspan number of columns the cell is expected to span.
039: * @param rowspan number of rows the cell is expected to span.
040: */
041: public Cell(String value, int colspan, int rowspan) {
042: this .value = value;
043: this .colspan = colspan;
044: this .rowspan = rowspan;
045: }
046:
047: /**
048: * @return the colspan for this cell.
049: */
050: public int getColspan() {
051: return colspan;
052: }
053:
054: /**
055: * @return the rowspan for this cell.
056: */
057: public int getRowspan() {
058: return rowspan;
059: }
060:
061: /**
062: * @return the text for the cell.
063: */
064: public final String getValue() {
065: return value;
066: }
067:
068: /**
069: * Assert that the current cell equals given one. Check text, colspan and rowspan.
070: *
071: * @param c given cell
072: */
073: public void assertEquals(Cell c) {
074: Assert
075: .assertTrue(c.getValue() + " do not equal "
076: + this .getValue(), this .getValue().equals(
077: c.getValue()));
078: Assert.assertTrue("Expected colspan was " + c.getColspan()
079: + " but was " + this .getColspan(),
080: this .getColspan() == c.getColspan());
081: Assert.assertTrue("Expected rowspan was " + c.getRowspan()
082: + " but was " + this .getRowspan(),
083: this .getRowspan() == c.getRowspan());
084: }
085:
086: /**
087: * Assert that the current cell matches given one. Check colspan and rowspan. Regexp is in text of given cell.
088: *
089: * @param c given cell
090: */
091: public void assertMatch(Cell c) {
092: RE re = getRE(c.getValue());
093: Assert.assertTrue(c.getValue() + " do not match "
094: + this .getValue(), re.match(this .getValue()));
095: Assert.assertTrue("Expected colspan was " + c.getColspan()
096: + " but was " + this .getColspan(),
097: this .getColspan() == c.getColspan());
098: Assert.assertTrue("Expected rowspan was " + c.getRowspan()
099: + " but was " + this .getRowspan(),
100: this .getRowspan() == c.getRowspan());
101: }
102:
103: /**
104: * Check if the current cell contains given text.
105: *
106: * @param text given text.
107: * @return true if the current cell contains given text.
108: */
109: public boolean equals(String text) {
110: return this .getValue().equals(text);
111: }
112:
113: /**
114: * Check if the current cell matches given text.
115: *
116: * @param regexp given regexp.
117: * @return true if the current cell matches given text.
118: */
119: public boolean match(String regexp) {
120: RE re = getRE(regexp);
121: return re.match(this .getValue());
122: }
123:
124: /**
125: * Create a regexp.
126: *
127: * @param regexp regexp pattern
128: * @return regexp object
129: */
130: private RE getRE(String regexp) {
131: RE re = null;
132: try {
133: re = new RE(regexp, RE.MATCH_SINGLELINE);
134: } catch (RESyntaxException e) {
135: Assert.fail(e.toString());
136: }
137: return re;
138: }
139: }
|