StringMapConverter implements Converter<Map<String, String>> { @Override public Map<String, String> convertFrom(String value) { if (value == null) { throw new ParameterException("Couldn't convert value \"null\" to a map of strings."); } try { return Splitter.on(SEPARATOR) .omitEmptyStrings() .trimResults() .withKeyValueSeparator(KEY_VALUE_SEPARATOR) .split(value); } catch (Exception e) { throw new ParameterException(e.getMessage(), e); } }  @Override Map<String, String> convertFrom(String value); @Override String convertTo(Map<String, String> value);  }
@Test public void convertFromValidString() throws Exception { final Map<String, String> map = new StringMapConverter().convertFrom("key1:value1, key2:value2"); assertEquals(2, map.size()); assertEquals("value1", map.get("key1")); assertEquals("value2", map.get("key2")); }
@Test public void convertFromInvalidString() throws Exception { expectedException.expect(ParameterException.class); expectedException.expectMessage("Chunk [key1;value1] is not a valid entry"); new StringMapConverter().convertFrom("key1;value1, key2:value2"); }
@Test public void convertFromNull() throws Exception { expectedException.expect(ParameterException.class); expectedException.expectMessage("Couldn't convert value \"null\" to a map of strings."); new StringMapConverter().convertFrom(null); }
@Test public void convertFromEmptyString() throws Exception { final Map<String, String> map = new StringMapConverter().convertFrom(""); assertTrue(map.isEmpty()); }
@Test public void convertToWithBlankKeysAndValues() throws Exception { final Map<String, String> map = new StringMapConverter().convertFrom("key1:, :value2"); assertEquals(2, map.size()); assertEquals("", map.get("key1")); assertEquals("value2", map.get("")); }