/*
 * Copyright 2003-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.mock.interceptor;

import groovy.lang.*;

import java.beans.IntrospectionException;

/**
 * The ProxyMetaClass for the MockInterceptor.
 * Instance and class methods are intercepted, but constructors are not to allow mocking of aggregated objects.
 * @author Dierk Koenig
 */

public class MockProxyMetaClass extends ProxyMetaClass {

    /**
     * @param adaptee the MetaClass to decorate with interceptability
     */
    public MockProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee) throws IntrospectionException {
        super(registry, theClass, adaptee);
    }

    /**
     * convenience factory method for the most usual case.
     */
    public static MockProxyMetaClass make(Class theClass) throws IntrospectionException {
        MetaClassRegistry metaRegistry = GroovySystem.getMetaClassRegistry();
        MetaClass meta = metaRegistry.getMetaClass(theClass);
        return new MockProxyMetaClass(metaRegistry, theClass, meta);
    }

    public Object invokeMethod(final Object object, final String methodName, final Object[] arguments) {
        if (null == interceptor) {
            throw new RuntimeException("cannot invoke without interceptor");
        }
        return interceptor.beforeInvoke(object, methodName, arguments);
    }

    public Object invokeStaticMethod(final Object object, final String methodName, final Object[] arguments) {
        if (null == interceptor) {
            throw new RuntimeException("cannot invoke without interceptor");
        }
        return interceptor.beforeInvoke(object, methodName, arguments);
    }

    public Object getProperty(Class aClass, Object object, String property, boolean b, boolean b1) {
        if (null == interceptor) {
            throw new RuntimeException("cannot invoke without interceptor");
        }
        if(interceptor instanceof PropertyAccessInterceptor) {
            return ((PropertyAccessInterceptor)interceptor).beforeGet(object,property);
        }
        else {
            return super.getProperty(aClass,object,property,b,b);
        }
    }

    public void setProperty(Class aClass, Object object, String property, Object newValue, boolean b, boolean b1) {
        if (null == interceptor) {
            throw new RuntimeException("cannot invoke without interceptor");
        }

        if(interceptor instanceof PropertyAccessInterceptor) {
            ((PropertyAccessInterceptor)interceptor).beforeSet(object,property, newValue);
        }
        else {
            super.setProperty(aClass,object,property,newValue,b,b);
        }
    }

    /**
     * Unlike general impl in superclass, ctors are not intercepted but relayed
     */
    public Object invokeConstructor(final Object[] arguments) {
        return adaptee.invokeConstructor(arguments);
    }
    
}

@@ -20,10 +20,10 @@
 3-20
+1
 0
-9
  the
@@ -876,16 +876,19 @@
 bjects.%0A
+ *%0A
  * @auth
@@ -1794,32 +1794,60 @@
 n(%22cannot invoke
+ method '%22 + methodName + %22'
  without interce
@@ -2134,32 +2134,67 @@
 n(%22cannot invoke
+ static method '%22 + methodName + %22'
  without interce
@@ -2465,38 +2465,63 @@
 ception(%22cannot 
-invoke
+get property '%22 + property + %22'
  without interce
@@ -2540,32 +2540,33 @@
     %7D%0A        if
+ 
 (interceptor ins
@@ -2641,32 +2641,33 @@
 cessInterceptor)
+ 
 interceptor).bef
@@ -2672,32 +2672,33 @@
 eforeGet(object,
+ 
 property);%0A     
@@ -2697,32 +2697,24 @@
 );%0A        %7D
-%0A       
  else %7B%0A    
@@ -2745,39 +2745,41 @@
 Property(aClass,
+ 
 object,
+ 
 property,b,b);%0A 
@@ -2771,18 +2771,20 @@
 roperty,
+ 
 b,
+ 
 b);%0A    
@@ -2994,22 +2994,47 @@
 %22cannot 
-invoke
+set property '%22 + property + %22'
  without
@@ -3070,16 +3070,17 @@
       if
+ 
 (interce
@@ -3164,16 +3164,17 @@
 rceptor)
+ 
 intercep
@@ -3191,24 +3191,25 @@
 eSet(object,
+ 
 property, ne
@@ -3226,24 +3226,16 @@
        %7D
-%0A       
  else %7B%0A
@@ -3271,23 +3271,25 @@
 (aClass,
+ 
 object,
+ 
 property
@@ -3289,16 +3289,17 @@
 roperty,
+ 
 newValue
@@ -3299,18 +3299,20 @@
 ewValue,
+ 
 b,
+ 
 b);%0A    
@@ -3543,15 +3543,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

tweak error messages