|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AssertTextFieldContainsTagHandler.java | 87.5% | 91.7% | 100% | 91.4% |
|
||||||||||||||
| 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.text.JTextComponent;
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
/**
|
|
| 23 |
* Provide a mechanism for validating the contents of a text field.
|
|
| 24 |
*
|
|
| 25 |
* <H3>Tag Name:</H3>
|
|
| 26 |
* AssertTextFieldContains
|
|
| 27 |
* <H3>Attributes:</H3>
|
|
| 28 |
* <pre>
|
|
| 29 |
* id - the text field to validate [required]
|
|
| 30 |
* value - the expected value in the text field [required]
|
|
| 31 |
* useRE - allow the value to contain a regular expression [optional]
|
|
| 32 |
* default - is false exact string match needed
|
|
| 33 |
* If set to true partial strings will work.
|
|
| 34 |
* </pre>
|
|
| 35 |
* <H3>Examples:</H3>
|
|
| 36 |
* <assertTextFieldContains id="component1" value="Joe"/>
|
|
| 37 |
* @author John Yunker
|
|
| 38 |
*/
|
|
| 39 |
public class AssertTextFieldContainsTagHandler extends AbstractTagHandler { |
|
| 40 |
/**
|
|
| 41 |
* Constructor.
|
|
| 42 |
* @param element Element to be processed by this tag handler.
|
|
| 43 |
* @param testcase containing test case.
|
|
| 44 |
*/
|
|
| 45 | 6 |
public AssertTextFieldContainsTagHandler(final Element element,
|
| 46 |
final IXMLTestCase testcase) {
|
|
| 47 | 6 |
super(element, testcase);
|
| 48 |
} |
|
| 49 |
|
|
| 50 |
/**
|
|
| 51 |
* Handle the XML processing of the tag 'assertTextFieldContains'.
|
|
| 52 |
* @throws XMLException is thrown if the element cannot be understood.
|
|
| 53 |
*/
|
|
| 54 | 5 |
public void processElement() throws XMLException { |
| 55 | 5 |
validateElement(); |
| 56 |
|
|
| 57 | 5 |
String id = getString(ID); |
| 58 |
|
|
| 59 | 5 |
Object o = ((IXMLTestCase) getTestCase()).getProperty(id); |
| 60 |
|
|
| 61 | 5 |
if (o == null) { |
| 62 | 0 |
System.out.println("Error: Unable to locate object id:" + id);
|
| 63 |
} else {
|
|
| 64 | 5 |
if (o instanceof JTextComponent) { |
| 65 | 4 |
String expectedval = getString(VALUE); |
| 66 | 4 |
JTextComponent jtf = (JTextComponent) o; |
| 67 |
|
|
| 68 | 4 |
String msg = "Component: " + id + " Expected: " |
| 69 |
+ expectedval + " Retrieved: " + jtf.getText();
|
|
| 70 |
|
|
| 71 | 4 |
boolean useRegExp = getBoolean(USERE);
|
| 72 |
|
|
| 73 | 4 |
if (useRegExp) {
|
| 74 | 2 |
RE regexp = null;
|
| 75 |
|
|
| 76 | 2 |
try {
|
| 77 | 2 |
regexp = new RE(expectedval);
|
| 78 |
} catch (RESyntaxException ex) {
|
|
| 79 | 0 |
throw new XMLException( |
| 80 |
"Could not create regular expression:"
|
|
| 81 |
+ expectedval, |
|
| 82 |
ex, |
|
| 83 |
getElement(), |
|
| 84 |
getXMLTestCase().getPropertyCache()); |
|
| 85 |
} |
|
| 86 |
|
|
| 87 | 2 |
Assert.assertTrue( |
| 88 |
msg, |
|
| 89 |
regexp.match(jtf.getText())); |
|
| 90 |
} else {
|
|
| 91 | 2 |
if (!jtf.getText().equals(expectedval)) {
|
| 92 | 1 |
System.out.println("Assertion failed: " + msg);
|
| 93 |
} |
|
| 94 |
|
|
| 95 | 2 |
Assert.assertEquals( |
| 96 |
jtf.getText(), |
|
| 97 |
expectedval); |
|
| 98 |
} |
|
| 99 |
} else {
|
|
| 100 | 1 |
throw new XMLException("id does not reference a JTextComponent", |
| 101 |
null,
|
|
| 102 |
getElement(), |
|
| 103 |
getTest().getPropertyCache()); |
|
| 104 |
} |
|
| 105 |
} |
|
| 106 |
} |
|
| 107 |
|
|
| 108 |
/**
|
|
| 109 |
* Make sure the appropriate tag and attributes are used.
|
|
| 110 |
* @throws XMLException may be thrown.
|
|
| 111 |
*/
|
|
| 112 | 5 |
public void validateElement() throws XMLException { |
| 113 | 5 |
super.validateElement();
|
| 114 |
|
|
| 115 |
// id is a required attribute
|
|
| 116 | 5 |
checkRequiredAttribute(ID); |
| 117 |
|
|
| 118 |
// value is a required attribute
|
|
| 119 | 5 |
checkRequiredAttribute(VALUE); |
| 120 |
} |
|
| 121 |
} |
|
| 122 |
|
|
||||||||||