1 3 package org.jmock.expectation; 4 5 import java.util.Enumeration ; 6 import java.util.Hashtable ; 7 import org.jmock.core.Verifiable; 8 9 10 13 public class ReturnObjectMap extends MockObject 14 { 15 private final Hashtable returnValues = new Hashtable (); 16 private final String name; 17 18 public ReturnObjectMap( String name ) { 19 this.name = name; 20 } 21 22 public Object getValue( Object 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 getValue( short key ) { 31 return getValue(new Short (key)); 32 } 33 34 public void putReturnValue( Object key, Object value ) { 35 returnValues.put(key != null ? key : Null.NULL, new CallCounter(key, value)); 36 } 37 38 public void putReturnValue( Object key, int value ) { 39 putReturnValue(key, new Integer (value)); 40 } 41 42 public void putReturnValue( short key, Object value ) { 43 putReturnValue(new Short (key), value); 44 } 45 46 public void putReturnValue( Object key, boolean value ) { 47 putReturnValue(key, new Boolean (value)); 48 } 49 50 public int getIntValue( Object key ) { 51 return ((Integer )getValue(key)).intValue(); 52 } 53 54 public boolean getBooleanValue( String key ) { 55 return ((Boolean )getValue(key)).booleanValue(); 56 } 57 58 private class CallCounter implements Verifiable 59 { 60 private int count = 0; 61 private final Object value; 62 private final Object key; 63 64 public CallCounter( Object key, Object value ) { 65 this.key = key; 66 this.value = value; 67 } 68 69 public Object 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 enumeration = returnValues.elements(); enumeration.hasMoreElements();) { 83 ((Verifiable)enumeration.nextElement()).verify(); 84 } 85 } 86 } 87 | Popular Tags |