KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > AbstractExpectationCollection


1 package com.mockobjects;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.HashSet JavaDoc;
7
8 abstract public class AbstractExpectationCollection extends AbstractExpectation implements ExpectationCollection {
9
10     public AbstractExpectationCollection(String JavaDoc name) {
11         super(name);
12     }
13
14     public void addActual(Object JavaDoc actualItem) {
15         getActualCollection().add(actualItem);
16         if (shouldCheckImmediately()) {
17             checkImmediateValues(actualItem);
18         }
19     }
20
21     public void addActual(int actualItem) {
22         addActual(new Integer JavaDoc(actualItem));
23     }
24
25     public void addActualMany(Object JavaDoc[] items) {
26         if(items == null) return;
27         
28         for (int i = 0; i < items.length; ++i) {
29             addActual(items[i]);
30         }
31     }
32
33     public void addActualMany(Enumeration JavaDoc items) {
34         while (items.hasMoreElements()) {
35             addActual(items.nextElement());
36         }
37     }
38
39     public void addActualMany(Iterator items) {
40         while (items.hasNext()) {
41             addActual(items.next());
42         }
43     }
44
45     public void addExpected(int expectedItem) {
46         addExpected(new Integer JavaDoc(expectedItem));
47     }
48
49     public void addExpected(Object JavaDoc expectedItem) {
50         getExpectedCollection().add(expectedItem);
51         setHasExpectations();
52     }
53
54     public void addExpectedMany(Object JavaDoc[] expectedItems) {
55         for (int i = 0; i < expectedItems.length; ++i) {
56             addExpected(expectedItems[i]);
57         }
58         setHasExpectations();
59     }
60
61     public void addExpectedMany(Enumeration JavaDoc expectedItems) {
62         while (expectedItems.hasMoreElements()) {
63             addExpected(expectedItems.nextElement());
64         }
65         setHasExpectations();
66     }
67
68     public void addExpectedMany(Iterator expectedItems) {
69         while (expectedItems.hasNext()) {
70             addExpected(expectedItems.next());
71         }
72         setHasExpectations();
73     }
74
75     abstract protected void checkImmediateValues(Object JavaDoc actualItem);
76
77     public void clearActual() {
78         getActualCollection().clear();
79     }
80
81     protected void clearExpectation() {
82         getExpectedCollection().clear();
83     }
84
85     abstract protected Collection JavaDoc getActualCollection();
86
87     abstract protected Collection JavaDoc getExpectedCollection();
88
89     public void setExpectNothing() {
90         clearExpectation();
91         setHasExpectations();
92     }
93
94     public void verify() {
95         assertEquals(
96             "did not receive the expected collection items.",
97             new HashSet(getExpectedCollection()),
98             new HashSet(getActualCollection()));
99     }
100 }
101
Popular Tags