    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.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: "+ 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      // Properties
    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  }