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 static org.mockito.internal.exceptions.Reporter.noMoreInteractionsWanted; 8 import static org.mockito.internal.exceptions.Reporter.wantedButNotInvoked; 9 import static org.mockito.internal.invocation.InvocationMarker.markVerified; 10 import static org.mockito.internal.invocation.InvocationsFinder.findFirstUnverified; 11 import static org.mockito.internal.invocation.InvocationsFinder.findInvocations; 12 13 import java.util.List; 14 15 import org.mockito.invocation.MatchableInvocation; 16 import org.mockito.internal.verification.api.VerificationData; 17 import org.mockito.invocation.Invocation; 18 import org.mockito.verification.VerificationMode; 19 20 public class Only implements VerificationMode { 21 22 @SuppressWarnings("unchecked") verify(VerificationData data)23 public void verify(VerificationData data) { 24 MatchableInvocation target = data.getTarget(); 25 List<Invocation> invocations = data.getAllInvocations(); 26 List<Invocation> chunk = findInvocations(invocations,target); 27 if (invocations.size() != 1 && !chunk.isEmpty()) { 28 Invocation unverified = findFirstUnverified(invocations); 29 throw noMoreInteractionsWanted(unverified, (List) invocations); 30 } 31 if (invocations.size() != 1 || chunk.isEmpty()) { 32 throw wantedButNotInvoked(target); 33 } 34 markVerified(chunk.get(0), target); 35 } 36 description(String description)37 public VerificationMode description(String description) { 38 return VerificationModeFactory.description(this, description); 39 } 40 } 41