changeset 21:4734cbeb8683

toString of Difference should properly quote the values and not quote null fields.
author dirk
date Sat, 17 Sep 2011 09:28:13 +0200
parents 9b10f33bcf90
children eea7df5137ac
files conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java conflict-editor/src/test/java/de/codedo/conflicteditor/DifferenceTestCase.java
diffstat 2 files changed, 52 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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()
     {
--- 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"));
+    }
 }