View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.plugin.junit.tags;
20  
21  /***
22   * Performs an assertEquals on two variables. This tag, along with all JUnit tags
23   * can be used inside any other plug-in's session tag
24   * 
25   * To compare two variables with a default JUnit error message on a failure:
26   * 
27   * <pre><source>
28   * &lt;testcase xmlns="jelly:jameleon"&gt;
29   *     &lt;ju-session application="someApp"&gt;
30   *       &lt;ju-assert-equals
31   *           functionId="Check that var1 equals var2."
32   *           expected="${var1}"
33   *           actual="${var2}"/&gt;
34   *     &lt;/ju-session&gt;
35   * &lt;/testcase&gt;
36   * </source></pre>
37   *
38   * To compare two variables that gives "First Name" at the beginning of the failure message:
39   * 
40   * <pre><source>
41   * &lt;testcase xmlns="jelly:jameleon"&gt;
42   *     &lt;jiffie-session application="someApp"&gt;
43   *       &lt;ju-assert-equals
44   *           functionId="Check that var1 equals var2."
45   *           expected="${var1}"
46   *           actual="${var2}"
47   *           msg="First Name"/&gt;
48   *     &lt;/jiffie-session&gt;
49   * &lt;/testcase&gt;
50   * </source></pre>
51   *
52   * @jameleon.function name="ju-assert-equals" type="action"
53   * @jameleon.step Compare the expected value against the actual value.
54   */
55  public class AssertEqualsTag extends AbstractAssertTag{
56  
57      /***
58       * The expected value
59       * @jameleon.attribute
60       */
61      protected Object expected;
62      /***
63       * The actual value
64       * @jameleon.attribute
65       */
66      protected Object actual;
67      /***
68       * Forces the values to be compared as Strings by calling the toString() method.
69       * @jameleon.attribute default="false"
70       */
71      protected boolean valuesAreStrings;
72  
73      public void testBlock(){
74          if (valuesAreStrings || 
75              (expected != null && actual != null && 
76               !expected.getClass().isInstance(actual))) {
77              if (msg != null) {
78                  assertEquals(msg, expected.toString(), actual.toString());
79              }else{
80                  assertEquals(expected.toString(), actual.toString());
81              }
82          } else{
83              if (msg != null) {
84                  assertEquals(msg, expected, actual);
85              }else{
86                  assertEquals(expected, actual);
87              }
88          }
89      }
90  
91  }