com.fitbur.mockito.internal.verification.AtMost Maven / Gradle / Ivy
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package com.fitbur.mockito.internal.verification;
import static com.fitbur.mockito.exceptions.Reporter.wantedAtMostX;
import static com.fitbur.mockito.internal.invocation.InvocationMarker.markVerified;
import java.util.Iterator;
import java.util.List;
import com.fitbur.mockito.exceptions.base.MockitoException;
import com.fitbur.mockito.internal.invocation.InvocationMatcher;
import com.fitbur.mockito.internal.invocation.InvocationsFinder;
import com.fitbur.mockito.internal.verification.api.VerificationData;
import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.verification.VerificationMode;
public class AtMost implements VerificationMode {
private final int maxNumberOfInvocations;
public AtMost(int maxNumberOfInvocations) {
if (maxNumberOfInvocations < 0) {
throw new MockitoException("Negative value is not allowed here");
}
this.maxNumberOfInvocations = maxNumberOfInvocations;
}
public void verify(VerificationData data) {
List invocations = data.getAllInvocations();
InvocationMatcher wanted = data.getWanted();
InvocationsFinder finder = new InvocationsFinder();
List found = finder.findInvocations(invocations, wanted);
int foundSize = found.size();
if (foundSize > maxNumberOfInvocations) {
throw wantedAtMostX(maxNumberOfInvocations, foundSize);
}
removeAlreadyVerified(found);
markVerified(found, wanted);
}
@Override
public VerificationMode description(String description) {
return VerificationModeFactory.description(this, description);
}
private void removeAlreadyVerified(List invocations) {
for (Iterator iterator = invocations.iterator(); iterator.hasNext(); ) {
Invocation i = iterator.next();
if (i.isVerified()) {
iterator.remove();
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy