AssertThatObjectImpl.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.impl;

  21. import java.util.function.Function;
  22. import java.util.function.Supplier;

  23. import org.hamcrest.Description;
  24. import org.hamcrest.Matcher;
  25. import org.hamcrest.StringDescription;

  26. import ch.powerunit.AssertThatCastableObject;
  27. import ch.powerunit.AssertThatObject;
  28. import ch.powerunit.exception.AssumptionError;
  29. import ch.powerunit.exception.InternalError;

  30. public class AssertThatObjectImpl<T> implements AssertThatObject<T>,
  31.         AssertThatCastableObject<T> {

  32.     private final boolean assertion;

  33.     private final Object underTest;

  34.     public AssertThatObjectImpl(Object underTest,boolean assertion, String msg,
  35.             Supplier<T> provider) {
  36.         this.underTest = underTest;
  37.         this.provider = provider;
  38.         this.msg = msg;
  39.         this.assertion = assertion;
  40.     }

  41.     private final Supplier<T> provider;

  42.     private final String msg;

  43.     @Override
  44.     public boolean is(Matcher<? super T> matching) {
  45.         Description message = new StringDescription();
  46.         message.appendText("expecting ");
  47.         matching.describeTo(message);
  48.         message.appendText(" but ");
  49.         T obj = provider.get();
  50.         if (!matching.matches(obj)) {
  51.             matching.describeMismatch(obj, message);
  52.             if (assertion) {
  53.                 TestContextImpl<Object> ctx = DefaultPowerUnitRunnerImpl
  54.                         .getCurrentContext(underTest);
  55.                 AssertionError e = new AssertionError((msg == null ? "" : msg
  56.                         + "\n")
  57.                         + message.toString());
  58.                 if (ctx == null || ctx.isFastFail()) {
  59.                     throw e;
  60.                 } else {
  61.                     ctx.addAssertionError(e);
  62.                     return false;
  63.                 }
  64.             } else {
  65.                 throw new AssumptionError((msg == null ? "" : msg + "\n")
  66.                         + message.toString());
  67.             }
  68.         }
  69.         return true;
  70.     }

  71.     @Override
  72.     public <P extends T> AssertThatObject<P> as(Class<P> clazz) {
  73.         if (clazz == null) {
  74.             throw new InternalError("clazz argument can't be null");
  75.         }
  76.         return new AssertThatObjectImpl<P>(underTest,assertion, msg, () -> {
  77.             T v = provider.get();
  78.             if (v == null) {
  79.                 return null;
  80.             } else if (!clazz.isAssignableFrom(v.getClass())) {
  81.                 if (assertion) {
  82.                     throw new AssertionError((msg == null ? "" : msg + "\n")
  83.                             + "The value " + v + " can't be casted to "
  84.                             + clazz.getName());
  85.                 } else {
  86.                     throw new AssumptionError((msg == null ? "" : msg + "\n")
  87.                             + "The value " + v + " can't be casted to "
  88.                             + clazz.getName());
  89.                 }
  90.             } else {
  91.                 return (P) v;
  92.             }
  93.         });
  94.     }

  95.     @Override
  96.     public <P> AssertThatCastableObject<P> as(Class<P> targetClass,
  97.             Function<T, P> converter) {
  98.         if (targetClass == null) {
  99.             throw new InternalError("targetClass argument can't be null");
  100.         }
  101.         return new AssertThatObjectImpl<P>(underTest,assertion, msg,
  102.                 () -> converter.apply(provider.get()));
  103.     }
  104. }