FieldDescriptionMetaData.java

  1. /**
  2.  * Powerunit - A JDK1.8 test framework
  3.  * Copyright (C) 2014 Mathieu Boretti.
  4.  *
  5.  * This file is part of Powerunit
  6.  *
  7.  * Powerunit is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * Powerunit is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20. package ch.powerunit.extensions.matchers.provideprocessor.fields;

  21. import static ch.powerunit.extensions.matchers.common.CommonUtils.toJavaSyntax;
  22. import static java.lang.String.format;
  23. import static java.util.stream.Collectors.joining;

  24. import java.util.Optional;

  25. import javax.lang.model.element.Element;
  26. import javax.lang.model.type.DeclaredType;
  27. import javax.lang.model.type.TypeMirror;

  28. import ch.powerunit.extensions.matchers.provideprocessor.Matchable;
  29. import ch.powerunit.extensions.matchers.provideprocessor.ProvidesMatchersAnnotatedElementData;
  30. import ch.powerunit.extensions.matchers.provideprocessor.helper.FeatureMatcher;

  31. public abstract class FieldDescriptionMetaData extends AbstractFieldDescriptionContainerMetaData {

  32.     public static final String SEE_TEXT_FOR_IS_MATCHER = "org.hamcrest.Matchers#is(java.lang.Object)";
  33.     public static final String SEE_TEXT_FOR_HAMCREST_MATCHER = "org.hamcrest.Matchers The main class from hamcrest that provides default matchers.";
  34.     public static final String MATCHERS = "org.hamcrest.Matchers";

  35.     protected final String generic;
  36.     protected final String defaultReturnMethod;
  37.     protected final FieldDescriptionMirror mirror;

  38.     public static final String computeGenericInformation(TypeMirror fieldTypeMirror) {
  39.         if (fieldTypeMirror instanceof DeclaredType) {
  40.             DeclaredType dt = ((DeclaredType) fieldTypeMirror);
  41.             return dt.getTypeArguments().stream().map(Object::toString).collect(joining(","));
  42.         }
  43.         return "";
  44.     }

  45.     public FieldDescriptionMetaData(ProvidesMatchersAnnotatedElementData containingElementMirror,
  46.             FieldDescriptionMirror mirror) {
  47.         super(containingElementMirror);
  48.         this.mirror = mirror;
  49.         TypeMirror fieldTypeMirror = mirror.getFieldTypeMirror();
  50.         this.defaultReturnMethod = containingElementMirror.getDefaultReturnMethod();
  51.         this.generic = computeGenericInformation(fieldTypeMirror);
  52.     }

  53.     public String getMatcherForField() {
  54.         return new FeatureMatcher(mirror.getMethodFieldName(), containingElementMirror.getFullGeneric(),
  55.                 getFullyQualifiedNameEnclosingClassOfField(), containingElementMirror.getGeneric(), getFieldType(),
  56.                 getFieldName(), "actual." + getFieldAccessor()).toString();
  57.     }

  58.     public String getFieldCopy(String lhs, String rhs, String ignore) {
  59.         return getTargetAsMatchable().filter(a -> mirror.getFieldTypeAsTypeElement().getTypeParameters().isEmpty())
  60.                 .filter(Matchable::hasWithSameValue)
  61.                 .map(p -> format(
  62.                         "%1$s.%6$s(%2$s.%3$s == null ? org.hamcrest.Matchers.nullValue() : %4$s(%2$s.%3$s%5$s));", lhs,
  63.                         rhs, getFieldAccessor(), p.getWithSameValue(false), p.supportIgnore() ? ignore : "",
  64.                         getFieldName()))
  65.                 .orElseGet(() -> format(
  66.                         "%1$s.%4$s((org.hamcrest.Matcher)org.hamcrest.Matchers.is((java.lang.Object)%2$s.%3$s));", lhs,
  67.                         rhs, getFieldAccessor(), getFieldName()));
  68.     }

  69.     public String asMatchesSafely() {
  70.         return format(
  71.                 "if(!%1$s.matches(actual)) {\n  mismatchDescription.appendText(\"[\"); %1$s.describeMismatch(actual,mismatchDescription); mismatchDescription.appendText(\"]\\n\");\n  result=false;\n}",
  72.                 getFieldName());
  73.     }

  74.     public String asDescribeTo() {
  75.         return "description.appendText(\"[\").appendDescriptionOf(" + getFieldName() + ").appendText(\"]\\n\");";
  76.     }

  77.     public String asMatcherField() {
  78.         return format("private %1$sMatcher %2$s = new %1$sMatcher(%3$s.anything(%4$s));", mirror.getMethodFieldName(),
  79.                 getFieldName(), MATCHERS, "");
  80.     }

  81.     public String getFullyQualifiedNameEnclosingClassOfField() {
  82.         return containingElementMirror.getFullyQualifiedNameOfClassAnnotatedWithProvideMatcher();
  83.     }

  84.     public String getDefaultReturnMethod() {
  85.         return defaultReturnMethod;
  86.     }

  87.     public String getFieldAccessor() {
  88.         return mirror.getFieldAccessor();
  89.     }

  90.     public String getFieldName() {
  91.         return mirror.getFieldName();
  92.     }

  93.     public String getFieldType() {
  94.         return mirror.getFieldType();
  95.     }

  96.     public Element getFieldElement() {
  97.         return mirror.getFieldElement();
  98.     }

  99.     public FieldDescriptionMirror getMirror() {
  100.         return mirror;
  101.     }

  102.     public Optional<Matchable> getTargetAsMatchable() {
  103.         return mirror.getMatchable(containingElementMirror.getRoundMirror());
  104.     }

  105.     public String getGeneric() {
  106.         return generic;
  107.     }

  108.     public String generateMetadata(String className) {
  109.         return "new " + className + "(" + toJavaSyntax(getFieldName()) + "," + toJavaSyntax(getFieldType()) + ","
  110.                 + toJavaSyntax(getFieldAccessor()) + "," + toJavaSyntax(getClass().getSimpleName()) + ","
  111.                 + Boolean.toString(this instanceof IgnoreFieldDescription) + ")";
  112.     }

  113. }