1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.verification; 6 7 import java.util.List; 8 9 import org.mockito.exceptions.Reporter; 10 import org.mockito.internal.invocation.InvocationMarker; 11 import org.mockito.internal.invocation.InvocationMatcher; 12 import org.mockito.internal.invocation.InvocationsFinder; 13 import org.mockito.internal.verification.api.VerificationData; 14 import org.mockito.invocation.Invocation; 15 import org.mockito.verification.VerificationMode; 16 17 public class Only implements VerificationMode { 18 19 private final InvocationsFinder finder = new InvocationsFinder(); 20 private final InvocationMarker marker = new InvocationMarker(); 21 private final Reporter reporter = new Reporter(); 22 23 @SuppressWarnings("unchecked") 24 public void verify(VerificationData data) { 25 InvocationMatcher wantedMatcher = data.getWanted(); 26 List<Invocation> invocations = data.getAllInvocations(); 27 List<Invocation> chunk = finder.findInvocations(invocations,wantedMatcher); 28 if (invocations.size() != 1 && chunk.size() > 0) { 29 Invocation unverified = finder.findFirstUnverified(invocations); 30 reporter.noMoreInteractionsWanted(unverified, (List) invocations); 31 } else if (invocations.size() != 1 || chunk.size() == 0) { 32 reporter.wantedButNotInvoked(wantedMatcher); 33 } 34 marker.markVerified(chunk.get(0), wantedMatcher); 35 } 36 } 37