KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > constraint > IsEqualTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.core.constraint;
4
5 import org.jmock.core.Constraint;
6 import org.jmock.core.constraint.IsEqual;
7
8
9 public class IsEqualTest extends AbstractConstraintsTest
10 {
11     public void testComparesObjectsUsingEqualsMethod() {
12         Integer JavaDoc i1 = new Integer JavaDoc(1);
13         Integer JavaDoc i2 = new Integer JavaDoc(2);
14         Constraint c = new IsEqual(i1);
15
16         assertTrue(c.eval(i1));
17         assertTrue(c.eval(new Integer JavaDoc(1)));
18         assertTrue(!c.eval(i2));
19     }
20
21     public void testCanCompareNullValues() {
22         Integer JavaDoc i1 = new Integer JavaDoc(1);
23         Constraint c = new IsEqual(i1);
24
25         assertTrue(!c.eval(null));
26         Constraint nullEquals = new IsEqual(null);
27         assertTrue(nullEquals.eval(null));
28         assertTrue(!nullEquals.eval(i1));
29     }
30
31     public void testComparesTheElementsOfAnObjectArray() {
32         String JavaDoc[] s1 = new String JavaDoc[]{"a", "b"};
33         String JavaDoc[] s2 = new String JavaDoc[]{"a", "b"};
34         String JavaDoc[] s3 = new String JavaDoc[]{"c", "d"};
35         String JavaDoc[] s4 = new String JavaDoc[]{"a", "b", "c", "d"};
36
37         Constraint c = new IsEqual(s1);
38
39         assertTrue("Should equal itself", c.eval(s1));
40         assertTrue("Should equal a similar array", c.eval(s2));
41         assertTrue("Should not equal a different array", !c.eval(s3));
42         assertTrue("Should not equal a different sized array", !c.eval(s4));
43     }
44
45     public void testComparesTheElementsOfAnArrayOfPrimitiveTypes() {
46         int[] i1 = new int[]{1, 2};
47         int[] i2 = new int[]{1, 2};
48         int[] i3 = new int[]{3, 4};
49         int[] i4 = new int[]{1, 2, 3, 4};
50
51         Constraint c = new IsEqual(i1);
52
53         assertTrue("Should equal itself", c.eval(i1));
54         assertTrue("Should equal a similar array", c.eval(i2));
55         assertTrue("Should not equal a different array", !c.eval(i3));
56         assertTrue("Should not equal a different sized array", !c.eval(i4));
57     }
58
59     public void testRecursivelyTestsElementsOfArrays() {
60         int[][] i1 = new int[][]{{1, 2}, {3, 4}};
61         int[][] i2 = new int[][]{{1, 2}, {3, 4}};
62         int[][] i3 = new int[][]{{5, 6}, {7, 8}};
63         int[] i4 = new int[]{1, 2, 3, 4};
64         int[][] i5 = new int[][]{{1, 2, 3, 4}, {3, 4}};
65
66         Constraint c = new IsEqual(i1);
67
68         assertTrue("Should equal itself", c.eval(i1));
69         assertTrue("Should equal a similar array", c.eval(i2));
70         assertTrue("Should not equal a different array", !c.eval(i3));
71         assertTrue("Should not equal a different sized array", !c.eval(i4));
72         assertTrue("Should not equal a different sized subarray", !c.eval(i5));
73     }
74
75     public void testIncludesTheResultOfCallingToStringOnItsArgumentInTheDescription() {
76         final String JavaDoc argumentDescription = "ARGUMENT DESCRIPTION";
77         Object JavaDoc argument = new Object JavaDoc()
78         {
79             public String JavaDoc toString() {
80                 return argumentDescription;
81             }
82         };
83         Constraint c = new IsEqual(argument);
84
85         assertTrue("should contain argument's toString in toString result",
86                    c.describeTo(new StringBuffer JavaDoc()).toString().indexOf(argumentDescription) >= 0);
87     }
88
89     public void testReturnsAnObviousDescriptionIfCreatedWithANestedConstraintByMistake() {
90         IsEqual innerConstraint = new IsEqual("NestedConstraint");
91         assertEquals("should get an obvious description to reflect nesting if viewed in a debugger",
92                      "eq(<" + innerConstraint.toString() + ">)",
93                      (new IsEqual(innerConstraint)).describeTo(new StringBuffer JavaDoc()).toString());
94     }
95
96     public void testReturnsGoodDescriptionIfCreatedWithNullReference() {
97         assertEquals("should get a description even if argument is null",
98                      "eq(null)",
99                      new IsEqual(null).describeTo(new StringBuffer JavaDoc()).toString());
100     }
101 }
102
103
Popular Tags