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

org.mockito.internal.matchers.VarargCapturingMatcher Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
package org.mockito.internal.matchers;

import org.hamcrest.Description;
import org.mockito.ArgumentMatcher;
import org.mockito.exceptions.Reporter;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


@SuppressWarnings("unchecked")
public class VarargCapturingMatcher extends ArgumentMatcher implements CapturesArguments, VarargMatcher, Serializable {
    private static final long serialVersionUID = 4057053345838026645L;
    private final LinkedList> arguments = new LinkedList>();

    public boolean matches(Object argument) {
        return true;
    }

    public void describeTo(Description description) {
        description.appendText("");
    }

    public List getLastVarargs() {
        if (arguments.isEmpty()) {
            new Reporter().noArgumentValueWasCaptured();
            return null;
        } else {
            return arguments.getLast();
        }
    }

    public List> getAllVarargs() {
        return arguments;
    }

    public void captureFrom(Object varArgArray) {
        List vararg = unpackAsList(varArgArray);
        this.arguments.add(vararg);
    }

    private List unpackAsList(Object varArgArray) {
        if (varArgArray instanceof Object[]) {
            return Arrays.asList((T[]) varArgArray);
        } else if (varArgArray.getClass().isArray()) {
            Object[] primitiveArray = new Object[Array.getLength(varArgArray)];
            for (int i = 0; i < primitiveArray.length; i++) {
                primitiveArray[i] = Array.get(varArgArray, i);
            }
            return Arrays.asList((T[]) primitiveArray);
        } else {
            return Collections.singletonList((T) varArgArray);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy