• 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 org.mockito.exceptions.Reporter;
9 import org.mockito.internal.invocation.InvocationMarker;
10 import org.mockito.internal.invocation.InvocationMatcher;
11 import org.mockito.internal.invocation.InvocationsFinder;
12 import org.mockito.internal.reporting.Discrepancy;
13 import org.mockito.internal.verification.api.InOrderContext;
14 import org.mockito.invocation.Invocation;
15 import org.mockito.invocation.Location;
16 
17 import java.util.List;
18 
19 public class NonGreedyNumberOfInvocationsInOrderChecker {
20 
21     private final InvocationsFinder finder;
22     private final Reporter reporter;
23     private final InvocationMarker marker;
24 
NonGreedyNumberOfInvocationsInOrderChecker()25     public NonGreedyNumberOfInvocationsInOrderChecker() {
26         this(new InvocationsFinder(), new Reporter(), new InvocationMarker());
27     }
28 
NonGreedyNumberOfInvocationsInOrderChecker(InvocationsFinder finder, Reporter reporter, InvocationMarker marker )29     NonGreedyNumberOfInvocationsInOrderChecker(InvocationsFinder finder, Reporter reporter, InvocationMarker marker ) {
30         this.finder = finder;
31         this.reporter = reporter;
32         this.marker = marker;
33     }
34 
check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context)35     public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) {
36         int actualCount = 0;
37         Location lastLocation = null;
38         while( actualCount < wantedCount ){
39             Invocation next = finder.findFirstMatchingUnverifiedInvocation( invocations, wanted, context );
40             if( next == null ){
41                 reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastLocation );
42             }
43             marker.markVerified( next, wanted );
44             context.markVerified( next );
45             lastLocation = next.getLocation();
46             actualCount++;
47         }
48     }
49 }