All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.internal.invocation.MatcherApplicationStrategy Maven / Gradle / Ivy

There is a newer version: 5.12.0
Show newest version
/*
 * Copyright (c) 2016 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.internal.invocation;

import java.util.List;

import org.mockito.ArgumentMatcher;
import org.mockito.internal.matchers.CapturingMatcher;
import org.mockito.invocation.Invocation;

public class MatcherApplicationStrategy {

    private final Invocation invocation;
    private final List> matchers;

    private MatcherApplicationStrategy(
            Invocation invocation, List> matchers) {
        this.invocation = invocation;
        this.matchers = matchers;
    }

    /**
     * Returns the {@link MatcherApplicationStrategy} that must be used to capture the
     * arguments of the given invocation using the given matchers.
     *
     * @param invocation
     *            that contain the arguments to capture
     * @param matchers
     *            that will be used to capture the arguments of the invocation,
     *            the passed {@link List} is not required to contain a
     *            {@link CapturingMatcher}
     * @return never null
     */
    public static MatcherApplicationStrategy getMatcherApplicationStrategyFor(
            Invocation invocation, List> matchers) {
        return new MatcherApplicationStrategy(invocation, matchers);
    }

    /**
     * Applies the given {@link ArgumentMatcherAction} to all arguments and
     * corresponding matchers
     *
     * @param action
     *            must not be null
     * @return
     *         
    *
  • true if the given action returned * true for all arguments and matchers passed to it. *
  • false if the given action returned * false for one of the passed arguments and matchers *
  • false if the given matchers don't fit to the given invocation * because too many or to few matchers are available. *
*/ public boolean forEachMatcherAndArgument(ArgumentMatcherAction action) { final boolean maybeVararg = invocation.getMethod().isVarArgs() && invocation.getRawArguments().length == matchers.size(); if (maybeVararg) { final Class matcherType = lastMatcherType(); final Class paramType = lastParameterType(); if (paramType.isAssignableFrom(matcherType)) { return argsMatch(invocation.getRawArguments(), matchers, action); } } if (invocation.getArguments().length == matchers.size()) { return argsMatch(invocation.getArguments(), matchers, action); } return false; } private boolean argsMatch( Object[] arguments, List> matchers, ArgumentMatcherAction action) { for (int i = 0; i < arguments.length; i++) { ArgumentMatcher matcher = matchers.get(i); Object argument = arguments[i]; if (!action.apply(matcher, argument)) { return false; } } return true; } private Class lastMatcherType() { return matchers.get(matchers.size() - 1).type(); } private Class lastParameterType() { final Class[] parameterTypes = invocation.getMethod().getParameterTypes(); return parameterTypes[parameterTypes.length - 1]; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy