KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > util > Dummy


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

3 package org.jmock.util;
4
5 import org.jmock.core.CoreMock;
6 import org.jmock.core.Formatting;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.InvocationMocker;
9 import org.jmock.core.matcher.StatelessInvocationMatcher;
10 import org.jmock.core.stub.CustomStub;
11
12
13 public class Dummy
14 {
15     private Dummy() {
16         /* This class cannot be instantiated. */
17     }
18
19     public static Object JavaDoc newDummy( Class JavaDoc interfaceClass ) {
20         return newDummy(interfaceClass, "dummy" + Formatting.classShortName(interfaceClass));
21     }
22
23     public static Object JavaDoc newDummy( final Class JavaDoc interfaceClass, final String JavaDoc name ) {
24         CoreMock mock = new CoreMock(interfaceClass, name);
25         InvocationMocker mocker = new InvocationMocker();
26
27         mocker.addMatcher(new StatelessInvocationMatcher()
28         {
29             public boolean matches( Invocation invocation ) {
30                 return invocation.invokedMethod.getDeclaringClass() == interfaceClass;
31             }
32
33             public StringBuffer JavaDoc describeTo( StringBuffer JavaDoc buf ) {
34                 return buf.append("any invokedMethod declared in " + interfaceClass);
35             }
36         });
37         mocker.setStub(new CustomStub("dummy invokedMethod")
38         {
39             public Object JavaDoc invoke( Invocation invocation ) throws Throwable JavaDoc {
40                 throw new NotImplementedException(invocation.invokedMethod.getName() + "called on " + name);
41             }
42         });
43
44         mock.addInvokable(mocker);
45
46         return mock.proxy();
47     }
48
49     public static Object JavaDoc newDummy( final String JavaDoc name ) {
50         return new Object JavaDoc()
51         {
52             public String JavaDoc toString() {
53                 return name;
54             }
55         };
56     }
57 }
58
Popular Tags