PatternListConverter implements Converter<List<Pattern>> { @Override public List<Pattern> convertFrom(String value) { if (value == null) { throw new ParameterException("Couldn't convert value \"null\" to a list of regular expressions."); } try { final Iterable<String> regexList = Splitter.on(SEPARATOR) .omitEmptyStrings() .trimResults() .split(value); return StreamSupport.stream(regexList.spliterator(), false) .map(Pattern::compile) .collect(Collectors.toList()); } catch (Exception e) { throw new ParameterException(e.getMessage(), e); } }  @Override List<Pattern> convertFrom(String value); @Override String convertTo(List<Pattern> value);  }
@Test public void convertFromValidString() throws Exception { final List<Pattern> patterns = new PatternListConverter().convertFrom("foobar.*,\n foo(bar|baz)"); assertEquals(2, patterns.size()); assertEquals("foobar.*", patterns.get(0).pattern()); assertEquals("foo(bar|baz)", patterns.get(1).pattern()); }
@Test public void convertWithInvalidPattern() throws Exception { expectedException.expect(ParameterException.class); expectedException.expectMessage("Dangling meta character '*' near index 0"); new PatternListConverter().convertFrom("*"); }
@Test public void convertFromNull() throws Exception { expectedException.expect(ParameterException.class); expectedException.expectMessage("Couldn't convert value \"null\" to a list of regular expressions."); new PatternListConverter().convertFrom(null); }
@Test public void convertFromEmptyString() throws Exception { final List<Pattern> patterns = new PatternListConverter().convertFrom(""); assertTrue(patterns.isEmpty()); }
@Test public void convertFromWithBlankValues() throws Exception { final List<Pattern> patterns = new PatternListConverter().convertFrom("foobar.*,,,foo(bar|baz)"); assertEquals(2, patterns.size()); assertEquals("foobar.*", patterns.get(0).pattern()); assertEquals("foo(bar|baz)", patterns.get(1).pattern()); }