EntrySplitter { public <T> boolean parse(Handler<T> handler, T target, String input) { if (input == null) throw new NullPointerException("input == null"); return parse(handler, target, input, 0, input.length()); } EntrySplitter(Builder builder); static Builder newBuilder(); boolean parse(Handler<T> handler, T target, String input); boolean parse(
      Handler<T> handler, T target, String input, int beginIndex, int endIndex);  }
@Test public void parse() { entrySplitter.parse(parseIntoMap, map, "k1=v1,k2=v2"); assertThat(map).containsExactly( entry("k1", "v1"), entry("k2", "v2") ); }
@Test public void parse_singleChars() { entrySplitter.parse(parseIntoMap, map, "k=v,a=b"); assertThat(map).containsExactly( entry("k", "v"), entry("a", "b") ); }
@Test public void parse_valuesAreRequired() { for (String missingValue : Arrays.asList("k1", "k1 ", "k1=v1,k2", "k1 ,k2=v1")) { assertThatThrownBy(() -> entrySplitter.parse(parseIntoMap, map, missingValue)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Invalid input: missing key value separator '='"); } assertThat(map.isEmpty()); }
@Test public void parse_emptyValuesOk() { for (String emptyValue : Arrays.asList("k1=", "k1 =", ",k1=", ",k1 =", "k1 =,")) { entrySplitter.parse(parseIntoMap, map, emptyValue); assertThat(map).containsExactly(entry("k1", "")); map.clear(); } entrySplitter.parse(parseIntoMap, map, "k1=v1,k2="); assertThat(map).containsExactly( entry("k1", "v1"), entry("k2", "") ); }
@Test public void parse_emptyKeysNotOk() { for (String missingKey : Arrays.asList("=", "=v1", ",=", ",=v2")) { assertThatThrownBy(() -> entrySplitter.parse(parseIntoMap, map, missingKey)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Invalid input: no key before '='"); } }
@Test public void parse_whitespaceInKeyValue() { entrySplitter.parse(parseIntoMap, map, "k 1=v 1,k 2=v 2"); assertThat(map).containsExactly( entry("k 1", "v 1"), entry("k 2", "v 2") ); }
@Test public void toleratesButIgnores_empty() { entrySplitter.parse(parseIntoMap, map, ""); assertThat(map.isEmpty()); }
@Test public void toleratesButIgnores_onlyWhitespace() { for (String w : Arrays.asList(" ", "\t")) { entrySplitter.parse(parseIntoMap, map, w); entrySplitter.parse(parseIntoMap, map, w + w); } assertThat(map.isEmpty()); }
@Test public void toleratesButIgnores_emptyMembers() { for (String w : Arrays.asList(" ", "\t")) { entrySplitter.parse(parseIntoMap, map, ","); entrySplitter.parse(parseIntoMap, map, w + ","); entrySplitter.parse(parseIntoMap, map, "," + w); entrySplitter.parse(parseIntoMap, map, ",,"); entrySplitter.parse(parseIntoMap, map, "," + w + ","); entrySplitter.parse(parseIntoMap, map, w + "," + w + "," + w); } assertThat(map.isEmpty()); }
@Test public void parse_badParameters() { assertThatThrownBy(() -> entrySplitter.parse(null, map, "")) .isInstanceOf(NullPointerException.class) .hasMessage("handler == null"); assertThatThrownBy(() -> entrySplitter.parse(parseIntoMap, null, "")) .isInstanceOf(NullPointerException.class) .hasMessage("target == null"); assertThatThrownBy(() -> entrySplitter.parse(parseIntoMap, map, null)) .isInstanceOf(NullPointerException.class) .hasMessage("input == null"); assertThatThrownBy(() -> entrySplitter.parse(parseIntoMap, map, "", -1, 1)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("beginIndex < 0"); assertThatThrownBy(() -> entrySplitter.parse(parseIntoMap, map, "", 0, 2)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("endIndex > input.length()"); }