StringMapConverter implements Converter<Map<String, String>> { @Override public String convertTo(Map<String, String> value) { if (value == null) { throw new ParameterException("Couldn't convert \"null\" to string."); } return Joiner.on(SEPARATOR) .withKeyValueSeparator(KEY_VALUE_SEPARATOR) .useForNull("") .join(value); }  @Override Map<String, String> convertFrom(String value); @Override String convertTo(Map<String, String> value);  }
@Test public void convertToWithNull() throws Exception { expectedException.expect(ParameterException.class); expectedException.expectMessage("Couldn't convert \"null\" to string."); new StringMapConverter().convertTo(null); }
@Test public void convertToWithEmptyMap() throws Exception { final String s = new StringMapConverter().convertTo(Collections.emptyMap()); assertEquals("", s); }
@Test public void convertToWithRegularMap() throws Exception { final String s = new StringMapConverter().convertTo(ImmutableMap.of("key1", "value1", "key2", "value2")); assertEquals("key1:value1,key2:value2", s); }
@Test public void convertToWithMapContainingNull() throws Exception { final Map<String, String> map = new HashMap<>(); map.put("key1", null); map.put(null, "value2"); final String s = new StringMapConverter().convertTo(map); assertEquals("key1:,:value2", s); }