changeset 22:eea7df5137ac

directly compare the JsonNode objects that are returned as values for a given field name. Difference needs to preserve them so that the JsonNodes can be used when merging conflict JSON documents later.
author dirk
date Sun, 18 Sep 2011 09:35:43 +0200
parents 4734cbeb8683
children 8d5f791f1811
files conflict-editor/src/main/java/de/codedo/conflicteditor/DocumentMatcher.java conflict-editor/src/test/java/de/codedo/conflicteditor/DocumentMatcherTestCase.java
diffstat 2 files changed, 23 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/DocumentMatcher.java	Sat Sep 17 09:28:13 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/DocumentMatcher.java	Sun Sep 18 09:35:43 2011 +0200
@@ -123,13 +123,12 @@
 
     private Difference compareValues(String key, JsonNode original, JsonNode other)
     {
-        Object originalValue = original.getValueAsText();
-        Object otherValue = other.getValueAsText();
-        if (originalValue.equals(otherValue) == false)
+        Difference diff = null;
+        if (original.equals(other) == false)
         {
-            return new Difference(key, originalValue, otherValue);
+            diff = new Difference(key, original, other);
         }
-        return null;
+        return diff;
     }
 
     private Set<String> getFieldNames(JsonNode node)
--- a/conflict-editor/src/test/java/de/codedo/conflicteditor/DocumentMatcherTestCase.java	Sat Sep 17 09:28:13 2011 +0200
+++ b/conflict-editor/src/test/java/de/codedo/conflicteditor/DocumentMatcherTestCase.java	Sun Sep 18 09:35:43 2011 +0200
@@ -4,12 +4,15 @@
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
 
 import java.io.IOException;
 import java.util.List;
 
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.node.ArrayNode;
+import org.codehaus.jackson.node.TextNode;
 import org.junit.Test;
 
 public class DocumentMatcherTestCase extends Object
@@ -33,8 +36,8 @@
         assertThat(diffs, hasSize(1));
 
         Difference actualDifference = diffs.get(0);
-        Difference expectedDifference = new Difference("key", "value", "other-value");
-        assertThat(actualDifference, is(expectedDifference));
+        assertThat(actualDifference.currentValue.toString(), is("\"value\""));
+        assertThat(actualDifference.otherValue.toString(), is("\"other-value\""));
     }
 
     @Test
@@ -63,6 +66,20 @@
         assertThat(actualDifference, is(expectedDifference));
     }
 
+    @Test
+    public void valuesWithDifferentTypesShouldProduceDifference() throws Exception
+    {
+        JsonNode original = parse("{ 'key' : 'value' }");
+        JsonNode conflict = parse("{ 'key' : [ 'one', 'two' ] }");
+
+        List<Difference> diffs = new DocumentMatcher(original, conflict).compare();
+        assertThat(diffs, hasSize(1));
+
+        Difference actualDifference = diffs.get(0);
+        assertThat(actualDifference.currentValue, instanceOf(TextNode.class));
+        assertThat(actualDifference.otherValue, instanceOf(ArrayNode.class));
+    }
+
     private JsonNode parse(String input) throws IOException
     {
         // Jackson cannot parse JSON that uses single quotes ...