diff --git a/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java b/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java index b0f6141d9..35a82dfda 100644 --- a/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java +++ b/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java @@ -33,12 +33,14 @@ import org.springframework.util.StringUtils; * * @author Oliver Gierke * @author Christoph Strobl + * @author John Blum */ public class PropertyReferenceException extends RuntimeException { private static final long serialVersionUID = -5254424051438976570L; - private static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'"; - private static final String HINTS_TEMPLATE = " Did you mean '%s'"; + + static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'"; + static final String HINTS_TEMPLATE = "Did you mean %s"; private final String propertyName; private final TypeInformation type; @@ -105,13 +107,13 @@ public class PropertyReferenceException extends RuntimeException { Collection potentialMatches = getPropertyMatches(); if (!potentialMatches.isEmpty()) { String matches = StringUtils.collectionToDelimitedString(potentialMatches, ",", "'", "'"); + builder.append("; "); builder.append(String.format(HINTS_TEMPLATE, matches)); } if (!alreadyResolvedPath.isEmpty()) { - builder.append(" Traversed path: "); + builder.append("; Traversed path: "); builder.append(alreadyResolvedPath.get(0).toString()); - builder.append("."); } return builder.toString(); diff --git a/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java b/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java index 2dff9403e..fb69066fe 100755 --- a/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java @@ -15,7 +15,8 @@ */ package org.springframework.data.mapping; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import java.util.Collection; import java.util.Collections; @@ -31,6 +32,7 @@ import org.springframework.data.util.TypeInformation; * * @author Oliver Gierke * @author Mark Paluch + * @author John Blum */ public class PropertyReferenceExceptionUnitTests { @@ -68,6 +70,33 @@ public class PropertyReferenceExceptionUnitTests { assertThat(matches).containsExactly("name"); } + @Test // GH-2750 + public void formatsMessageWithTypeInfoAndHintsCorrectly() { + + PropertyReferenceException exception = new PropertyReferenceException("nme", TYPE_INFO, NO_PATHS); + + String expectedMessage = String.format("%s; %s", PropertyReferenceException.ERROR_TEMPLATE, + PropertyReferenceException.HINTS_TEMPLATE); + + assertThat(exception) + .hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'"); + } + + @Test // GH-2750 + public void formatsMessageWithTypeInfoHintsAndPathCorrectly() { + + TypeInformation ctype = ClassTypeInformation.from(C.class); + + PropertyReferenceException exception = new PropertyReferenceException("nme", TYPE_INFO, + Collections.singletonList(PropertyPath.from("b.a", ctype))); + + String expectedMessage = String.format("%s; %s; %s", PropertyReferenceException.ERROR_TEMPLATE, + PropertyReferenceException.HINTS_TEMPLATE, "Traversed path: C.b.a"); + + assertThat(exception) + .hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'"); + } + static class Sample { String name; @@ -80,4 +109,16 @@ public class PropertyReferenceExceptionUnitTests { this.name = name; } } + + static class A { + + } + + static class B { + A a; + } + + static class C { + B b; + } }