KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > expectation > AbstractExpectationCollection


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

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