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 automated developer testing. It contains mocking/faking APIs and a code coverage tool, 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.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006-2015 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 javax.annotation.*;

import mockit.internal.expectations.invocation.*;

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

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

   @Override
   protected void findNonStrictExpectation(
      @Nullable Object mock, @Nonnull String mockClassDesc, @Nonnull String mockNameAndDesc, @Nonnull 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 replayInstance = invocationInstancesInReplayOrder.get(i);
         Object[] replayArgs = invocationArgumentsInReplayOrder.get(i);

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

      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(@Nonnull VerifiedExpectation verifiedExpectation)
   {
      super.addVerifiedExpectation(verifiedExpectation);
      verifiedExpectations.add(verifiedExpectation);
   }

   @Override
   public void handleInvocationCountConstraint(int minInvocations, int maxInvocations)
   {
      pendingError = null;

      Expectation verifying = expectationBeingVerified();
      int multiplier = numberOfIterations <= 1 ? 1 : numberOfIterations;
      int iteratedMin = multiplier * minInvocations;
      Error errorThrown;

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

      if (errorThrown != null) {
         throw errorThrown;
      }
   }

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

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

      return first;
   }
}