1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.junit;
17
18 import org.apache.commons.jelly.JellyTagException;
19 import org.apache.commons.jelly.XMLOutput;
20 import org.apache.commons.jelly.MissingAttributeException;
21 import org.apache.commons.jelly.expression.Expression;
22
23 import org.jaxen.JaxenException;
24 import org.jaxen.XPath;
25
26 /***
27 * Performs an assertion that a given boolean expression, or XPath expression is
28 * true. If the expression returns false then this test fails.
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155420 $
32 */
33 public class AssertTag extends AssertTagSupport {
34
35 /*** The expression to evaluate. */
36 private Expression test;
37
38 /*** The XPath expression to evaluate */
39 private XPath xpath;
40
41 public AssertTag() {
42 }
43
44
45
46 public void doTag(XMLOutput output) throws JellyTagException {
47 if (test == null && xpath == null) {
48 throw new MissingAttributeException( "test" );
49 }
50 if (test != null) {
51 if (! test.evaluateAsBoolean(context)) {
52 fail( getBodyText(), "evaluating test: "+ test.getExpressionText() );
53 }
54 }
55 else {
56 try {
57 Object xpathContext = getXPathContext();
58 if (! xpath.booleanValueOf(xpathContext)) {
59 fail( getBodyText(), "evaluating xpath: "+ xpath );
60 }
61 } catch (JaxenException anException) {
62 throw new JellyTagException("Error evaluating xpath", anException);
63 }
64
65 }
66
67 }
68
69
70
71
72 /***
73 * Sets the boolean expression to evaluate. If this expression returns true
74 * then the test succeeds otherwise if it returns false then the text will
75 * fail with the content of the tag being the error message.
76 */
77 public void setTest(Expression test) {
78 this.test = test;
79 }
80
81 /***
82 * Sets the boolean XPath expression to evaluate. If this expression returns true
83 * then the test succeeds otherwise if it returns false then the text will
84 * fail with the content of the tag being the error message.
85 */
86 public void setXpath(XPath xpath) {
87 this.xpath = xpath;
88 }
89 }