URIConverter implements PropertyConverter<URI> { @Override public URI convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<uri> -> new URI(uri)"); if(value==null){ return null; } String trimmed = value.trim(); if(value.isEmpty()){ return null; } try { return new URI(trimmed); } catch (Exception e) { LOG.log(Level.FINE, "Unparseable URI: " + trimmed, e); return null; } }  @Override URI convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testConvert_URI() throws Exception { URIConverter converter = new URIConverter(); assertThat(new URI("test:path")).isEqualTo(converter.convert("test:path", context)); }
@Test public void testConvert_URI_WithSpaces() throws Exception { URIConverter converter = new URIConverter(); assertThat(new URI("test:path")).isEqualTo(converter.convert(" test:path\t", context)); }
@Test public void testConvert_URI_WithSpacesBefore() throws Exception { URIConverter converter = new URIConverter(); assertThat(new URI("test:path")).isEqualTo(converter.convert(" test:path", context)); }
@Test public void testConvert_URI_WithSpacesAfter() throws Exception { URIConverter converter = new URIConverter(); assertThat(new URI("test:path")).isEqualTo(converter.convert("test:path ", context)); }
@Test public void testConvert_NotPresent() throws Exception { URIConverter converter = new URIConverter(); assertThat(converter.convert("", context)).isNull(); assertThat(converter.convert(null, context)).isNull(); }
@Test public void testConvert_URIInvalid() throws ConfigException { URIConverter converter = new URIConverter(); assertThat(converter.convert("not a uri", context)).isNull(); }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(URI.class)).build(); URIConverter converter = new URIConverter(); converter.convert("test:path", context); assertThat(context.getSupportedFormats()).contains("<uri> -> new URI(uri) (URIConverter)"); }