View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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.MissingAttributeException;
20  import org.apache.commons.jelly.XMLOutput;
21  import org.apache.commons.jelly.expression.Expression;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.jaxen.JaxenException;
27  import org.jaxen.XPath;
28  
29  /***
30   * Performs an assertion that a given boolean expression, or XPath expression is
31   * true. If the expression returns false then this test fails.
32   *
33   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
34   * @version $Revision: 155420 $
35   */
36  public class AssertTag extends AssertTagSupport {
37  
38      /*** The Log to which logging calls will be made. */
39      private static final Log log = LogFactory.getLog(AssertTag.class);
40  
41      /*** The expression to evaluate. */
42      private Expression test;
43  
44      /*** The XPath expression to evaluate */
45      private XPath xpath;
46  
47      public AssertTag() {
48      }
49  
50      // Tag interface
51      //-------------------------------------------------------------------------
52      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
53          if (test == null && xpath == null) {
54              throw new MissingAttributeException( "test" );
55          }
56          if (test != null) {
57              if (! test.evaluateAsBoolean(context)) {
58                  fail( getBodyText(), "evaluating test: "+ test.getExpressionText() );
59              }
60          }
61          else {
62              Object xpathContext = getXPathContext();
63              try {
64                  if (! xpath.booleanValueOf(xpathContext)) {
65                      fail( getBodyText(), "evaluating xpath: "+ xpath );
66                  }
67              }
68              catch (JaxenException e) {
69                  throw new JellyTagException(e);
70              }
71          }
72  
73      }
74  
75      // Properties
76      //-------------------------------------------------------------------------
77  
78      /***
79       * Sets the boolean expression to evaluate. If this expression returns true
80       * then the test succeeds otherwise if it returns false then the text will
81       * fail with the content of the tag being the error message.
82       */
83      public void setTest(Expression test) {
84          this.test = test;
85      }
86  
87      /***
88       * Sets the boolean XPath expression to evaluate. If this expression returns true
89       * then the test succeeds otherwise if it returns false then the text will
90       * fail with the content of the tag being the error message.
91       */
92      public void setXpath(XPath xpath) {
93          this.xpath = xpath;
94      }
95  }