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.InvocationMatcher;
     12 import org.mockito.internal.invocation.InvocationsFinder;
     13 import org.mockito.internal.reporting.SmartPrinter;
     14 import org.mockito.internal.verification.api.InOrderContext;
     15 import org.mockito.internal.verification.argumentmatching.ArgumentMatchingTool;
     16 import org.mockito.invocation.Invocation;
     17 import org.mockito.verification.VerificationMode;
     18 
     19 public class MissingInvocationInOrderChecker {
     20 
     21     private final Reporter reporter;
     22     private final InvocationsFinder finder;
     23 
     24     public MissingInvocationInOrderChecker() {
     25         this(new InvocationsFinder(), new Reporter());
     26     }
     27 
     28     MissingInvocationInOrderChecker(InvocationsFinder finder, Reporter reporter) {
     29         this.finder = finder;
     30         this.reporter = reporter;
     31     }
     32 
     33     public void check(List<Invocation> invocations, InvocationMatcher wanted, VerificationMode mode, InOrderContext context) {
     34         List<Invocation> chunk = finder.findAllMatchingUnverifiedChunks(invocations, wanted, context);
     35 
     36         if (!chunk.isEmpty()) {
     37             return;
     38         }
     39 
     40         Invocation previousInOrder = finder.findPreviousVerifiedInOrder(invocations, context);
     41         if (previousInOrder == null) {
     42             /**
     43              * It is of course possible to have an issue where the arguments are different
     44              * rather that not invoked in order. Issue related to
     45              * http://code.google.com/p/mockito/issues/detail?id=27. If the previous order
     46              * is missing, then this method checks if the arguments are different or if the order
     47              * is not invoked.
     48              */
     49              List<Invocation> actualInvocations = finder.findInvocations(invocations, wanted);
     50              if (actualInvocations == null || actualInvocations.isEmpty())  {
     51                  Invocation similar = finder.findSimilarInvocation(invocations, wanted);
     52                  if (similar != null) {
     53                      Integer[] indicesOfSimilarMatchingArguments =
     54                              new ArgumentMatchingTool().getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(),
     55                                      similar.getArguments());
     56                      SmartPrinter smartPrinter = new SmartPrinter(wanted, similar, indicesOfSimilarMatchingArguments);
     57                      reporter.argumentsAreDifferent(smartPrinter.getWanted(), smartPrinter.getActual(), similar.getLocation());
     58                  } else {
     59                      reporter.wantedButNotInvoked(wanted);
     60                  }
     61              }
     62         } else {
     63             reporter.wantedButNotInvokedInOrder(wanted, previousInOrder);
     64         }
     65     }
     66 }