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

com.fitbur.mockito.internal.matchers.VarargCapturingMatcher Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.mockito.internal.matchers;

import com.fitbur.mockito.ArgumentMatcher;
import com.fitbur.mockito.exceptions.Reporter;

import static com.fitbur.mockito.exceptions.Reporter.noArgumentValueWasCaptured;

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 implements ArgumentMatcher, CapturesArguments, VarargMatcher, Serializable {

    private final LinkedList> arguments = new LinkedList>();

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

    public String toString() {
        return "";
    }

	public List getLastVarargs() {
		if (arguments.isEmpty()) {
			throw noArgumentValueWasCaptured();
		}
		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