KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > core > AbstractDynamicMock


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

3 package org.jmock.core;
4
5 import java.util.List JavaDoc;
6 import junit.framework.AssertionFailedError;
7 import org.jmock.core.constraint.IsAnything;
8 import org.jmock.core.matcher.ArgumentsMatcher;
9 import org.jmock.core.matcher.MethodNameMatcher;
10 import org.jmock.core.matcher.NoArgumentsMatcher;
11 import org.jmock.core.stub.CustomStub;
12 import org.jmock.core.stub.ReturnStub;
13
14
15 public abstract class AbstractDynamicMock
16         implements DynamicMock
17 {
18     private InvocationDispatcher invocationDispatcher;
19     private Class JavaDoc mockedType;
20     private String JavaDoc name;
21
22     public AbstractDynamicMock( Class JavaDoc mockedType, String JavaDoc name ) {
23         this(mockedType, name, new LIFOInvocationDispatcher());
24     }
25
26     public AbstractDynamicMock( Class JavaDoc mockedType,
27                                 String JavaDoc name,
28                                 InvocationDispatcher invocationDispatcher ) {
29         this.mockedType = mockedType;
30         this.name = name;
31         this.invocationDispatcher = invocationDispatcher;
32
33         setupDefaultBehaviour();
34     }
35
36     public Class JavaDoc getMockedType() {
37         return mockedType;
38     }
39
40     protected Object JavaDoc mockInvocation( Invocation invocation )
41             throws Throwable JavaDoc {
42         try {
43             return invocationDispatcher.dispatch(invocation);
44         }
45         catch (AssertionFailedError failure) {
46             DynamicMockError mockFailure =
47                     new DynamicMockError(this, invocation, invocationDispatcher, failure.getMessage());
48
49             mockFailure.fillInStackTrace();
50             throw mockFailure;
51         }
52     }
53
54     public void verify() {
55         try {
56             invocationDispatcher.verify();
57         }
58         catch (AssertionFailedError ex) {
59             throw new AssertionFailedError( "mock object " + name + ": " + ex.getMessage());
60         }
61     }
62
63     public String JavaDoc toString() {
64         return this.name;
65     }
66
67     public String JavaDoc getMockName() {
68         return this.name;
69     }
70
71     public void setDefaultStub( Stub newDefaultStub ) {
72         invocationDispatcher.setDefaultStub(newDefaultStub);
73     }
74
75     public void addInvokable( Invokable invokable ) {
76         invocationDispatcher.add(invokable);
77     }
78
79     public void reset() {
80         //TODO write tests for this
81
invocationDispatcher.clear();
82         setupDefaultBehaviour();
83     }
84
85     public static String JavaDoc mockNameFromClass( Class JavaDoc c ) {
86         return "mock" + Formatting.classShortName(c);
87     }
88
89     private void setupDefaultBehaviour() {
90         addInvokable(hiddenInvocationMocker("toString",
91                                             NoArgumentsMatcher.INSTANCE,
92                                             new ReturnStub(name)));
93         addInvokable(hiddenInvocationMocker("equals",
94                                             new ArgumentsMatcher(new Constraint[]{new IsAnything()}),
95                                             new IsSameAsProxyStub()));
96         addInvokable(hiddenInvocationMocker("hashCode",
97                                             NoArgumentsMatcher.INSTANCE,
98                                             new HashCodeStub()));
99     }
100
101     private static final InvocationMocker.Describer NO_DESCRIPTION =
102         new InvocationMocker.Describer()
103         {
104             public boolean hasDescription() {
105                 return false;
106             }
107
108             public void describeTo( StringBuffer JavaDoc buffer, List JavaDoc matchers, Stub stub, String JavaDoc name ) {
109             }
110         };
111
112     private InvocationMocker hiddenInvocationMocker( String JavaDoc methodName,
113                                                      InvocationMatcher arguments,
114                                                      Stub stub )
115     {
116         InvocationMocker invocationMocker = new InvocationMocker(NO_DESCRIPTION);
117
118         invocationMocker.addMatcher(new MethodNameMatcher(methodName));
119         invocationMocker.addMatcher(arguments);
120         invocationMocker.setStub(stub);
121
122         return invocationMocker;
123     }
124
125     private class IsSameAsProxyStub extends CustomStub
126     {
127         private IsSameAsProxyStub() {
128             super("returns whether equal to proxy");
129         }
130
131         public Object JavaDoc invoke( Invocation invocation ) throws Throwable JavaDoc {
132             return new Boolean JavaDoc(invocation.parameterValues.get(0) == proxy());
133         }
134     }
135
136     private class HashCodeStub extends CustomStub
137     {
138         private HashCodeStub() {
139             super("returns hashCode for proxy");
140         }
141
142         public Object JavaDoc invoke( Invocation invocation ) throws Throwable JavaDoc {
143             return new Integer JavaDoc(AbstractDynamicMock.this.hashCode());
144         }
145     }
146 }
147
Popular Tags