OptionalFieldDescription.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 java.util.Optional.ofNullable;

  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collection;

  25. import ch.powerunit.extensions.matchers.provideprocessor.Matchable;
  26. import ch.powerunit.extensions.matchers.provideprocessor.ProvidesMatchersAnnotatedElementData;

  27. public class OptionalFieldDescription extends DefaultFieldDescription {

  28.     public OptionalFieldDescription(ProvidesMatchersAnnotatedElementData containingElementMirror,
  29.             FieldDescriptionMirror mirror) {
  30.         super(containingElementMirror, mirror);
  31.     }

  32.     @Override
  33.     protected Collection<FieldDSLMethod> getSpecificFieldDslMethodFor() {
  34.         Collection<FieldDSLMethod> dsl = new ArrayList<>();
  35.         dsl.addAll(getPresentDSL());
  36.         dsl.addAll(getAbsentDSL());
  37.         return dsl;
  38.     }

  39.     private Collection<FieldDSLMethod> getPresentDSL() {
  40.         String fieldType = getFieldType();
  41.         return Arrays.asList(
  42.                 getDslMethodBuilder().withSuffixDeclarationJavadocAndDefault("IsPresent", "with a present optional",
  43.                         "new org.hamcrest.CustomTypeSafeMatcher<" + fieldType
  44.                                 + ">(\"optional is present\"){ public boolean matchesSafely(" + fieldType
  45.                                 + " o) {return o.isPresent();}}"),
  46.                 getDslMethodBuilder().withDeclaration("IsPresentAndIs", generic + " value")
  47.                         .withJavaDoc("with a present optional having a specific value",
  48.                                 "value the value the optional must have")
  49.                         .havingDefault("new org.hamcrest.TypeSafeMatcher<" + fieldType
  50.                                 + ">(){ public boolean matchesSafely(" + fieldType
  51.                                 + " o) {return o.isPresent() && o.get().equals(value);} public void describeTo(org.hamcrest.Description description) {description.appendText(\"optional is present and is \").appendValue(value);}}"),
  52.                 getDslMethodBuilder().withDeclaration("IsPresentAndIs", "org.hamcrest.Matcher<" + generic + "> matcher")
  53.                         .withJavaDoc("with a present optional matching a specified matcher",
  54.                                 "matcher the matcher that must accept the optional value")
  55.                         .havingDefault("new org.hamcrest.TypeSafeMatcher<" + fieldType
  56.                                 + ">(){ public boolean matchesSafely(" + fieldType
  57.                                 + " o) {return o.isPresent() && matcher.matches(o.get());} public void describeTo(org.hamcrest.Description description) {description.appendText(\"optional is present and [\").appendDescriptionOf(matcher).appendText(\"]\");}}"));
  58.     }

  59.     private Collection<FieldDSLMethod> getAbsentDSL() {
  60.         String fieldType = getFieldType();
  61.         return Arrays.asList(
  62.                 getDslMethodBuilder().withSuffixDeclarationJavadocAndDefault("IsNotPresent",
  63.                         "with a not present optional",
  64.                         "new org.hamcrest.CustomTypeSafeMatcher<" + fieldType
  65.                                 + ">(\"optional is not present\"){ public boolean matchesSafely(" + fieldType
  66.                                 + " o) {return !o.isPresent();}}"),
  67.                 getDslMethodBuilder().withSuffixDeclarationJavadocAndDefault("IsAbsent", "with an absent optional",
  68.                         "new org.hamcrest.CustomTypeSafeMatcher<" + fieldType
  69.                                 + ">(\"optional is not present\"){ public boolean matchesSafely(" + fieldType
  70.                                 + " o) {return !o.isPresent();}}"));
  71.     }

  72.     @Override
  73.     public String getFieldCopy(String lhs, String rhs, String ignore) {
  74.         if (!"".equals(generic)) {
  75.             return getFieldCopyForOptional(lhs, rhs, ignore);
  76.         }
  77.         return super.getFieldCopy(lhs, rhs, ignore);
  78.     }

  79.     public String getFieldCopyForOptional(String lhs, String rhs, String paramForHasSameValue) {
  80.         String fieldAccessor = getFieldAccessor();
  81.         String fieldName = getFieldName();
  82.         return "if(" + rhs + "." + fieldAccessor + "==null) {\n  " + lhs + "." + fieldName + "(" + MATCHERS
  83.                 + ".nullValue());\n} else if (!" + rhs + "." + fieldAccessor + ".isPresent()) {\n  " + lhs + "."
  84.                 + fieldName + "IsAbsent();\n} else {\n  " + lhs + "." + fieldName + "IsPresentAndIs("
  85.                 + generateMatcherBuilderReferenceFor(generic, rhs + "." + fieldAccessor + ".get()",
  86.                         paramForHasSameValue)
  87.                 + ");\n}";
  88.     }

  89.     public String generateMatcherBuilderReferenceFor(String generic, String accessor, String paramForHasSameValue) {
  90.         return ofNullable(getByName(generic)).filter(Matchable::hasWithSameValue).map(
  91.                 t -> t.getWithSameValue(false) + "(" + accessor + (t.supportIgnore() ? paramForHasSameValue : "") + ")")
  92.                 .orElse(MATCHERS + ".is(" + accessor + ")");
  93.     }

  94. }