AbstractSpecification implements Specification { @Override @SuppressWarnings({ "unchecked" }) public final String satisfies(final Object obj) { if (obj == null) { return nullability == Nullability.IGNORE_IF_NULL ? null : "Cannot be null"; } if (!expectedType.isInstance(obj)) { return typeChecking == TypeChecking.IGNORE_INCORRECT_TYPE ? null : "Incorrect type"; } final T objAsT = (T) obj; return satisfiesSafely(objAsT); } protected  AbstractSpecification(); protected  AbstractSpecification(final Nullability nullability, final TypeChecking typeChecking); @Override @SuppressWarnings({ "unchecked" }) final String satisfies(final Object obj); abstract String satisfiesSafely(T obj);  }
@Test public void shouldSatisfyByDefaultForNull() { specAbstractSomeDomainObject = new AbstractSpecification<SomeDomainObject>() { @Override public String satisfiesSafely(final SomeDomainObject obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfies(null), is(nullValue())); }
@Test public void shouldNotSatisfyForNullIfConfiguredAsSuch() { specAbstractSomeDomainObject = new AbstractSpecification<SomeDomainObject>(Nullability.ENSURE_NOT_NULL, TypeChecking.IGNORE_INCORRECT_TYPE) { @Override public String satisfiesSafely(final SomeDomainObject obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfies(null), is(not(nullValue()))); }
@Test public void shouldSatisfyByDefaultForIncorrectType() { specAbstractSomeDomainObject = new AbstractSpecification<SomeDomainObject>() { @Override public String satisfiesSafely(final SomeDomainObject obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfies(new SomeOtherDomainObject()), is(nullValue())); }
@Test public void shouldNotSatisfyForIncorrectTypeIfConfiguredAsSuch() { specAbstractSomeDomainObject = new AbstractSpecification<SomeDomainObject>(Nullability.IGNORE_IF_NULL, TypeChecking.ENSURE_CORRECT_TYPE) { @Override public String satisfiesSafely(final SomeDomainObject obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfies(new SomeOtherDomainObject()), is(not(nullValue()))); }
@Test public void shouldSatisfyForNonNullCorrectTypeIfConfiguredAsSuch() { specAbstractSomeDomainObject = new AbstractSpecification<SomeDomainObject>(Nullability.ENSURE_NOT_NULL, TypeChecking.ENSURE_CORRECT_TYPE) { @Override public String satisfiesSafely(final SomeDomainObject obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfies(new SomeDomainObject()), is(nullValue())); }