PathConverter implements PropertyConverter<Path> { @Override public Path convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(),"<File>"); if(value==null){ return null; } String trimmed = value.trim(); if(value.isEmpty()){ return null; } try { return FileSystems.getDefault().getPath(value); } catch (Exception e) { LOG.log(Level.FINE, "Unparseable Path: " + trimmed, e); return null; } }  @Override Path convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void convert() throws Exception { PathConverter conv = new PathConverter(); Path value = conv.convert("testRoot", context); assertThat(Paths.get("testRoot")).isEqualTo(value); value = conv.convert("foo", context); assertThat(value).isNotNull(); }
@Test public void convertNull() throws Exception { PathConverter conv = new PathConverter(); Path value = conv.convert(null, context); assertThat(value).isNull(); value = conv.convert("", context); assertThat(value).isNull(); }
@Test public void convertInvalidPath() throws Exception { PathConverter conv = new PathConverter(); Path value = conv.convert("/invalid:/\u0000", context); assertThat(value).isNull(); }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Path.class)).build(); PathConverter converter = new PathConverter(); converter.convert("notempty", context); assertThat(context.getSupportedFormats()).contains("<File> (PathConverter)"); }