# HG changeset patch # User dirk # Date 1316244493 -7200 # Node ID 4734cbeb868306a11b6b721f1b3b5fd3b6e430ee # Parent 9b10f33bcf90bde3ae14efc8d05b4fc66ac3750b toString of Difference should properly quote the values and not quote null fields. diff -r 9b10f33bcf90 -r 4734cbeb8683 conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java --- a/conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java Sat Sep 17 09:22:30 2011 +0200 +++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java Sat Sep 17 09:28:13 2011 +0200 @@ -21,14 +21,32 @@ { StringBuilder buf = new StringBuilder(128); buf.append(key); - buf.append(" : \""); - buf.append(currentValue); - buf.append("\" - \""); - buf.append(otherValue); - buf.append("\""); + buf.append(" : "); + buf.append(format(currentValue)); + buf.append(" - "); + buf.append(format(otherValue)); return buf.toString(); } + private String format(Object object) + { + if (object == null) + { + return null; + } + + String string = object.toString(); + if (string.startsWith("\"") == false) + { + string = "\"" + string; + } + if (string.endsWith("\"") == false) + { + string = string + "\""; + } + return string; + } + @Override public int hashCode() { diff -r 9b10f33bcf90 -r 4734cbeb8683 conflict-editor/src/test/java/de/codedo/conflicteditor/DifferenceTestCase.java --- a/conflict-editor/src/test/java/de/codedo/conflicteditor/DifferenceTestCase.java Sat Sep 17 09:22:30 2011 +0200 +++ b/conflict-editor/src/test/java/de/codedo/conflicteditor/DifferenceTestCase.java Sat Sep 17 09:28:13 2011 +0200 @@ -2,6 +2,7 @@ package de.codedo.conflicteditor; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; @@ -24,4 +25,32 @@ Difference diff2 = new Difference("other-key", "current", "other"); assertThat(diff1, not(equalTo(diff2))); } + + @Test + public void valuesSholdBeQuoted() + { + Difference diff = new Difference("key", "theValue", null); + assertThat(diff.toString(), is("key : \"theValue\" - null")); + } + + @Test + public void valueWithoutLeadingQuoteShouldBeFormattedSurroundedByQuotes() + { + Difference diff = new Difference("key", "theValue\"", null); + assertThat(diff.toString(), is("key : \"theValue\" - null")); + } + + @Test + public void valueWithoutTrailingQuoteShouldBeFormattedSurroundedByQuotes() + { + Difference diff = new Difference("key", "\"theValue", null); + assertThat(diff.toString(), is("key : \"theValue\" - null")); + } + + @Test + public void nullValuesShouldBeFormattedWithoutSurroundingQuotes() + { + Difference diff = new Difference("key", null, null); + assertThat(diff.toString(), is("key : null - null")); + } }