KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jmock.expectation;
4
5 import org.jmock.core.Verifiable;
6
7
8 abstract public class AbstractExpectation implements Verifiable, Expectation
9 {
10     protected boolean myFailureModeIsImmediate = true;
11     protected String JavaDoc myName;
12
13     private boolean myHasExpectations = false;
14
15     public AbstractExpectation( String JavaDoc name ) {
16         myName = name;
17     }
18
19     protected void assertEquals( String JavaDoc msg,
20                                  int expectedValue,
21                                  int actualValue ) {
22         assertEquals(msg, new Integer JavaDoc(expectedValue), new Integer JavaDoc(actualValue));
23     }
24
25     /**
26      * Due to junit Assert being a Singleton implemented with static methods, and java's
27      * unfortunate implementation of class methods (e.g. no late binding) it is
28      * necessary to re-implement this invokedMethod here instead of over-riding failNotEquals
29      */

30
31     protected void assertEquals( String JavaDoc msg,
32                                  Object JavaDoc expectedValue,
33                                  Object JavaDoc actualValue ) {
34         if (!myHasExpectations) {
35             return;
36         }
37
38         if (expectedValue == null && actualValue == null) {
39             return;
40         }
41
42         if (expectedValue != null && expectedValue.equals(actualValue)) {
43             return;
44         }
45
46         junit.framework.Assert.fail(myName
47                                     + " "
48                                     + msg
49                                     + "\nExpected: "
50                                     + expectedValue
51                                     + "\nReceived: "
52                                     + actualValue);
53
54     }
55
56     abstract public void clearActual();
57
58     public boolean hasExpectations() {
59         return myHasExpectations;
60     }
61
62     public void setFailOnVerify() {
63         myFailureModeIsImmediate = false;
64     }
65
66     protected void setHasExpectations() {
67         clearActual();
68         myHasExpectations = true;
69     }
70
71     protected boolean shouldCheckImmediately() {
72         return myFailureModeIsImmediate && myHasExpectations;
73     }
74
75     public abstract void verify();
76 }
77
Popular Tags