01: package groovy.mock.interceptor;
02:
03: import groovy.lang.ProxyMetaClass;
04: import groovy.lang.MetaClassRegistry;
05: import groovy.lang.MetaClass;
06:
07: import java.beans.IntrospectionException;
08:
09: import org.codehaus.groovy.runtime.InvokerHelper;
10:
11: /**
12: * The ProxyMetaClass for the MockInterceptor.
13: * Instance and class methods are intercepted, but constructors are not to allow mocking of aggregated objects.
14: * @author Dierk Koenig
15: */
16:
17: public class MockProxyMetaClass extends ProxyMetaClass {
18:
19: /**
20: * @param adaptee the MetaClass to decorate with interceptability
21: */
22: public MockProxyMetaClass(MetaClassRegistry registry,
23: Class theClass, MetaClass adaptee)
24: throws IntrospectionException {
25: super (registry, theClass, adaptee);
26: }
27:
28: /**
29: * convenience factory method for the most usual case.
30: */
31: public static MockProxyMetaClass make(Class theClass)
32: throws IntrospectionException {
33: MetaClassRegistry metaRegistry = InvokerHelper.getInstance()
34: .getMetaRegistry();
35: MetaClass meta = metaRegistry.getMetaClass(theClass);
36: return new MockProxyMetaClass(metaRegistry, theClass, meta);
37: }
38:
39: public Object invokeMethod(final Object object,
40: final String methodName, final Object[] arguments) {
41: if (null == interceptor) {
42: throw new RuntimeException(
43: "cannot invoke without interceptor");
44: }
45: return interceptor.beforeInvoke(object, methodName, arguments);
46: }
47:
48: public Object invokeStaticMethod(final Object object,
49: final String methodName, final Object[] arguments) {
50: if (null == interceptor) {
51: throw new RuntimeException(
52: "cannot invoke without interceptor");
53: }
54: return interceptor.beforeInvoke(object, methodName, arguments);
55: }
56:
57: /**
58: * Unlike general impl in superclass, ctors are not intercepted but relayed
59: */
60: public Object invokeConstructor(final Object[] arguments) {
61: return adaptee.invokeConstructor(arguments);
62: }
63:
64: }
|