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; 7 8 import java.util.List; 9 10 import org.mockito.exceptions.Reporter; 11 import org.mockito.exceptions.base.MockitoException; 12 import org.mockito.internal.invocation.InvocationMatcher; 13 import org.mockito.internal.invocation.InvocationMarker; 14 import org.mockito.internal.invocation.InvocationsFinder; 15 import org.mockito.internal.verification.api.VerificationData; 16 import org.mockito.invocation.Invocation; 17 import org.mockito.verification.VerificationMode; 18 19 public class AtMost implements VerificationMode { 20 21 private final int maxNumberOfInvocations; 22 private final InvocationMarker invocationMarker = new InvocationMarker(); 23 24 public AtMost(int maxNumberOfInvocations) { 25 if (maxNumberOfInvocations < 0) { 26 throw new MockitoException("Negative value is not allowed here"); 27 } 28 this.maxNumberOfInvocations = maxNumberOfInvocations; 29 } 30 31 public void verify(VerificationData data) { 32 List<Invocation> invocations = data.getAllInvocations(); 33 InvocationMatcher wanted = data.getWanted(); 34 35 InvocationsFinder finder = new InvocationsFinder(); 36 List<Invocation> found = finder.findInvocations(invocations, wanted); 37 int foundSize = found.size(); 38 if (foundSize > maxNumberOfInvocations) { 39 new Reporter().wantedAtMostX(maxNumberOfInvocations, foundSize); 40 } 41 42 invocationMarker.markVerified(found, wanted); 43 } 44 }