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

mockit.internal.expectations.invocation.InvocationArguments 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.invocation;

import java.util.*;

import org.jetbrains.annotations.*;

import mockit.external.asm4.*;
import mockit.internal.*;
import mockit.internal.expectations.argumentMatching.*;
import mockit.internal.state.*;
import mockit.internal.util.*;

public final class InvocationArguments
{
   @NotNull final String classDesc;
   @NotNull final String methodNameAndDesc;
   @Nullable final String genericSignature;
   @Nullable final String[] exceptions;
   @NotNull private final ArgumentValuesAndMatchers valuesAndMatchers;
   @Nullable private RealMethod realMethod;

   InvocationArguments(
      int access, @NotNull String classDesc, @NotNull String methodNameAndDesc,
      @Nullable String genericSignature, @Nullable String exceptions, @NotNull Object[] args)
   {
      this.classDesc = classDesc;
      this.methodNameAndDesc = methodNameAndDesc;
      this.genericSignature = genericSignature;
      this.exceptions = exceptions == null ? null : exceptions.split(" ");
      valuesAndMatchers =
         (access & Opcodes.ACC_VARARGS) == 0 ?
            new ArgumentValuesAndMatchersWithoutVarargs(this, args) :
            new ArgumentValuesAndMatchersWithVarargs(this, args);
   }

   @NotNull String getClassName() { return classDesc.replace('/', '.'); }

   boolean isForConstructor() { return methodNameAndDesc.charAt(0) == '<'; }

   @NotNull public Object[] getValues() { return valuesAndMatchers.values; }
   void setValues(@NotNull Object[] values) { valuesAndMatchers.values = values; }

   public void setValuesWithNoMatchers(@NotNull Object[] argsToVerify)
   {
      valuesAndMatchers.setValuesWithNoMatchers(argsToVerify);
   }

   @Nullable public List getMatchers() { return valuesAndMatchers.matchers; }
   public void setMatchers(@Nullable List matchers) { valuesAndMatchers.matchers = matchers; }

   @NotNull
   public Object[] prepareForVerification(@NotNull Object[] argsToVerify, @Nullable List matchers)
   {
      return valuesAndMatchers.prepareForVerification(argsToVerify, matchers);
   }

   public boolean isMatch(@NotNull Object[] replayArgs, @NotNull Map instanceMap)
   {
      TestRun.enterNoMockingZone();

      try {
         return valuesAndMatchers.isMatch(replayArgs, instanceMap);
      }
      finally {
         TestRun.exitNoMockingZone();
      }
   }

   @Nullable
   public Error assertMatch(
      @NotNull Object[] replayArgs, @NotNull Map instanceMap, @Nullable CharSequence errorMessagePrefix)
   {
      return valuesAndMatchers.assertMatch(replayArgs, instanceMap, errorMessagePrefix);
   }

   @NotNull
   Error argumentMismatchMessage(
      int paramIndex, @Nullable Object expected, @Nullable Object actual, @Nullable CharSequence errorMessagePrefix)
   {
      ArgumentMismatch message = new ArgumentMismatch();

      if (errorMessagePrefix != null) {
         message.append(errorMessagePrefix);
         message.append('\n');
      }

      message.append("Parameter ");

      String parameterName = ParameterNames.getName(classDesc, methodNameAndDesc, paramIndex);

      if (parameterName == null) {
         message.append(paramIndex);
      }
      else {
         message.appendFormatted(parameterName);
      }

      message.append(" of ").append(new MethodFormatter(classDesc, methodNameAndDesc).toString());
      message.append(" expected ").appendFormatted(expected);

      if (!message.isFinished()) {
         message.append(", got ").appendFormatted(actual);
      }

      return new UnexpectedInvocation(message.toString());
   }

   @Override
   public String toString()
   {
      MethodFormatter methodFormatter = new MethodFormatter(classDesc, methodNameAndDesc);
      return valuesAndMatchers.toString(methodFormatter);
   }

   public boolean hasEquivalentMatchers(@NotNull InvocationArguments other)
   {
      return valuesAndMatchers.hasEquivalentMatchers(other.valuesAndMatchers);
   }

   @NotNull RealMethod getRealMethod()
   {
      if (realMethod == null) {
         realMethod = new RealMethod(getClassName(), methodNameAndDesc);
      }

      return realMethod;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy