    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.expression.Expression;
    21
    22  /***
    23   * Compares an actual object against an expected object and if they are different
    24   * then the test will fail.
    25   *
    26   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    27   * @version $Revision: 155420 $
    28   */
    29  public class AssertEqualsTag extends AssertTagSupport {
    30
    31      private Expression actual;
    32      private Expression expected;
    33
    34
    35      // Tag interface
    36      //-------------------------------------------------------------------------
    37      public void doTag(XMLOutput output) throws JellyTagException {
    38          String message = getBodyText();
    39
    40          Object expectedValue = expected.evaluate(context);
    41          Object actualValue = actual.evaluate(context);
    42
    43          if (expectedValue == null && actualValue == null) {
    44              return;
    45          }
    46          if (actualValue != null && expectedValue.equals(actualValue)) {
    47              return;
    48          }
    49
    50          String expressions = "\nExpected expression: "
    51              + expected.getExpressionText()
    52              + "\nActual expression: "
    53              + actual.getExpressionText();
    54
    55          failNotEquals(message, expectedValue, actualValue, expressions);
    56      }
    57
    58      // Properties
    59      //-------------------------------------------------------------------------
    60
    61      /***
    62       * Sets the actual value which will be compared against the
    63       * expected value.
    64       */
    65      public void setActual(Expression actual) {
    66          this.actual = actual;
    67      }
    68
    69      /***
    70       * Sets the expected value to be tested against
    71       */
    72      public void setExpected(Expression expected) {
    73          this.expected = expected;
    74      }
    75  }