|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AssertTableContainsTagHandler.java | 57.1% | 79.5% | 80% | 74.6% |
|
1 |
/*
|
|
2 |
* Created on Oct 27, 2003
|
|
3 |
*
|
|
4 |
* To change the template for this generated file go to
|
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
|
6 |
*/
|
|
7 |
package junit.extensions.xml.elements;
|
|
8 |
|
|
9 |
import junit.extensions.xml.IXMLTestCase;
|
|
10 |
import junit.extensions.xml.XMLException;
|
|
11 |
|
|
12 |
import junit.framework.Assert;
|
|
13 |
|
|
14 |
import org.apache.regexp.RE;
|
|
15 |
import org.apache.regexp.RESyntaxException;
|
|
16 |
|
|
17 |
import org.w3c.dom.Element;
|
|
18 |
|
|
19 |
import javax.swing.JTable;
|
|
20 |
|
|
21 |
|
|
22 |
/**
|
|
23 |
* Provide a mechanism for validating the contents of a table.
|
|
24 |
*
|
|
25 |
* <H3>Tag Name:</H3>
|
|
26 |
* asserttablecontains
|
|
27 |
* <H3>Attributes:</H3>
|
|
28 |
* <pre>
|
|
29 |
* id - the table to inspect [required]
|
|
30 |
* value - the expected value [required]
|
|
31 |
* row - the row to inspect [required]
|
|
32 |
* col - the column to inspect (int index or column name)[required]
|
|
33 |
* useRE - allow the value to contain a regular expression [optional]
|
|
34 |
* default - is false exact string match needed
|
|
35 |
* If set to true partial strings will work.
|
|
36 |
* </pre>
|
|
37 |
* <H3>Examples:</H3>
|
|
38 |
* <assertTableContains id="component1" value="Joe"/>
|
|
39 |
* @author Kevin Wilson
|
|
40 |
*/
|
|
41 |
public class AssertTableContainsTagHandler extends AbstractTagHandler { |
|
42 |
/**
|
|
43 |
* Constructor.
|
|
44 |
* @param element Element to be processed.
|
|
45 |
* @param testcase TestCase containing the element.
|
|
46 |
*/
|
|
47 | 6 |
public AssertTableContainsTagHandler(final Element element,
|
48 |
final IXMLTestCase testcase) { |
|
49 | 6 |
super(element, testcase);
|
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Get the column index from the attibute column.
|
|
54 |
* The attribute value may be a integer index of the
|
|
55 |
* column. Or the name of the column according to the
|
|
56 |
* table model.
|
|
57 |
* @param table JTable containing the data.
|
|
58 |
* @return index of the column.
|
|
59 |
*/
|
|
60 | 4 |
public int getColumn(final JTable table) { |
61 | 4 |
String col = getString(COLUMN); |
62 |
|
|
63 | 4 |
try {
|
64 | 4 |
int i = Integer.parseInt(col);
|
65 |
|
|
66 | 4 |
return i;
|
67 |
} catch (NumberFormatException e) {
|
|
68 | 0 |
return getColumnIndex(col, table);
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* Handle the XML processing of the tag 'assertTableContains'.
|
|
74 |
* @throws XMLException when assert fails.
|
|
75 |
*/
|
|
76 | 5 |
public void processElement() throws XMLException { |
77 | 5 |
validateElement(); |
78 |
|
|
79 | 5 |
String id = getString(ID); |
80 |
|
|
81 | 5 |
Object o = ((IXMLTestCase) getTestCase()).getProperty(id); |
82 |
|
|
83 | 5 |
if (o == null) { |
84 | 0 |
Assert.fail("Error: Unable to locate object id:" + id);
|
85 |
} else {
|
|
86 | 5 |
if (o instanceof JTable) { |
87 | 4 |
String expectedval = getString(VALUE); |
88 | 4 |
JTable table = (JTable) o; |
89 | 4 |
int row = getInt(ROW, -1);
|
90 | 4 |
Assert.assertTrue(row != -1); |
91 | 4 |
Assert.assertTrue(row < table.getRowCount()); |
92 |
|
|
93 | 4 |
int col = getColumn(table);
|
94 | 4 |
Assert.assertTrue(col != -1); |
95 | 4 |
Assert.assertTrue(row < table.getRowCount()); |
96 |
|
|
97 | 4 |
String tableval = table.getValueAt(row, col).toString(); |
98 |
|
|
99 | 4 |
String msg = "table contains[" + row + "," + col |
100 |
+ "] Component: " + id + " Expected: " + expectedval |
|
101 |
+ " Retrieved: " + tableval;
|
|
102 |
|
|
103 | 4 |
if (getXMLTestCase().getDebug()) {
|
104 | 0 |
System.out.println("msg: " + msg);
|
105 |
} |
|
106 |
|
|
107 | 4 |
boolean useRegExp = getBoolean(USERE);
|
108 |
|
|
109 | 4 |
if (useRegExp) {
|
110 | 2 |
RE regexp = null;
|
111 |
|
|
112 | 2 |
try {
|
113 | 2 |
regexp = new RE(expectedval);
|
114 |
} catch (RESyntaxException ex) {
|
|
115 | 0 |
throw new XMLException( |
116 |
"Could not create regular expression:"
|
|
117 |
+ expectedval, |
|
118 |
ex, |
|
119 |
getElement(), |
|
120 |
getXMLTestCase().getPropertyCache()); |
|
121 |
} |
|
122 |
|
|
123 | 2 |
Assert.assertTrue( |
124 |
msg, |
|
125 |
regexp.match(tableval)); |
|
126 |
} else {
|
|
127 | 2 |
if (!tableval.equals(expectedval)) {
|
128 | 1 |
System.out.println("Assertion failed: " + msg);
|
129 |
} |
|
130 |
|
|
131 | 2 |
Assert.assertEquals(expectedval, tableval); |
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* Insure that the required attributes are specified.
|
|
139 |
* @throws XMLException if a required attribute is missing.
|
|
140 |
*/
|
|
141 | 5 |
public void validateElement() throws XMLException { |
142 | 5 |
super.validateElement();
|
143 |
|
|
144 |
// id is a required attribute
|
|
145 | 5 |
checkRequiredAttribute(ID); |
146 |
|
|
147 | 5 |
checkRequiredAttribute(ROW); |
148 | 5 |
checkRequiredAttribute(COLUMN); |
149 |
|
|
150 |
// value is a required attribute
|
|
151 | 5 |
checkRequiredAttribute(VALUE); |
152 |
} |
|
153 |
|
|
154 |
/**
|
|
155 |
* Get the column index matching the name.
|
|
156 |
* @param columnName Name of the column.
|
|
157 |
* @param table JTable Table to search.
|
|
158 |
* @return int index of the column.
|
|
159 |
*/
|
|
160 | 0 |
private int getColumnIndex(final String columnName, final JTable table) { |
161 | 0 |
int count = table.getColumnCount();
|
162 |
|
|
163 | 0 |
for (int index = 0; index < count; index++) { |
164 | 0 |
if (columnName.equals(table.getColumnName(index))) {
|
165 | 0 |
return index;
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 | 0 |
return -1;
|
170 |
} |
|
171 |
} |
|
172 |
|
|