1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockito.internal.verification.checkers; 7 8 import java.util.List; 9 10 import org.mockito.exceptions.Reporter; 11 import org.mockito.internal.invocation.InvocationMatcher; 12 import org.mockito.internal.invocation.InvocationMarker; 13 import org.mockito.internal.invocation.InvocationsFinder; 14 import org.mockito.internal.reporting.Discrepancy; 15 import org.mockito.invocation.Invocation; 16 import org.mockito.invocation.Location; 17 18 public class NumberOfInvocationsChecker { 19 20 private final Reporter reporter; 21 private final InvocationsFinder finder; 22 private final InvocationMarker invocationMarker = new InvocationMarker(); 23 24 public NumberOfInvocationsChecker() { 25 this(new Reporter(), new InvocationsFinder()); 26 } 27 28 NumberOfInvocationsChecker(Reporter reporter, InvocationsFinder finder) { 29 this.reporter = reporter; 30 this.finder = finder; 31 } 32 33 public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount) { 34 List<Invocation> actualInvocations = finder.findInvocations(invocations, wanted); 35 36 int actualCount = actualInvocations.size(); 37 if (wantedCount > actualCount) { 38 Location lastInvocation = finder.getLastLocation(actualInvocations); 39 reporter.tooLittleActualInvocations(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation); 40 } else if (wantedCount == 0 && actualCount > 0) { 41 Location firstUndesired = actualInvocations.get(wantedCount).getLocation(); 42 reporter.neverWantedButInvoked(wanted, firstUndesired); 43 } else if (wantedCount < actualCount) { 44 Location firstUndesired = actualInvocations.get(wantedCount).getLocation(); 45 reporter.tooManyActualInvocations(wantedCount, actualCount, wanted, firstUndesired); 46 } 47 48 invocationMarker.markVerified(actualInvocations, wanted); 49 } 50 }