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.verification.checkers;
7
8import java.util.List;
9
10import org.mockito.exceptions.Reporter;
11import org.mockito.internal.invocation.InvocationMarker;
12import org.mockito.internal.invocation.InvocationMatcher;
13import org.mockito.internal.invocation.InvocationsFinder;
14import org.mockito.internal.reporting.Discrepancy;
15import org.mockito.internal.verification.api.InOrderContext;
16import org.mockito.invocation.Invocation;
17import org.mockito.invocation.Location;
18
19public class NumberOfInvocationsInOrderChecker {
20
21    private final Reporter reporter;
22    private final InvocationsFinder finder;
23    private final InvocationMarker invocationMarker = new InvocationMarker();
24
25    public NumberOfInvocationsInOrderChecker() {
26        this(new InvocationsFinder(), new Reporter());
27    }
28
29    NumberOfInvocationsInOrderChecker(InvocationsFinder finder, Reporter reporter) {
30        this.finder = finder;
31        this.reporter = reporter;
32    }
33
34    public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) {
35        List<Invocation> chunk = finder.findMatchingChunk(invocations, wanted, wantedCount, context);
36
37        int actualCount = chunk.size();
38
39        if (wantedCount > actualCount) {
40            Location lastInvocation = finder.getLastLocation(chunk);
41            reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation);
42        } else if (wantedCount < actualCount) {
43            Location firstUndesired = chunk.get(wantedCount).getLocation();
44            reporter.tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, firstUndesired);
45        }
46
47        invocationMarker.markVerifiedInOrder(chunk, wanted, context);
48    }
49}