KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > TestExpectationCollection


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