• 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 
6 package org.mockito.internal.verification.checkers;
7 
8 import java.util.Arrays;
9 import java.util.List;
10 import org.mockito.internal.reporting.Discrepancy;
11 import org.mockito.internal.verification.api.InOrderContext;
12 import org.mockito.invocation.Invocation;
13 import org.mockito.invocation.Location;
14 import org.mockito.invocation.MatchableInvocation;
15 
16 import static org.mockito.internal.exceptions.Reporter.neverWantedButInvoked;
17 import static org.mockito.internal.exceptions.Reporter.tooLittleActualInvocations;
18 import static org.mockito.internal.exceptions.Reporter.tooLittleActualInvocationsInOrder;
19 import static org.mockito.internal.exceptions.Reporter.tooManyActualInvocations;
20 import static org.mockito.internal.exceptions.Reporter.tooManyActualInvocationsInOrder;
21 import static org.mockito.internal.invocation.InvocationMarker.markVerified;
22 import static org.mockito.internal.invocation.InvocationMarker.markVerifiedInOrder;
23 import static org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation;
24 import static org.mockito.internal.invocation.InvocationsFinder.findInvocations;
25 import static org.mockito.internal.invocation.InvocationsFinder.findMatchingChunk;
26 import static org.mockito.internal.invocation.InvocationsFinder.getAllLocations;
27 
28 public class NumberOfInvocationsChecker {
29 
NumberOfInvocationsChecker()30     private NumberOfInvocationsChecker() {
31     }
32 
checkNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount)33     public static void checkNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount) {
34         List<Invocation> actualInvocations = findInvocations(invocations, wanted);
35 
36         int actualCount = actualInvocations.size();
37         if (wantedCount > actualCount) {
38             List<Location> allLocations = getAllLocations(actualInvocations);
39             throw tooLittleActualInvocations(new Discrepancy(wantedCount, actualCount), wanted, allLocations);
40         }
41         if (wantedCount == 0 && actualCount > 0) {
42             throw neverWantedButInvoked(wanted, getAllLocations(actualInvocations));
43         }
44         if (wantedCount < actualCount) {
45             throw tooManyActualInvocations(wantedCount, actualCount, wanted, getAllLocations(actualInvocations));
46         }
47 
48         markVerified(actualInvocations, wanted);
49     }
50 
checkNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount, InOrderContext context)51     public static void checkNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount, InOrderContext context) {
52         List<Invocation> chunk = findMatchingChunk(invocations, wanted, wantedCount, context);
53 
54         int actualCount = chunk.size();
55 
56         if (wantedCount > actualCount) {
57             List<Location> allLocations = getAllLocations(chunk);
58             throw tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, allLocations);
59         }
60         if (wantedCount < actualCount) {
61             throw tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, getAllLocations(chunk));
62         }
63 
64         markVerifiedInOrder(chunk, wanted, context);
65     }
66 
checkNumberOfInvocationsNonGreedy(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount, InOrderContext context)67     public static void checkNumberOfInvocationsNonGreedy(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount, InOrderContext context) {
68         int actualCount = 0;
69         Location lastLocation = null;
70         while( actualCount < wantedCount ){
71             Invocation next = findFirstMatchingUnverifiedInvocation(invocations, wanted, context );
72             if( next == null ){
73                 throw tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, Arrays.asList(lastLocation));
74             }
75             markVerified( next, wanted );
76             context.markVerified( next );
77             lastLocation = next.getLocation();
78             actualCount++;
79         }
80     }
81 }
82