1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson/*
2f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson * Copyright (c) 2007 Mockito contributors
3f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson * This program is made available under the terms of the MIT License.
4f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson */
5f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
6f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonpackage org.mockito.internal.verification.checkers;
7f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
8f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport java.util.List;
9f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
10f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.exceptions.Reporter;
11f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.internal.invocation.InvocationMarker;
12f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.internal.invocation.InvocationMatcher;
13f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.internal.invocation.InvocationsFinder;
14f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.internal.reporting.Discrepancy;
15f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.internal.verification.api.InOrderContext;
16f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.invocation.Invocation;
17f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonimport org.mockito.invocation.Location;
18f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
19f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonpublic class NumberOfInvocationsInOrderChecker {
20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    private final Reporter reporter;
22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    private final InvocationsFinder finder;
23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    private final InvocationMarker invocationMarker = new InvocationMarker();
24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
25f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    public NumberOfInvocationsInOrderChecker() {
26f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson        this(new InvocationsFinder(), new Reporter());
27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    }
28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    NumberOfInvocationsInOrderChecker(InvocationsFinder finder, Reporter reporter) {
30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson        this.finder = finder;
31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson        this.reporter = reporter;
32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    }
33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) {
35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson        List<Invocation> chunk = finder.findMatchingChunk(invocations, wanted, wantedCount, context);
36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
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}