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

mockit.internal.expectations.argumentMatching.ArgumentMismatch 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.

There is a newer version: 1.7
Show newest version
/*
 * 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.argumentMatching;

import java.lang.reflect.*;

import org.jetbrains.annotations.*;

public final class ArgumentMismatch
{
   @NotNull private final StringBuilder out = new StringBuilder(50);
   @Nullable private String parameterType;
   private boolean finished;

   @Nullable public String getParameterType() { return parameterType; }

   public boolean isFinished() { return finished; }
   void markAsFinished() { finished = true; }

   @Override
   public String toString() { return out.toString(); }

   public ArgumentMismatch append(char c) { out.append(c); return this; }
   public ArgumentMismatch append(int i) { out.append(i); return this; }
   public ArgumentMismatch append(double d) { out.append(d); return this; }
   public ArgumentMismatch append(@Nullable CharSequence str) { out.append(str); return this; }

   public void appendFormatted(
      @Nullable String parameterType, @Nullable Object argumentValue, @Nullable ArgumentMatcher matcher)
   {
      if (matcher == null) {
         appendFormatted(argumentValue);
      }
      else {
         this.parameterType = parameterType;
         matcher.writeMismatchPhrase(this);
      }
   }

   public void appendFormatted(@Nullable Object value)
   {
      if (value == null) {
         out.append("null");
      }
      else if (value instanceof CharSequence) {
         appendCharacters((CharSequence) value);
      }
      else if (value instanceof Character) {
         out.append('"');
         appendEscapedOrPlainCharacter((Character) value);
         out.append('"');
      }
      else if (value instanceof Byte) {
         out.append(value).append('b');
      }
      else if (value instanceof Short) {
         out.append(value).append('s');
      }
      else if (value instanceof Long) {
         out.append(value).append('L');
      }
      else if (value instanceof Float) {
         out.append(value).append('F');
      }
      else if (value.getClass().isArray()) {
         appendArray(value);
      }
      else if (value instanceof ArgumentMatcher) {
         ((ArgumentMatcher) value).writeMismatchPhrase(this);
      }
      else {
         out.append(value);
      }
   }

   private void appendArray(@NotNull Object array)
   {
      out.append('[');
      String separator = "";

      for (int i = 0, n = Array.getLength(array); i < n; i++) {
         Object nextValue = Array.get(array, i);
         out.append(separator);
         appendFormatted(nextValue);
         separator = ", ";
      }

      out.append(']');
   }

   private void appendCharacters(@NotNull CharSequence characters)
   {
      out.append('"');

      for (int i = 0, n = characters.length(); i < n; i++) {
         char c = characters.charAt(i);
         appendEscapedOrPlainCharacter(c);
      }

      out.append('"');
   }

   private void appendEscapedOrPlainCharacter(char c)
   {
      switch (c) {
         case '"':
            out.append("\\\"");
            break;
         case '\t':
            out.append("\\t");
            break;
         case '\n':
            out.append("\\n");
            break;
         case '\r':
            out.append("\\r");
            break;
         default:
            out.append(c);
      }
   }

   public void appendFormatted(@NotNull Object[] values)
   {
      String separator = "";

      for (Object value : values) {
         append(separator).appendFormatted(value);
         separator = ", ";
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy