protected AbstractSpecification(final Nullability nullability, final TypeChecking typeChecking) { this.expectedType = findExpectedType(getClass()); this.nullability = nullability; this.typeChecking = typeChecking; }
private static Class<?> findExpectedType(final Class<?> fromClass) { for (Class<?> c = fromClass; c != Object.class; c = c.getSuperclass()) { for (final Method method : c.getDeclaredMethods()) { if (isSatisfiesSafelyMethod(method)) { return method.getParameterTypes()[0]; } } } throw new Error("Cannot determine correct type for satisfiesSafely() method."); }
/** * Checks not null and is correct type, and delegates to * {@link #satisfiesSafely(Object)}. */ @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); }
@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()))); }
protected AbstractSpecification(final Nullability nullability, final TypeChecking typeChecking) { this.expectedType = findExpectedType(getClass()); this.nullability = nullability; this.typeChecking = typeChecking; }
private static Class<?> findExpectedType(final Class<?> fromClass) { for (Class<?> c = fromClass; c != Object.class; c = c.getSuperclass()) { for (final Method method : c.getDeclaredMethods()) { if (isSatisfiesSafelyMethod(method)) { return method.getParameterTypes()[0]; } } } throw new Error("Cannot determine correct type for satisfiesSafely() method."); }
/** * Checks not null and is correct type, and delegates to * {@link #satisfiesSafely(Object)}. */ @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); }
@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())); }