KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > AbstractExpectation


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

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