# HG changeset patch # User dirk # Date 1316331343 -7200 # Node ID eea7df5137ac921b2a13e53f1584ea1bbd408b43 # Parent 4734cbeb868306a11b6b721f1b3b5fd3b6e430ee 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. diff -r 4734cbeb8683 -r eea7df5137ac conflict-editor/src/main/java/de/codedo/conflicteditor/DocumentMatcher.java --- 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 getFieldNames(JsonNode node) diff -r 4734cbeb8683 -r eea7df5137ac conflict-editor/src/test/java/de/codedo/conflicteditor/DocumentMatcherTestCase.java --- 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 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 ...