KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > dynamic > CallOnceExpectation


1 package com.mockobjects.dynamic;
2
3 import junit.framework.*;
4
5 public class CallOnceExpectation implements Callable {
6     private Callable delegate;
7     private boolean wasCalled = false;
8     
9     public CallOnceExpectation( Callable delegate ) {
10         this.delegate = delegate;
11     }
12     
13     public String JavaDoc getDescription() {
14         return delegate.getDescription() + " [" + (wasCalled ? "" : "not ") + "called]";
15     }
16     
17     public Object JavaDoc call(Mock mock, String JavaDoc methodName, Object JavaDoc[] args) throws Throwable JavaDoc {
18         wasCalled = true;
19         return delegate.call( mock, methodName, args );
20     }
21     
22     public boolean matches(String JavaDoc methodName, Object JavaDoc[] args) {
23         return (!wasCalled) && delegate.matches( methodName, args );
24     }
25
26     public void verify() {
27         if( !wasCalled ) {
28             throw new AssertionFailedError( delegate.getDescription() + " was expected but not called" );
29         }
30         
31         delegate.verify();
32     }
33     
34     // Implemented to aid visualisation in an IDE debugger
35
public String JavaDoc toString() {
36         return Mock.className(this.getClass()) + "(" + this.getDescription() + ")";
37     }
38 }
39
Popular Tags