1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal;
7
8import java.util.LinkedList;
9import java.util.List;
10
11import org.mockito.InOrder;
12import org.mockito.exceptions.Reporter;
13import org.mockito.exceptions.base.MockitoException;
14import org.mockito.internal.verification.InOrderContextImpl;
15import org.mockito.internal.verification.InOrderWrapper;
16import org.mockito.internal.verification.VerificationModeFactory;
17import org.mockito.internal.verification.api.InOrderContext;
18import org.mockito.internal.verification.api.VerificationInOrderMode;
19import org.mockito.invocation.Invocation;
20import org.mockito.verification.VerificationMode;
21
22/**
23 * Allows verifying in order. This class should not be exposed, hence default access.
24 */
25public class InOrderImpl implements InOrder, InOrderContext {
26
27    private final MockitoCore mockitoCore = new MockitoCore();
28    private final Reporter reporter = new Reporter();
29    private final List<Object> mocksToBeVerifiedInOrder = new LinkedList<Object>();
30    private final InOrderContext inOrderContext = new InOrderContextImpl();
31
32    public List<Object> getMocksToBeVerifiedInOrder() {
33        return mocksToBeVerifiedInOrder;
34    }
35
36    public InOrderImpl(List<Object> mocksToBeVerifiedInOrder) {
37        this.mocksToBeVerifiedInOrder.addAll(mocksToBeVerifiedInOrder);
38    }
39
40    public <T> T verify(T mock) {
41        return this.verify(mock, VerificationModeFactory.times(1));
42    }
43
44    public <T> T verify(T mock, VerificationMode mode) {
45        if (!mocksToBeVerifiedInOrder.contains(mock)) {
46            reporter.inOrderRequiresFamiliarMock();
47        } else if (!(mode instanceof VerificationInOrderMode)) {
48            throw new MockitoException(mode.getClass().getSimpleName() + " is not implemented to work with InOrder");
49        }
50        return mockitoCore.verify(mock, new InOrderWrapper((VerificationInOrderMode) mode, this));
51    }
52
53    public boolean isVerified(Invocation i) {
54        return inOrderContext.isVerified(i);
55    }
56
57    public void markVerified(Invocation i) {
58        inOrderContext.markVerified(i);
59    }
60
61    public void verifyNoMoreInteractions() {
62        mockitoCore.verifyNoMoreInteractionsInOrder(mocksToBeVerifiedInOrder, this);
63    }
64}