Browse Source

Fix formatting in PropertyReferenceException message.

A message like, "No property 'creat' found for type 'User' Did you mean ''created''" is now properly formatted as:
"No property 'creat' found for type 'User'; Did you mean 'created'".

Closes gh-2603.
pull/2772/head
John Blum 3 years ago
parent
commit
69865d6152
  1. 10
      src/main/java/org/springframework/data/mapping/PropertyReferenceException.java
  2. 43
      src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java

10
src/main/java/org/springframework/data/mapping/PropertyReferenceException.java

@ -33,12 +33,14 @@ import org.springframework.util.StringUtils; @@ -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 { @@ -105,13 +107,13 @@ public class PropertyReferenceException extends RuntimeException {
Collection<String> 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();

43
src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java

@ -15,7 +15,8 @@ @@ -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; @@ -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 { @@ -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<C> 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 { @@ -80,4 +109,16 @@ public class PropertyReferenceExceptionUnitTests {
this.name = name;
}
}
static class A {
}
static class B {
A a;
}
static class C {
B b;
}
}

Loading…
Cancel
Save