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

com.fitbur.mockito.internal.InOrderImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */

package com.fitbur.mockito.internal;

import com.fitbur.mockito.InOrder;
import com.fitbur.mockito.exceptions.base.MockitoException;
import com.fitbur.mockito.internal.verification.InOrderContextImpl;
import com.fitbur.mockito.internal.verification.InOrderWrapper;
import com.fitbur.mockito.internal.verification.VerificationModeFactory;
import com.fitbur.mockito.internal.verification.api.InOrderContext;
import com.fitbur.mockito.internal.verification.api.VerificationInOrderMode;
import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.verification.VerificationMode;
import com.fitbur.mockito.verification.VerificationWrapper;
import com.fitbur.mockito.verification.VerificationWrapperInOrderWrapper;

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

import java.util.LinkedList;
import java.util.List;

/**
 * Allows verifying in order. This class should not be exposed, hence default access.
 */
public class InOrderImpl implements InOrder, InOrderContext {
    
    private final MockitoCore mockitoCore = new MockitoCore();
    private final List mocksToBeVerifiedInOrder = new LinkedList();
    private final InOrderContext inOrderContext = new InOrderContextImpl();
    
    public List getMocksToBeVerifiedInOrder() {
        return mocksToBeVerifiedInOrder;
    }

    public InOrderImpl(List mocksToBeVerifiedInOrder) {
        this.mocksToBeVerifiedInOrder.addAll(mocksToBeVerifiedInOrder);
    }

    public  T verify(T mock) {
        return this.verify(mock, VerificationModeFactory.times(1));
    }
    
    public  T verify(T mock, VerificationMode mode) {
        if (!mocksToBeVerifiedInOrder.contains(mock)) {
            throw inOrderRequiresFamiliarMock();
        }
        if (mode instanceof VerificationWrapper) {
            return mockitoCore.verify(mock, new VerificationWrapperInOrderWrapper((VerificationWrapper) mode, this));
        }  else if (!(mode instanceof VerificationInOrderMode)) {
            throw new MockitoException(mode.getClass().getSimpleName() + " is not implemented to work with InOrder");
        }
        return mockitoCore.verify(mock, new InOrderWrapper((VerificationInOrderMode) mode, this));
    }

    public boolean isVerified(Invocation i) {
        return inOrderContext.isVerified(i);
    }

    public void markVerified(Invocation i) {
        inOrderContext.markVerified(i);
    }

    public void verifyNoMoreInteractions() {
        mockitoCore.verifyNoMoreInteractionsInOrder(mocksToBeVerifiedInOrder, this);
    }
}