Package org.jmock.dynamic

Source Code of org.jmock.dynamic.CoreMock

/* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
package org.jmock.dynamic;

import junit.framework.AssertionFailedError;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class CoreMock implements DynamicMock {
    private InvocationDispatcher invocationDispatcher;
    private Object proxy;
    private String name;

    public CoreMock(Class mockedClass, String name, InvocationDispatcher invocationDispatcher) {
        this.proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{mockedClass}, this);
        this.name = name;
        this.invocationDispatcher = invocationDispatcher;

// callables.addStub(new ProxyIsEqual(this.mock));
    }

    public Object proxy() {
        return this.proxy;
    }

    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        Invocation invocation = new Invocation(method, args);
        try {
            if (invocation.isCheckingEqualityOnProxy()) {
                return new Boolean(args[0] == this.proxy);
            } else if (invocation.isMockNameGetter()) {
                return this.getMockName();
            } else {
                return invocationDispatcher.dispatch(invocation);
            }
        } catch (AssertionFailedError ex) {
            DynamicMockError error = new DynamicMockError(invocation, name + ": " + ex.getMessage());
            error.fillInStackTrace();
            throw error;
        }
    }

    public void verify() {
        try {
            invocationDispatcher.verify();
        } catch (AssertionFailedError ex) {
            throw new AssertionFailedError(name + ": " + ex.getMessage());
        }
    }

    public String toString() {
        return this.name;
    }

    public String getMockName() {
        return this.name;
    }

    public void add(Invokable invokable) {
        invocationDispatcher.add(invokable);
    }

    public void reset() {
        invocationDispatcher.clear();
    }

    public static String mockNameFromClass(Class c) {
        return "mock" + className(c);
    }

    public static String className(Class c) {
        String name = c.getName();
        return name.substring(name.lastIndexOf('.') + 1);
    }
}
TOP

Related Classes of org.jmock.dynamic.CoreMock

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.