KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > expectation > AbstractTestExpectationCollection


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

3 package test.jmock.expectation;
4
5 import java.util.Vector JavaDoc;
6 import junit.framework.AssertionFailedError;
7 import junit.framework.TestCase;
8 import org.jmock.expectation.AssertMo;
9 import org.jmock.expectation.ExpectationCollection;
10
11
12 public abstract class AbstractTestExpectationCollection extends TestCase
13 {
14     ExpectationCollection myExpectation;
15
16     public void testEmpty() {
17         myExpectation.verify();
18     }
19
20     public void testFailImmediately() {
21         myExpectation.addExpected("A");
22         myExpectation.addExpected("B");
23
24         myExpectation.addActual("A");
25         try {
26             myExpectation.addActual("C");
27         }
28         catch (AssertionFailedError ex) {
29             return;
30         }
31         fail("Should have failed immediately");
32     }
33
34     public void testFailImmediatelyAddingTooMany() {
35         myExpectation.addExpected("A");
36
37         myExpectation.addActual("A");
38         try {
39             myExpectation.addActual("C");
40         }
41         catch (AssertionFailedError ex) {
42             return;
43         }
44         fail("Should have failed immediately");
45     }
46
47     public void testFailOnSizes() {
48         myExpectation.addExpected("A");
49         myExpectation.addExpected("B");
50
51         myExpectation.addActual("A");
52         myExpectation.addActual("B");
53
54         try {
55             myExpectation.addActual("C");
56         }
57         catch (AssertionFailedError ex) {
58             return;
59         }
60         fail("Should have failed immediately");
61     }
62
63     public void testFailOnVerify() {
64         myExpectation.setFailOnVerify();
65         myExpectation.addExpectedMany(new String JavaDoc[]{"A", "B"});
66
67         myExpectation.addActualMany(new String JavaDoc[]{"C", "A"});
68
69         AssertMo.assertVerifyFails(myExpectation);
70     }
71
72     public void testFlushActual() {
73
74         myExpectation.addActual("a value");
75
76         myExpectation.setExpectNothing();
77         myExpectation.verify();
78     }
79
80     public void testHasExpectations() {
81         assertTrue("Should not have any expectations",
82                    !myExpectation.hasExpectations());
83
84         myExpectation.addExpected("item");
85
86         assertTrue("Should have an expectation", myExpectation.hasExpectations());
87     }
88
89     public void testHasExpectationsForAddingManyArray() {
90         assertTrue("Should not have any expectations",
91                    !myExpectation.hasExpectations());
92
93         myExpectation.addExpectedMany(new Object JavaDoc[0]);
94
95         assertTrue("Should have an expectation", myExpectation.hasExpectations());
96     }
97
98     public void testHasExpectationsForAddingManyVector() {
99         assertTrue("Should not have any expectations",
100                    !myExpectation.hasExpectations());
101
102         myExpectation.addExpectedMany(new Vector JavaDoc().elements());
103
104         assertTrue("Should have an expectation", myExpectation.hasExpectations());
105     }
106
107     public void testHasNoExpectations() {
108
109         myExpectation.addActual("a value");
110         assertTrue("Has no expectations", !myExpectation.hasExpectations());
111     }
112
113     public void testManyFromEnumeration() {
114         Vector JavaDoc expectedItems = new Vector JavaDoc();
115         expectedItems.addElement("A");
116         expectedItems.addElement("B");
117
118         Vector JavaDoc actualItems = (Vector JavaDoc)expectedItems.clone();
119
120         myExpectation.addExpectedMany(expectedItems.elements());
121
122         myExpectation.addActualMany(actualItems.elements());
123
124         myExpectation.verify();
125     }
126
127     public void testManyFromIterator() {
128         Vector JavaDoc expectedItems = new Vector JavaDoc();
129         expectedItems.addElement("A");
130         expectedItems.addElement("B");
131
132         Vector JavaDoc actualItems = (Vector JavaDoc)expectedItems.clone();
133
134         myExpectation.addExpectedMany(expectedItems.iterator());
135
136         myExpectation.addActualMany(actualItems.iterator());
137
138         myExpectation.verify();
139     }
140
141     public void testMultiFailureFromEnumeration() {
142         Vector JavaDoc expectedItems = new Vector JavaDoc();
143         expectedItems.addElement("A");
144         expectedItems.addElement("B");
145
146         Vector JavaDoc actualItems = new Vector JavaDoc();
147         actualItems.addElement("A");
148         actualItems.addElement("C");
149
150         myExpectation.addExpectedMany(expectedItems.elements());
151         myExpectation.setFailOnVerify();
152
153         myExpectation.addActualMany(actualItems.elements());
154
155         AssertMo.assertVerifyFails(myExpectation);
156     }
157
158     public void testMultiFailureFromIterator() {
159         Vector JavaDoc expectedItems = new Vector JavaDoc();
160         expectedItems.addElement("A");
161         expectedItems.addElement("B");
162
163         Vector JavaDoc actualItems = new Vector JavaDoc();
164         actualItems.addElement("A");
165         actualItems.addElement("C");
166
167         myExpectation.addExpectedMany(expectedItems.iterator());
168         myExpectation.setFailOnVerify();
169
170         myExpectation.addActualMany(actualItems.iterator());
171
172         AssertMo.assertVerifyFails(myExpectation);
173     }
174
175     public void testMultiFailureSizes() {
176         myExpectation.addExpectedMany(new String JavaDoc[]{"A", "B"});
177         myExpectation.setFailOnVerify();
178
179         myExpectation.addActualMany(new String JavaDoc[]{"A", "B", "C"});
180
181         AssertMo.assertVerifyFails(myExpectation);
182     }
183
184     public void testExpectingALong() {
185         final long expectedLong = 666l;
186
187         myExpectation.addExpected(expectedLong);
188         myExpectation.addActual(expectedLong);
189
190         myExpectation.verify();
191     }
192 }
193
Popular Tags