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

mockit.internal.expectations.argumentMatching.HamcrestAdapter 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 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.expectations.argumentMatching;

import javax.annotation.*;

import mockit.internal.util.*;

import org.hamcrest.*;
import org.hamcrest.core.*;

/**
 * Adapts the {@code org.hamcrest.Matcher} interface to {@link ArgumentMatcher}.
 */
public final class HamcrestAdapter implements ArgumentMatcher
{
   @Nonnull private final Matcher hamcrestMatcher;

   public HamcrestAdapter(@Nonnull Matcher matcher) { hamcrestMatcher = matcher; }

   @Override
   public boolean same(@Nonnull HamcrestAdapter other) { return hamcrestMatcher == other.hamcrestMatcher; }

   @Override
   public boolean matches(@Nullable Object argValue)
   {
      return hamcrestMatcher.matches(argValue);
   }

   @Override
   public void writeMismatchPhrase(@Nonnull ArgumentMismatch argumentMismatch)
   {
      Description strDescription = new StringDescription();
      hamcrestMatcher.describeTo(strDescription);
      argumentMismatch.append(strDescription.toString());
   }

   @Nullable
   public Object getInnerValue()
   {
      Object innermostMatcher = getInnermostMatcher();
      return getArgumentValueFromMatcherIfAvailable(innermostMatcher);
   }

   @Nonnull
   private Object getInnermostMatcher()
   {
      Matcher innerMatcher = hamcrestMatcher;

      while (innerMatcher instanceof Is || innerMatcher instanceof IsNot) {
         innerMatcher = FieldReflection.getField(innerMatcher.getClass(), Matcher.class, innerMatcher);
      }

      assert innerMatcher != null;
      return innerMatcher;
   }

   @Nullable
   private static Object getArgumentValueFromMatcherIfAvailable(@Nonnull Object argMatcher)
   {
      if (
         argMatcher instanceof IsEqual || argMatcher instanceof IsSame ||
         "org.hamcrest.number.OrderingComparison".equals(argMatcher.getClass().getName())
      ) {
         return FieldReflection.getField(argMatcher.getClass(), Object.class, argMatcher);
      }

      return null;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy