All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.expectations.UnorderedVerificationPhase Maven / Gradle / Ivy

/*
 * Copyright (c) 2006-2013 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.expectations;

import java.util.*;

import org.jetbrains.annotations.*;

import mockit.internal.expectations.invocation.*;

final class UnorderedVerificationPhase extends BaseVerificationPhase
{
   @NotNull final List verifiedExpectations;

   UnorderedVerificationPhase(
      @NotNull RecordAndReplayExecution recordAndReplay,
      @NotNull List expectationsInReplayOrder, @NotNull List invocationArgumentsInReplayOrder)
   {
      super(recordAndReplay, expectationsInReplayOrder, invocationArgumentsInReplayOrder);
      verifiedExpectations = new ArrayList();
   }

   @Override
   protected void findNonStrictExpectation(
      @Nullable Object mock, @NotNull String mockClassDesc, @NotNull String mockNameAndDesc, @NotNull Object[] args)
   {
      if (!matchInstance && recordAndReplay.executionState.isToBeMatchedOnInstance(mock, mockNameAndDesc)) {
         matchInstance = true;
      }

      replayIndex = -1;

      for (int i = 0, n = expectationsInReplayOrder.size(); i < n; i++) {
         Expectation replayExpectation = expectationsInReplayOrder.get(i);
         Object[] replayArgs = invocationArgumentsInReplayOrder.get(i);

         if (matches(mock, mockClassDesc, mockNameAndDesc, args, replayExpectation, replayArgs)) {
            replayIndex = i;
            expectationBeingVerified().constraints.invocationCount++;
            currentExpectation = replayExpectation;
         }
      }

      if (replayIndex >= 0) {
         pendingError = verifyConstraints();
      }
   }

   @Nullable private Error verifyConstraints()
   {
      ExpectedInvocation lastInvocation = expectationsInReplayOrder.get(replayIndex).invocation;
      Object[] lastArgs = invocationArgumentsInReplayOrder.get(replayIndex);
      int minInvocations = numberOfIterations > 0 ? numberOfIterations : 1;
      int maxInvocations = numberOfIterations > 0 ? numberOfIterations : -1;

      return expectationBeingVerified().verifyConstraints(lastInvocation, lastArgs, minInvocations, maxInvocations);
   }

   @Override
   void addVerifiedExpectation(@NotNull VerifiedExpectation verifiedExpectation)
   {
      super.addVerifiedExpectation(verifiedExpectation);
      verifiedExpectations.add(verifiedExpectation);
   }

   @Override
   public void handleInvocationCountConstraint(int minInvocations, int maxInvocations)
   {
      Expectation verifying = expectationBeingVerified();
      int multiplier = numberOfIterations <= 1 ? 1 : numberOfIterations;
      int iteratedMin = multiplier * minInvocations;

      if (replayIndex >= 0) {
         ExpectedInvocation replayInvocation = expectationsInReplayOrder.get(replayIndex).invocation;
         Object[] replayArgs = invocationArgumentsInReplayOrder.get(replayIndex);
         int iteratedMax = multiplier * maxInvocations;
         pendingError = verifying.verifyConstraints(replayInvocation, replayArgs, iteratedMin, iteratedMax);
      }
      else {
         pendingError = verifying.verifyConstraints(iteratedMin);
      }
   }

   @Override
   public void applyHandlerForEachInvocation(@NotNull Object invocationHandler)
   {
      if (pendingError != null) {
         return;
      }

      InvocationHandlerResult handler = new InvocationHandlerResult(invocationHandler);
      int matchedExpectations = 0;

      for (int i = 0, n = expectationsInReplayOrder.size(); i < n; i++) {
         Expectation expectation = expectationsInReplayOrder.get(i);
         Object[] args = invocationArgumentsInReplayOrder.get(i);

         if (evaluateInvocationHandlerIfExpectationMatchesCurrent(expectation, args, handler, matchedExpectations)) {
            matchedExpectations++;
         }
      }
   }

   @Nullable VerifiedExpectation firstExpectationVerified()
   {
      VerifiedExpectation first = null;

      for (VerifiedExpectation expectation : verifiedExpectations) {
         if (first == null || expectation.replayIndex < first.replayIndex) {
            first = expectation;
         }
      }

      return first;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy