Browse Source

DATACMNS-1359 - Improved exception message for missing accessors and getters.

The exception messages used in the PersistentProperty.getRequired(Getter|Setter|Field)(…) now mention the name of the property that's offending.
pull/313/head
Oliver Gierke 8 years ago
parent
commit
7be149b2f5
  1. 6
      src/main/java/org/springframework/data/mapping/PersistentProperty.java
  2. 3
      src/main/java/org/springframework/data/mapping/model/Property.java
  3. 38
      src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

6
src/main/java/org/springframework/data/mapping/PersistentProperty.java

@ -97,7 +97,7 @@ public interface PersistentProperty<P extends PersistentProperty<P>> { @@ -97,7 +97,7 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
Method getter = getGetter();
if (getter == null) {
throw new IllegalArgumentException("No getter available for this persistent property!");
throw new IllegalArgumentException(String.format("No getter available for persistent property %s!", this));
}
return getter;
@ -117,7 +117,7 @@ public interface PersistentProperty<P extends PersistentProperty<P>> { @@ -117,7 +117,7 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
Method setter = getSetter();
if (setter == null) {
throw new IllegalArgumentException("No setter available for this persistent property!");
throw new IllegalArgumentException(String.format("No setter available for persistent property %s!", this));
}
return setter;
@ -131,7 +131,7 @@ public interface PersistentProperty<P extends PersistentProperty<P>> { @@ -131,7 +131,7 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
Field field = getField();
if (field == null) {
throw new IllegalArgumentException("No field backing this persistent property!");
throw new IllegalArgumentException(String.format("No field backing persistent property %s!", this));
}
return field;

3
src/main/java/org/springframework/data/mapping/model/Property.java

@ -64,7 +64,8 @@ public class Property { @@ -64,7 +64,8 @@ public class Property {
);
this.hashCode = Lazy.of(() -> withFieldOrDescriptor(Object::hashCode));
this.name = Lazy.of(() -> withFieldOrDescriptor(Field::getName, FeatureDescriptor::getName));
this.toString = Lazy.of(() -> withFieldOrDescriptor(Object::toString));
this.toString = Lazy.of(() -> withFieldOrDescriptor(Object::toString,
it -> String.format("%s.%s", type.getType().getName(), it.getDisplayName())));
this.getter = descriptor.map(PropertyDescriptor::getReadMethod)//
.filter(it -> getType() != null)//

38
src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

@ -239,6 +239,39 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase @@ -239,6 +239,39 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
assertThatThrownBy(() -> property.getRequiredAnnotation(Transient.class)).isInstanceOf(IllegalStateException.class);
}
@Test // DATACMNS-1359
public void missingRequiredGetterThrowsException() {
SamplePersistentProperty property = getProperty(Sample.class, "field");
assertThatExceptionOfType(IllegalArgumentException.class) //
.isThrownBy(() -> property.getRequiredGetter()) //
.withMessageContaining("field") //
.withMessageContaining(Sample.class.getName());
}
@Test // DATACMNS-1359
public void missingRequiredSetterThrowsException() {
SamplePersistentProperty property = getProperty(Sample.class, "field");
assertThatExceptionOfType(IllegalArgumentException.class) //
.isThrownBy(() -> property.getRequiredSetter()) //
.withMessageContaining("field") //
.withMessageContaining(Sample.class.getName());
}
@Test
public void missingRequiredFieldThrowsException() {
SamplePersistentProperty property = getProperty(NoField.class, "firstname");
assertThatExceptionOfType(IllegalArgumentException.class) //
.isThrownBy(() -> property.getRequiredField()) //
.withMessageContaining("firstname") //
.withMessageContaining(NoField.class.getName());
}
@SuppressWarnings("unchecked")
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@ -410,4 +443,9 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase @@ -410,4 +443,9 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
@interface CustomReadOnly {
}
interface NoField {
String getFirstname();
}
}

Loading…
Cancel
Save