KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jmock.expectation;
4
5 import java.util.Enumeration JavaDoc;
6 import java.util.Hashtable JavaDoc;
7 import org.jmock.core.Verifiable;
8
9
10 /**
11  * @version $Revision: 1.5 $
12  */

13 public class ReturnObjectMap extends MockObject
14 {
15     private final Hashtable JavaDoc returnValues = new Hashtable JavaDoc();
16     private final String JavaDoc name;
17
18     public ReturnObjectMap( String JavaDoc name ) {
19         this.name = name;
20     }
21
22     public Object JavaDoc getValue( Object JavaDoc key ) {
23         if (key == null) {
24             key = Null.NULL;
25         }
26         AssertMo.assertTrue(name + " does not contain " + key.toString(), returnValues.containsKey(key));
27         return ((CallCounter)returnValues.get(key)).getValue();
28     }
29
30     public Object JavaDoc getValue( short key ) {
31         return getValue(new Short JavaDoc(key));
32     }
33
34     public void putReturnValue( Object JavaDoc key, Object JavaDoc value ) {
35         returnValues.put(key != null ? key : Null.NULL, new CallCounter(key, value));
36     }
37
38     public void putReturnValue( Object JavaDoc key, int value ) {
39         putReturnValue(key, new Integer JavaDoc(value));
40     }
41
42     public void putReturnValue( short key, Object JavaDoc value ) {
43         putReturnValue(new Short JavaDoc(key), value);
44     }
45
46     public void putReturnValue( Object JavaDoc key, boolean value ) {
47         putReturnValue(key, new Boolean JavaDoc(value));
48     }
49
50     public int getIntValue( Object JavaDoc key ) {
51         return ((Integer JavaDoc)getValue(key)).intValue();
52     }
53
54     public boolean getBooleanValue( String JavaDoc key ) {
55         return ((Boolean JavaDoc)getValue(key)).booleanValue();
56     }
57
58     private class CallCounter implements Verifiable
59     {
60         private int count = 0;
61         private final Object JavaDoc value;
62         private final Object JavaDoc key;
63
64         public CallCounter( Object JavaDoc key, Object JavaDoc value ) {
65             this.key = key;
66             this.value = value;
67         }
68
69         public Object JavaDoc getValue() {
70             count++;
71             return value;
72         }
73
74         public void verify() {
75             AssertMo.assertTrue("Object never called for key: " + key, count > 0);
76         }
77     }
78
79     public void verify() {
80         super.verify();
81
82         for (Enumeration JavaDoc enumeration = returnValues.elements(); enumeration.hasMoreElements();) {
83             ((Verifiable)enumeration.nextElement()).verify();
84         }
85     }
86 }
87
Popular Tags