Package org.jmock.dynamic

Source Code of org.jmock.dynamic.InvocationMocker

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

import org.jmock.InvocationMatcher;
import org.jmock.Stub;
import org.jmock.matcher.MethodNameMatcher;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class InvocationMocker implements Invokable {

    private List matchers = new ArrayList();
    private Stub stub;

    public InvocationMocker(String methodName, InvocationMatcher arguments, Stub stub) {
        this(stub);
        addMatcher(new MethodNameMatcher(methodName));
        addMatcher(arguments);
    }

    public InvocationMocker(InvocationMatcher[] matchers, Stub stub) {
        this(stub);
        for (int i = 0; i < matchers.length; i++) addMatcher(matchers[i]);
    }

    public InvocationMocker(Stub stub) {
        this.stub = stub;
    }

    public String getDescription() {
        return null; // TODO
    }

    public boolean matches(Invocation invocation) {
        Iterator i = matchers.iterator();
        while (i.hasNext()) {
            if (!((InvocationMatcher) i.next()).matches(invocation)) {
                return false;
            }
        }
        return true;
    }

    public Object invoke(Invocation invocation) throws Throwable {
        Iterator i = matchers.iterator();
        while (i.hasNext()) {
            ((InvocationMatcher) i.next()).invoked(invocation);
        }
        return stub.invoke(invocation);
    }

    public void verify() {
        Iterator i = matchers.iterator();
        while (i.hasNext()) {
            ((InvocationMatcher) i.next()).verify();
        }
    }

    public InvocationMocker addMatcher(InvocationMatcher matcher) {
        matchers.add(matcher);
        return this;
    }

    public void setStub(Stub stub) {
        this.stub = stub;
    }
}
TOP

Related Classes of org.jmock.dynamic.InvocationMocker

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.