1 4 package com.mockobjects.dynamic; 5 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 11 public class CallBag extends CallCollection implements Callable, CallableAddable { 12 private List expectedCalls = new ArrayList (); 13 private List expectedMatches = new ArrayList (); 14 15 public CallBag() { 16 } 17 18 public void reset() { 19 this.expectedCalls.clear(); 20 this.expectedMatches.clear(); 21 } 22 23 public Object call(Mock mock, String methodName, Object [] args) 24 throws Throwable { 25 26 Callable matchingCall = findMatchingCall(methodName, args, this.expectedCalls); 27 if(matchingCall == null) { 28 matchingCall = findMatchingCall(methodName, args, this.expectedMatches); 29 } 30 if(matchingCall == null) { 31 throw createUnexpectedCallError(methodName, args); 32 } 33 34 return matchingCall.call(mock, methodName, args); 35 36 } 37 38 private Callable findMatchingCall(String methodName, Object [] args, List callList) { 39 for (Iterator call = callList.iterator(); call.hasNext();) { 40 Callable element = (Callable) call.next(); 41 42 if (element.matches(methodName, args)) { 43 return element; 44 } 45 } 46 47 return null; 48 } 49 50 public String getDescription() { 51 if (this.expectedCalls.isEmpty()) { 52 return "no methods"; 53 } else { 54 StringBuffer buf = new StringBuffer (); 55 56 buf.append("one of:\n"); 57 58 for (Iterator i = this.expectedCalls.iterator(); i.hasNext();) { 59 buf.append(((Callable) i.next()).getDescription()); 60 buf.append("\n"); 61 } 62 63 return buf.toString(); 64 } 65 } 66 67 public boolean matches(String methodName, Object [] args) { 68 throw new Error ("not implemented"); 69 } 70 71 public void verify() { 72 for (Iterator call = this.expectedCalls.iterator(); call.hasNext();) { 73 Callable element = (Callable) call.next(); 74 element.verify(); 75 } 76 } 77 78 public void addExpect(Callable call) { 79 this.expectedCalls.add(call); 80 } 81 82 public void addMatch(Callable call) { 83 this.expectedMatches.add(call); 84 } 85 } 86 | Popular Tags |