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

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

Go to download

JMockit is a Java toolkit for developer (unit/integration) testing. It contains mocking APIs and other tools, supporting both JUnit and TestNG. The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested in isolation from selected dependencies.

The newest version!
/*
 * Copyright (c) 2006-2014 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);
      }
   }

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

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

      return first;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy