KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > Mock


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

3 package org.jmock;
4
5 import java.util.HashMap JavaDoc;
6 import junit.framework.AssertionFailedError;
7 import org.jmock.builder.*;
8 import org.jmock.core.*;
9
10
11 public class Mock
12         implements DynamicMock, BuilderNamespace, Verifiable
13 {
14     DynamicMock coreMock;
15     HashMap JavaDoc idTable = new HashMap JavaDoc();
16
17     public Mock( Class JavaDoc mockedType ) {
18         this(mockedType, CoreMock.mockNameFromClass(mockedType));
19     }
20
21     public Mock( Class JavaDoc mockedType, String JavaDoc name ) {
22         this(new CoreMock(mockedType, name));
23     }
24
25     public Mock( DynamicMock coreMock ) {
26         this.coreMock = coreMock;
27     }
28
29     public Class JavaDoc getMockedType() {
30         return coreMock.getMockedType();
31     }
32
33     public Object JavaDoc proxy() {
34         return coreMock.proxy();
35     }
36
37     public String JavaDoc toString() {
38         return coreMock.toString();
39     }
40
41     public void verify() {
42         coreMock.verify();
43     }
44
45     public void addInvokable( Invokable invokable ) {
46         coreMock.addInvokable(invokable);
47     }
48
49     public NameMatchBuilder stubs() {
50         return addNewInvocationMocker();
51     }
52
53     public NameMatchBuilder expects( InvocationMatcher expectation ) {
54         NameMatchBuilder builder = addNewInvocationMocker();
55         builder.match(expectation);
56         return builder;
57     }
58
59     private NameMatchBuilder addNewInvocationMocker() {
60         InvocationMocker mocker = new InvocationMocker(new InvocationMockerDescriber());
61         addInvokable(mocker);
62
63         return new InvocationMockerBuilder( mocker, this, getMockedType() );
64     }
65
66     public void setDefaultStub( Stub newDefaultStub ) {
67         coreMock.setDefaultStub(newDefaultStub);
68     }
69
70     public void reset() {
71         coreMock.reset();
72     }
73
74     public MatchBuilder lookupID( String JavaDoc id ) {
75         if (!idTable.containsKey(id)) {
76             throw new AssertionFailedError("no expected invocation named '" + id + "'");
77         }
78
79         return (MatchBuilder)idTable.get(id);
80     }
81
82     public void registerUniqueID( String JavaDoc id, MatchBuilder builder ) {
83         if (idTable.containsKey(id)) {
84             throw new AssertionFailedError("duplicate invocation named \"" + id + "\"");
85         }
86
87         storeID(id, builder);
88     }
89
90     public void registerMethodName( String JavaDoc id, MatchBuilder builder ) {
91         storeID(id, builder);
92     }
93
94     private void storeID( String JavaDoc id, IdentityBuilder builder ) {
95         idTable.put(id, builder);
96     }
97 }
98
Popular Tags