Home | History | Annotate | Download | only in checkers
      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.List;
      9 
     10 import org.mockito.exceptions.Reporter;
     11 import org.mockito.internal.invocation.InvocationMarker;
     12 import org.mockito.internal.invocation.InvocationMatcher;
     13 import org.mockito.internal.invocation.InvocationsFinder;
     14 import org.mockito.internal.reporting.Discrepancy;
     15 import org.mockito.internal.verification.api.InOrderContext;
     16 import org.mockito.invocation.Invocation;
     17 import org.mockito.invocation.Location;
     18 
     19 public class NumberOfInvocationsInOrderChecker {
     20 
     21     private final Reporter reporter;
     22     private final InvocationsFinder finder;
     23     private final InvocationMarker invocationMarker = new InvocationMarker();
     24 
     25     public NumberOfInvocationsInOrderChecker() {
     26         this(new InvocationsFinder(), new Reporter());
     27     }
     28 
     29     NumberOfInvocationsInOrderChecker(InvocationsFinder finder, Reporter reporter) {
     30         this.finder = finder;
     31         this.reporter = reporter;
     32     }
     33 
     34     public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) {
     35         List<Invocation> chunk = finder.findMatchingChunk(invocations, wanted, wantedCount, context);
     36 
     37         int actualCount = chunk.size();
     38 
     39         if (wantedCount > actualCount) {
     40             Location lastInvocation = finder.getLastLocation(chunk);
     41             reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation);
     42         } else if (wantedCount < actualCount) {
     43             Location firstUndesired = chunk.get(wantedCount).getLocation();
     44             reporter.tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, firstUndesired);
     45         }
     46 
     47         invocationMarker.markVerifiedInOrder(chunk, wanted, context);
     48     }
     49 }