KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jmock.expectation;
4
5 import junit.framework.Assert;
6
7
8 public class ExpectationCounter extends AbstractExpectation
9 {
10     private int myExpectedCalls = 0;
11     private int myActualCalls = 0;
12
13     public ExpectationCounter( String JavaDoc name ) {
14         super(name);
15     }
16
17     public void clearActual() {
18         myActualCalls = 0;
19     }
20
21     public void inc() {
22         myActualCalls++;
23         if (shouldCheckImmediately()) {
24             Assert.assertTrue(myName + " should not be called more than " + myExpectedCalls + " times",
25                               myActualCalls <= myExpectedCalls);
26         }
27     }
28
29     public void setExpected( int expectedCalls ) {
30         myExpectedCalls = expectedCalls;
31         setHasExpectations();
32     }
33
34     public void setExpectNothing() {
35         myExpectedCalls = 0;
36         setHasExpectations();
37     }
38
39     public void verify() {
40         assertEquals("did not receive the expected Count.",
41                      myExpectedCalls,
42                      myActualCalls);
43     }
44 }
45
Popular Tags