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

mockit.internal.expectations.TestOnlyPhase 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 JMockit developers
 * 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.argumentMatching.*;
import static mockit.internal.util.Utilities.*;

public abstract class TestOnlyPhase extends Phase
{
   boolean matchInstance;
   @Nullable protected List> argMatchers;
   @Nullable Expectation currentExpectation;

   TestOnlyPhase(@Nonnull RecordAndReplayExecution recordAndReplay) { super(recordAndReplay); }

   public final void addArgMatcher(@Nonnull ArgumentMatcher matcher) { getArgumentMatchers().add(matcher); }

   @Nonnull
   private List> getArgumentMatchers() {
      if (argMatchers == null) {
         argMatchers = new ArrayList<>();
      }

      return argMatchers;
   }

   final void moveArgMatcher(@Nonnegative int originalMatcherIndex, @Nonnegative int toIndex) {
      List> matchers = getArgumentMatchers();
      int i = getMatcherPositionIgnoringNulls(originalMatcherIndex, matchers);

      for (i--; i < toIndex; i++) {
         matchers.add(i, null);
      }
   }

   @Nonnegative
   private static int getMatcherPositionIgnoringNulls(@Nonnegative int originalMatcherIndex, @Nonnull List> matchers) {
      int i = 0;

      for (int matchersFound = 0; matchersFound <= originalMatcherIndex; i++) {
         if (matchers.get(i) != null) {
            matchersFound++;
         }
      }

      return i;
   }

   final void setExpectedSingleArgumentType(@Nonnegative int parameterIndex, @Nonnull Class argumentType) {
      ArgumentMatcher newMatcher = ClassMatcher.create(argumentType);
      getArgumentMatchers().set(parameterIndex, newMatcher);
   }

   final void setExpectedMultiArgumentType(@Nonnegative int parameterIndex, @Nonnull Class argumentType) {
      CaptureMatcher matcher = (CaptureMatcher) getArgumentMatchers().get(parameterIndex);
      matcher.setExpectedType(argumentType);
   }

   public void setMaxInvocationCount(int maxInvocations) {
      if (currentExpectation != null) {
         int currentMinimum = currentExpectation.constraints.minInvocations;
         int minInvocations = maxInvocations < 0 ? currentMinimum : Math.min(currentMinimum, maxInvocations);
         handleInvocationCountConstraint(minInvocations, maxInvocations);
      }
   }

   public abstract void handleInvocationCountConstraint(int minInvocations, int maxInvocations);

   static boolean isEnumElement(@Nonnull Object mock) {
      Class mockedClass = mock.getClass();
      return mockedClass.isEnum() && indexOfReference(mockedClass.getEnumConstants(), mock) >= 0;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy