changeset 111:6369d2282c1d

Add an EmptyStringToNullConverter Add unit test for all inner classes of Converters.
author Omair Majid <omajid@redhat.com>
date Fri, 28 Feb 2014 11:09:26 -0500
parents 93f5d040235d
children 4d5e3d32d681
files com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/editor/ConvertersTest.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Converters.java
diffstat 2 files changed, 112 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/editor/ConvertersTest.java	Fri Feb 28 11:09:26 2014 -0500
@@ -0,0 +1,79 @@
+package com.redhat.thermostat.tools.eclipse.plugin.tests.editor;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.tools.eclipse.plugin.editor.Converters;
+import com.redhat.thermostat.tools.eclipse.plugin.editor.Converters.EmptyStringToNullConverter;
+import com.redhat.thermostat.tools.eclipse.plugin.editor.Converters.ListToStringConverter;
+import com.redhat.thermostat.tools.eclipse.plugin.editor.Converters.StringToListConverter;
+
+public class ConvertersTest {
+
+    @Test
+    public void testListToStringConverterHandlesEmptyList() {
+        List<String> input = new ArrayList<>();
+        ListToStringConverter converter = new Converters.ListToStringConverter();
+        String result = (String) converter.convert(input);
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testListToStringConverterWorks() {
+        List<String> input = new ArrayList<>();
+        input.add("foo");
+        input.add("bar");
+        input.add("baz");
+        ListToStringConverter converter = new Converters.ListToStringConverter();
+        String result = converter.convert(input);
+        assertEquals("foo bar baz", result);
+    }
+
+    @Test
+    public void testStringToListConvertHandlesEmptyString() {
+        String input = "";
+        StringToListConverter converter = new Converters.StringToListConverter();
+        List<String> result = converter.convert(input);
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testStringToListConvertWorks() {
+        String input = "foo bar baz";
+
+        StringToListConverter converter = new Converters.StringToListConverter();
+        List<String> result = converter.convert(input);
+
+        assertNotNull(result);
+
+        ArrayList<Object> expected = new ArrayList<>();
+        expected.add("foo");
+        expected.add("bar");
+        expected.add("baz");
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void testEmptyStringToNullConverterConvertsEmptyStringToNull() {
+        String input = "";
+        EmptyStringToNullConverter converter = new Converters.EmptyStringToNullConverter();
+        String result = converter.convert(input);
+        assertNull(result);
+    }
+
+    @Test
+    public void testEmptyStringToNulLConverterDoesNotModifyNonEmptyStrings() {
+        String input = "foo bar baz";
+        EmptyStringToNullConverter converter = new Converters.EmptyStringToNullConverter();
+        String result = converter.convert(input);
+        assertEquals(result, input);
+    }
+}
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Converters.java	Thu Feb 27 17:12:59 2014 -0500
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/Converters.java	Fri Feb 28 11:09:26 2014 -0500
@@ -1,12 +1,17 @@
 package com.redhat.thermostat.tools.eclipse.plugin.editor;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.core.databinding.conversion.Converter;
 
 public class Converters {
 
+    /**
+     * Converts a {@link List}{@code <String>} to a single {@code String} by
+     * joining all the items in the list with a single space.
+     */
     public static class ListToStringConverter extends Converter {
 
         public ListToStringConverter() {
@@ -14,7 +19,7 @@
         }
 
         @Override
-        public Object convert(Object fromObject) {
+        public String convert(Object fromObject) {
             if (!(fromObject instanceof List)) {
                 throw new AssertionError("Only List objects can be converted");
             }
@@ -33,6 +38,10 @@
         }
     }
 
+    /**
+     * Converts a {@link String} to a {@link List}{@code <String>} by splitting
+     * the {@code String} along spaces.
+     */
     public static class StringToListConverter extends Converter {
 
         public StringToListConverter() {
@@ -40,11 +49,14 @@
         }
 
         @Override
-        public Object convert(Object fromObject) {
+        public List<String> convert(Object fromObject) {
             if (!(fromObject instanceof String)) {
-                throw new AssertionError("Only string objects can be converted");
+                throw new AssertionError("Only String objects can be converted");
             }
             String from = (String) fromObject;
+            if (from.trim().isEmpty()) {
+                return Collections.emptyList();
+            }
             String[] parts = from.trim().split(" +");
             List<String> result = Arrays.asList(parts);
             return result;
@@ -52,4 +64,22 @@
 
     }
 
+    /** Converts an empty String to {@code null}. */
+    public static class EmptyStringToNullConverter extends Converter {
+        public EmptyStringToNullConverter() {
+            super(String.class, String.class);
+        }
+
+        @Override
+        public String convert(Object fromObject) {
+            if (!(fromObject instanceof String)) {
+                throw new IllegalArgumentException("Only strings can be converted. Got a " + fromObject.getClass() + " type");
+            }
+            String in = (String) fromObject;
+            if (in == null || in.trim().isEmpty()) {
+                return null;
+            }
+            return in;
+        }
+    }
 }