You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2025/06/18 14:52:13 UTC
(sling-org-apache-sling-testing-hamcrest) 01/01: SLING-12833 Improved output for mismatch
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch feature/improved-mismatch-output-for-resource-properties-matcher
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-hamcrest.git
commit ed90660ec0cde7947f3e6e64230b01d59a4cf67f
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed Jun 18 16:52:03 2025 +0200
SLING-12833 Improved output for mismatch
Highlight the first property name, value and type where a mismatch
occurred.
---
.../matchers/ResourcePropertiesMatcher.java | 29 ++++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java b/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
index 4bbba9e..461e449 100644
--- a/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
+++ b/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
@@ -29,12 +29,13 @@ import org.hamcrest.TypeSafeMatcher;
public class ResourcePropertiesMatcher extends TypeSafeMatcher<Resource> {
private final Map<String, Object> expectedProps;
+ private String firstMismatchPropertyName;
public ResourcePropertiesMatcher(Map<String, Object> properties) {
if (properties == null || properties.isEmpty()) {
throw new IllegalArgumentException("properties is null or empty");
}
-
+ firstMismatchPropertyName = null;
this.expectedProps = properties;
}
@@ -53,11 +54,13 @@ public class ResourcePropertiesMatcher extends TypeSafeMatcher<Resource> {
if (givenValue != null && expectedValue != null
&& givenValue.getClass().isArray() && expectedValue.getClass().isArray()) {
if (!arrayEquals(expectedValue, givenValue)) {
+ firstMismatchPropertyName = prop.getKey();
return false;
}
}
else {
if (!objectEquals(expectedValue, givenValue)) {
+ firstMismatchPropertyName = prop.getKey();
return false;
}
}
@@ -99,8 +102,30 @@ public class ResourcePropertiesMatcher extends TypeSafeMatcher<Resource> {
.appendText(" (resource: ")
.appendValue(item)
.appendText(")");
+ if (firstMismatchPropertyName != null) {
+ Object expectedValue = expectedProps.get(firstMismatchPropertyName);
+ Object actualValue = actualProperties.get(firstMismatchPropertyName);
+ mismatchDescription.appendText(System.lineSeparator());
+ mismatchDescription.appendText(" First mismatch in property ")
+ .appendValue(firstMismatchPropertyName)
+ .appendText(": expected ");
+ appendValueAndType(mismatchDescription, expectedValue);
+ mismatchDescription.appendText(" but was ");
+ appendValueAndType(mismatchDescription, actualValue);
+ }
}
-
+
+ private static void appendValueAndType(Description mismatchDescription, Object value) {
+ if (value == null) {
+ mismatchDescription.appendText("null");
+ } else {
+ mismatchDescription.appendText("value ")
+ .appendValue(value)
+ .appendText(" of type ")
+ .appendValue(value.getClass().getName());
+ }
+ }
+
/**
* Convert arrays to string representation to get better message if comparison fails.
* @param props Properties