You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2016/12/23 14:45:43 UTC

svn commit: r1775842 - /sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java

Author: sseifert
Date: Fri Dec 23 14:45:43 2016
New Revision: 1775842

URL: http://svn.apache.org/viewvc?rev=1775842&view=rev
Log:
SLING-6429 Hamcrest Matchers: nicer message display when array comparison fails

Modified:
    sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java

Modified: sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java?rev=1775842&r1=1775841&r2=1775842&view=diff
==============================================================================
--- sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java (original)
+++ sling/trunk/testing/hamcrest/src/main/java/org/apache/sling/hamcrest/matchers/ResourcePropertiesMatcher.java Fri Dec 23 14:45:43 2016
@@ -18,6 +18,8 @@ package org.apache.sling.hamcrest.matche
 
 import java.lang.reflect.Array;
 import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
@@ -38,7 +40,8 @@ public class ResourcePropertiesMatcher e
 
     @Override
     public void describeTo(Description description) {
-        description.appendText("Resource with properties ").appendValueList("[", ",", "]", expectedProps.entrySet());
+        description.appendText("Resource with properties ")
+            .appendValueList("[", ",", "]", convertArraysToStrings(expectedProps).entrySet());
     }
 
     @Override
@@ -95,7 +98,43 @@ public class ResourcePropertiesMatcher e
             mismatchDescription.appendText("was Resource which does not expose a value map via adaptTo(ValueMap.class)");
             return;
         }
-        mismatchDescription.appendText("was Resource with properties ").appendValueList("[", ",", "]", actualProperties.entrySet()).appendText(" (resource: ").appendValue(item).appendText(")");
+        mismatchDescription.appendText("was Resource with properties ")
+             .appendValueList("[", ",", "]", convertArraysToStrings(actualProperties).entrySet())
+             .appendText(" (resource: ")
+             .appendValue(item)
+             .appendText(")");
+    }
+    
+    /**
+     * Convert arrays to string representation to get better message if comparison fails.
+     * @param props Properties
+     * @return Properties with array values converted to strings
+     */
+    private Map<String,Object> convertArraysToStrings(Map<String,Object> props) {
+        SortedMap<String,Object> transformedProps = new TreeMap<String,Object>();
+        for (Map.Entry<String, Object> entry : props.entrySet()) {
+            Object value = entry.getValue();
+            if (value != null && value.getClass().isArray()) {
+                StringBuilder sb = new StringBuilder();
+                sb.append("[");
+                for (int i=0; i<Array.getLength(value); i++) {
+                    if (i > 0) {
+                        sb.append(",");
+                    }
+                    Object item = Array.get(value, i);
+                    if (item == null) {
+                        sb.append("null");
+                    }
+                    else {
+                        sb.append(item.toString());
+                    }
+                }
+                sb.append("]");
+                value = sb.toString();
+            }
+            transformedProps.put(entry.getKey(), value);
+        }
+        return transformedProps;
     }
 
 }
\ No newline at end of file