• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.internal.invocation.InvocationMatcher;
16 import org.mockito.invocation.MatchableInvocation;
17 import org.mockito.internal.verification.api.VerificationData;
18 import org.mockito.invocation.Invocation;
19 import org.mockito.verification.VerificationMode;
20 
21 public class Only implements VerificationMode {
22 
23     @SuppressWarnings("unchecked")
verify(VerificationData data)24     public void verify(VerificationData data) {
25         MatchableInvocation target = data.getTarget();
26         List<Invocation> invocations = data.getAllInvocations();
27         List<Invocation> chunk = findInvocations(invocations,target);
28         if (invocations.size() != 1 && chunk.size() > 0) {
29             Invocation unverified = findFirstUnverified(invocations);
30             throw noMoreInteractionsWanted(unverified, (List) invocations);
31         }
32         if (invocations.size() != 1 || chunk.size() == 0) {
33             throw wantedButNotInvoked(target);
34         }
35         markVerified(chunk.get(0), target);
36     }
37 
description(String description)38     public VerificationMode description(String description) {
39         return VerificationModeFactory.description(this, description);
40     }
41 }
42