AbstractSpecification2 implements Specification2 { @Override public TranslatableString satisfiesTranslatable(final Object obj) { if (obj == null) { return nullability == Nullability.IGNORE_IF_NULL ? null : TranslatableString.tr("Cannot be null"); } if (!expectedType.isInstance(obj)) { return typeChecking == TypeChecking.IGNORE_INCORRECT_TYPE ? null : TranslatableString.tr("Incorrect type"); } final T objAsT = _Casts.uncheckedCast(obj); return satisfiesTranslatableSafely(objAsT); } protected  AbstractSpecification2(); protected  AbstractSpecification2(final Nullability nullability, final TypeChecking typeChecking); @Override final String satisfies(final Object obj); @Override TranslatableString satisfiesTranslatable(final Object obj); abstract TranslatableString satisfiesTranslatableSafely(T obj);  }
@Test public void shouldSatisfyByDefaultForNull() { specAbstractSomeDomainObject = new AbstractSpecification2<String>() { @Override public TranslatableString satisfiesTranslatableSafely(final String obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfiesTranslatable(null), is(nullValue())); }
@Test public void shouldNotSatisfyForNullIfConfiguredAsSuch() { specAbstractSomeDomainObject = new AbstractSpecification2<String>(Nullability.ENSURE_NOT_NULL, TypeChecking.IGNORE_INCORRECT_TYPE) { @Override public TranslatableString satisfiesTranslatableSafely(final String obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfiesTranslatable(null), is(not(nullValue()))); }
@Test public void shouldSatisfyByDefaultForIncorrectType() { specAbstractSomeDomainObject = new AbstractSpecification2<String>() { @Override public TranslatableString satisfiesTranslatableSafely(final String obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfiesTranslatable(Integer.valueOf(1)), is(nullValue())); }
@Test public void shouldNotSatisfyForIncorrectTypeIfConfiguredAsSuch() { specAbstractSomeDomainObject = new AbstractSpecification2<String>(Nullability.IGNORE_IF_NULL, TypeChecking.ENSURE_CORRECT_TYPE) { @Override public TranslatableString satisfiesTranslatableSafely(final String obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfiesTranslatable(Integer.valueOf(1)), is(not(nullValue()))); }
@Test public void shouldSatisfyForNonNullCorrectTypeIfConfiguredAsSuch() { specAbstractSomeDomainObject = new AbstractSpecification2<String>(Nullability.ENSURE_NOT_NULL, TypeChecking.ENSURE_CORRECT_TYPE) { @Override public TranslatableString satisfiesTranslatableSafely(final String obj) { return null; } }; assertThat(specAbstractSomeDomainObject.satisfiesTranslatable(new String()), is(nullValue())); }