KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > MockObjectSupportTestCaseTest


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

3 package test.jmock.core;
4
5 import junit.framework.TestCase;
6 import org.jmock.core.Constraint;
7 import org.jmock.core.MockObjectSupportTestCase;
8 import org.jmock.util.Dummy;
9 import test.jmock.core.testsupport.AlwaysFalse;
10 import test.jmock.core.testsupport.AlwaysTrue;
11
12
13 public class MockObjectSupportTestCaseTest extends TestCase
14 {
15     private static final String JavaDoc DUMMY_NAME = "DUMMY NAME";
16
17     Constraint trueConstraint = AlwaysTrue.INSTANCE;
18     Constraint falseConstraint = AlwaysFalse.INSTANCE;
19
20
21     interface ExampleInterface
22     {
23         void method1();
24     }
25
26
27     MockObjectSupportTestCase testCase;
28
29     public void setUp() {
30         testCase = new MockObjectSupportTestCase() {};
31     }
32
33     public void testCanBeConstructedWithAName() {
34         String JavaDoc name = "NAME";
35
36         testCase = new MockObjectSupportTestCase(name) {};
37
38         assertEquals("name", name, testCase.getName());
39     }
40
41     public void testCanCreateNamedDummyObjects() {
42         Object JavaDoc dummy = testCase.newDummy(DUMMY_NAME);
43
44         assertEquals("should return name from toString",
45                      DUMMY_NAME, dummy.toString());
46     }
47
48     public void testCanCreateNamedDummyObjectsThatImplementASpecificInterface() {
49         Object JavaDoc dummy = testCase.newDummy(ExampleInterface.class, DUMMY_NAME);
50
51         assertTrue("should be instanceof ExampleInterface",
52                    dummy instanceof ExampleInterface);
53         assertEquals("should return name from toString",
54                      DUMMY_NAME, dummy.toString());
55     }
56
57     public void testGeneratesUsefulNamesForDummiesFromTheDummiedInterface() {
58         Object JavaDoc dummy = testCase.newDummy(ExampleInterface.class);
59
60         assertEquals("should return name from toString",
61                      "dummyExampleInterface", dummy.toString());
62     }
63
64     public void testHasConvenienceConstantForIsAnything() {
65         assertConstraintTrue(MockObjectSupportTestCase.ANYTHING, new Object JavaDoc());
66         assertConstraintTrue(MockObjectSupportTestCase.ANYTHING, new Object JavaDoc());
67     }
68
69     public void testHasConvenienceConstantForIsNull() {
70         assertConstraintTrue(MockObjectSupportTestCase.NULL, null);
71         assertConstraintFalse(MockObjectSupportTestCase.NULL, "not null");
72     }
73
74     public void testHasConvenienceConstantForNotNull() {
75         assertConstraintTrue(MockObjectSupportTestCase.NOT_NULL, "not null");
76         assertConstraintFalse(MockObjectSupportTestCase.NOT_NULL, null );
77     }
78
79     public void testHasConvenienceMethodForCreatingIsEqualConstraints() {
80         String JavaDoc stringValue = new String JavaDoc("STRING VALUE");
81
82         assertConstraintTrue(testCase.eq(stringValue), stringValue);
83     }
84
85     public void testConvenienceMethodForCreatingIsEqualConstraintsIsOverloadedForPrimitiveTypes() {
86         assertConstraintTrue(testCase.eq(true), new Boolean JavaDoc(true));
87         assertConstraintTrue(testCase.eq(false), new Boolean JavaDoc(false));
88         assertConstraintTrue(testCase.eq((byte)1), new Byte JavaDoc((byte)1));
89         assertConstraintTrue(testCase.eq((short)1), new Short JavaDoc((short)1));
90         assertConstraintTrue(testCase.eq('a'), new Character JavaDoc('a'));
91         assertConstraintTrue(testCase.eq(1), new Integer JavaDoc(1));
92         assertConstraintTrue(testCase.eq(1L), new Long JavaDoc(1L));
93         assertConstraintTrue(testCase.eq(1.0F), new Float JavaDoc(1.0F));
94         assertConstraintTrue(testCase.eq(1.0), new Double JavaDoc(1.0));
95
96         assertConstraintFalse(testCase.eq(true), new Boolean JavaDoc(false));
97         assertConstraintFalse(testCase.eq(false), new Boolean JavaDoc(true));
98         assertConstraintFalse(testCase.eq((byte)1), new Byte JavaDoc((byte)2));
99         assertConstraintFalse(testCase.eq((short)1), new Short JavaDoc((short)2));
100         assertConstraintFalse(testCase.eq('a'), new Character JavaDoc('b'));
101         assertConstraintFalse(testCase.eq(1), new Integer JavaDoc(2));
102         assertConstraintFalse(testCase.eq(1L), new Long JavaDoc(2L));
103         assertConstraintFalse(testCase.eq(1.0F), new Float JavaDoc(2.0F));
104         assertConstraintFalse(testCase.eq(1.0), new Double JavaDoc(2.0));
105     }
106
107     public void testHasConvenienceMethodForCreatingIsSameConstraints() {
108         Object JavaDoc o1 = Dummy.newDummy("o1");
109         Object JavaDoc o2 = Dummy.newDummy("o2");
110
111         assertConstraintTrue(testCase.same(o1), o1);
112         assertConstraintFalse(testCase.same(o1), o2);
113     }
114
115     public void testHasConvenienceMethodForCreatingIsAConstraints() {
116         String JavaDoc aString = "a string";
117
118         assertConstraintTrue(testCase.isA(String JavaDoc.class), aString);
119         assertConstraintTrue(testCase.isA(Object JavaDoc.class), aString);
120         assertConstraintFalse(testCase.isA(Integer JavaDoc.class), aString);
121     }
122
123     public void testHasConvenienceMethodForCreatingStringContainsConstraints() {
124         assertConstraintTrue(testCase.stringContains("fruit"), "fruitcake");
125         assertConstraintFalse(testCase.stringContains("chocolate chips"), "fruitcake");
126     }
127
128     public void testHasConvenienceMethodForLogicalNegationOfConstraints() {
129         assertConstraintTrue(testCase.not(testCase.eq("hello")), "world");
130         assertConstraintFalse(testCase.not(testCase.eq("hello")), "hello");
131     }
132
133     public void testHasConvenienceMethodForLogicalConjunctionOfConstraints() {
134         Object JavaDoc ignored = new Object JavaDoc();
135
136         assertConstraintTrue(testCase.and(trueConstraint, trueConstraint), ignored);
137         assertConstraintFalse(testCase.and(trueConstraint, falseConstraint), ignored);
138         assertConstraintFalse(testCase.and(falseConstraint, trueConstraint), ignored);
139         assertConstraintFalse(testCase.and(falseConstraint, falseConstraint), ignored);
140     }
141
142     public void testHasConvenienceMethodForLogicalDisjunctionOfConstraints() {
143         Object JavaDoc ignored = new Object JavaDoc();
144
145         assertConstraintTrue(testCase.or(trueConstraint, trueConstraint), ignored);
146         assertConstraintTrue(testCase.or(trueConstraint, falseConstraint), ignored);
147         assertConstraintTrue(testCase.or(falseConstraint, trueConstraint), ignored);
148         assertConstraintFalse(testCase.or(falseConstraint, falseConstraint), ignored);
149     }
150
151     private void assertConstraintTrue( Constraint constraint, Object JavaDoc argument ) {
152         assertTrue("expected <" + constraint + "> to return true when passed <" + argument + ">",
153                    constraint.eval(argument));
154     }
155
156     private void assertConstraintFalse( Constraint constraint, Object JavaDoc argument ) {
157         assertFalse("expected <" + constraint + "> to return false when passed <" + argument + ">",
158                     constraint.eval(argument));
159     }
160 }
161
Popular Tags