01: /*
02: * Copyright 2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
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: // Tag interface
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: "
53: + test.getExpressionText());
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",
63: anException);
64: }
65:
66: }
67:
68: }
69:
70: // Properties
71: //-------------------------------------------------------------------------
72:
73: /**
74: * Sets the boolean expression to evaluate. If this expression returns true
75: * then the test succeeds otherwise if it returns false then the text will
76: * fail with the content of the tag being the error message.
77: */
78: public void setTest(Expression test) {
79: this .test = test;
80: }
81:
82: /**
83: * Sets the boolean XPath expression to evaluate. If this expression returns true
84: * then the test succeeds otherwise if it returns false then the text will
85: * fail with the content of the tag being the error message.
86: */
87: public void setXpath(XPath xpath) {
88: this.xpath = xpath;
89: }
90: }
|