View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.call;
4   
5   import java.util.ArrayList;
6   import java.util.List;
7   import junit.framework.TestCase;
8   import org.virtualmock.Constants;
9   import org.virtualmock.matcher.ArgMatcher;
10  
11  
12  /***
13   * Manages recorded and invoked calls.
14   *
15   * @author Chad Woolley
16   * @version $Revision: 1.37 $
17   */
18  public class CallManager {
19      private InvokedCallQueue invokedCallQueue = null;
20      private List mockedClasses = null;
21  
22      // Commented for Hansel
23      // private Logger logger = Logger.getLogger(CallManager.class.getName());
24  
25      /*** The last call that was recorded. */
26      private RecordedCall lastCall = null;
27      private RecordedCallQueue recordedCallQueue = null;
28      private TestCase testCase = null;
29      private int phase = 0;
30  
31      /***
32       * Creates a new CallManager object.
33       *
34       * @param invokedCallQueue the
35       * @param recordedCallQueue DOCUMENT ME! (Constructor Parameter)
36       */
37      public CallManager(InvokedCallQueue invokedCallQueue,
38          RecordedCallQueue recordedCallQueue) {
39          this.invokedCallQueue = invokedCallQueue;
40          this.recordedCallQueue = recordedCallQueue;
41          init();
42      }
43  
44      /***
45       * Indicates if calls are being mocked for the specified class.
46       *
47       * @param className The class which is being checked to see if calls are
48       *        being mocked for it
49       *
50       * @return true if the class is being mocked, false if it is not
51       */
52      public boolean isClassMocked(String className) {
53          if (mockedClasses.contains(className)) {
54              return true;
55          }
56  
57          return false;
58      }
59  
60      /***
61       * Returns the last call that was recorded.
62       *
63       * @return the last call that was recorded
64       */
65      public RecordedCall getLastCall() {
66          return lastCall;
67      }
68  
69      /***
70       * Returns a recorded mock call which matches the specified invoked call,
71       * or null if none match.
72       *
73       * @param invokedCall the invoked call to match
74       *
75       * @return DOCUMENT ME! (Method Return Value)
76       */
77      public RecordedCall getMatchingCall(InvokedCall invokedCall) {
78          return recordedCallQueue.getMatchingCall(invokedCall);
79      }
80  
81      /***
82       * Set the phase that VirtualMock is currently in.
83       *
84       * @param phase the phase that VirtualMock is currently in
85       */
86      public void setPhase(int phase) {
87          this.phase = phase;
88      }
89  
90      /***
91       * Indicates if VirtualMock is in the Playback phase.
92       *
93       * @return true if VirtualMock is in the Record phase
94       */
95      public boolean isPlaybackPhase() {
96          return phase == Constants.PLAYBACK_PHASE;
97      }
98  
99      /***
100      * Indicates if VirtualMock is in the Record phase.
101      *
102      * @return true if VirtualMock is in the Record phase
103      */
104     public boolean isRecordPhase() {
105         return phase == Constants.RECORD_PHASE;
106     }
107 
108     /***
109      * Accessor.
110      *
111      * @return the recorded call queue
112      */
113     public RecordedCallQueue getRecordedCallQueue() {
114         return recordedCallQueue;
115     }
116 
117     /***
118      * Convenience method to retrieve calls list for the RecordedCallQueue.
119      *
120      * @return the calls list of the recorded call queue
121      */
122     public List getRecordedCalls() {
123         return recordedCallQueue.getCalls();
124     }
125 
126     /***
127      * Set the TestCase being executed.
128      *
129      * @param case1 the TestCase to set
130      */
131     public void setTestCase(TestCase case1) {
132         testCase = case1;
133     }
134 
135     /***
136      * Get the TestCase being executed.
137      *
138      * @return the TestCase being executed
139      */
140     public TestCase getTestCase() {
141         return testCase;
142     }
143 
144     /***
145      * Gets all uninvoked calls from the RecordedCallQueue.
146      *
147      * @return An array of all uninvoked RecordedCalls
148      */
149     public RecordedCall[] getUninvokedCalls() {
150         return recordedCallQueue.getUninvokedCalls();
151     }
152 
153     /***
154      * Indicates if VirtualMock is in the Verify phase.
155      *
156      * @return true if VirtualMock is in the Verify phase
157      */
158     public boolean isVerifyPhase() {
159         return phase == Constants.VERIFY_PHASE;
160     }
161 
162     /***
163      * Indicates that all calls should be mocked for the specified class.
164      *
165      * @param classToMock The class for which to mock calls
166      */
167     public void addMockClass(String classToMock) {
168         mockedClasses.add(classToMock);
169     }
170 
171     /***
172      * This method is called from the advice to indicate when a call is
173      * invoked.
174      *
175      * @param invokedCall the call that was invoked
176      */
177     public void indicateCalled(InvokedCall invokedCall) {
178         putInvokedCall(invokedCall);
179 
180         // flag the call as invoked in recordedCallQueue queue
181         RecordedCall nextMatchingUninvokedCall = getMatchingCall(invokedCall);
182 
183         if (nextMatchingUninvokedCall != null) {
184             nextMatchingUninvokedCall.incrementInvocationCount();
185         }
186     }
187 
188     /***
189      * Define the ArgMatchers for the last recorded call, which should have the
190      * same number of arguments as elements in the array.
191      *
192      * @param argMatchers The array of ArgMatchers that VirtualMock will use to
193      *        match the arguments when the last recorded call is invoked.
194      */
195     public void recordArgMatchers(ArgMatcher[] argMatchers) {
196         getLastCall().setArgMatchers(argMatchers);
197     }
198 
199     /***
200      * Records a call which will be mocked.
201      *
202      * @param signature the unique identifier for the call
203      * @param argValues values of arguments for the call
204      *
205      * @return the newly recorded call
206      */
207     public RecordedCall recordCall(Signature signature, Object[] argValues) {
208         // DEBUG: disabled for Hansel
209         //        if (logger.isDebugEnabled()) {
210         //            logger.debug("in recordCall, call='" + signature +
211         //                "', argValues='" + argValues + "', recordedCallQueue size=" +
212         //                recordedCallQueue.size());
213         //        }
214         RecordedCall recordedCall = null;
215         recordedCall = new RecordedCall(signature, argValues);
216 
217         recordedCallQueue.add(recordedCall);
218 
219         // DEBUG: disabled for Hansel
220         //        if (logger.isDebugEnabled()) {
221         //            logger.debug("exiting recordCall, " +
222         //                "added to queue with callId=" +
223         //                signature + ", recordedCallQueue size=" +
224         //                recordedCallQueue.size());
225         //        }
226         // keep track of the last call that was recorded
227         lastCall = recordedCall;
228 
229         return recordedCall;
230     }
231 
232     /***
233      * Records a return value for the most recently recorded call.
234      *
235      * @param returnValue the value to return when the call is invoked
236      */
237     public void recordReturnValueForLastCall(Object returnValue) {
238         lastCall.setReturnValue(returnValue);
239     }
240 
241     /***
242      * Records a throwable to be thrown for the most recently recorded call.
243      *
244      * @param throwable the throwable to throw when the call is invoked
245      */
246     public void recordThrowableForLastCall(Throwable throwable) {
247         lastCall.setThrowable(throwable);
248     }
249 
250     /***
251      * Initializes the CallManager by clearing the RecordedCallQueue and
252      * InvokedCallQueue, and setting phase to RECORD_PHASE.
253      */
254     protected void init() {
255         setPhase(Constants.RECORD_PHASE);
256         mockedClasses = new ArrayList();
257         recordedCallQueue.clear();
258         invokedCallQueue.clear();
259     }
260 
261     /***
262      * Puts an InvokedCall on the InvokedCallQueue.
263      *
264      * @param invokedCall the call that was invoked, and should be put on the
265      *        InvokedCallQueue
266      */
267     private void putInvokedCall(InvokedCall invokedCall) {
268         // add this invoked call to the end of the queue
269         invokedCallQueue.add((InvokedCall) invokedCall);
270 
271         // DEBUG: disabled for Hansel
272         //        if (logger.isDebugEnabled()) {
273         //            logger.debug("exiting putInvokedCall, queued call = " +
274         //                invokedCall.toString());
275         //        }
276     }
277 }