ConvertQuery implements Function<Configuration, T> { @Override public T apply(Configuration config) { List<PropertyConverter<T>> converters = config.getContext().getPropertyConverters(type); ConversionContext context = new ConversionContext.Builder(type).setConfiguration(config) .setKey(ConvertQuery.class.getName()).build(); for (PropertyConverter<?> conv : converters) { try { if (conv instanceof OptionalConverter) { continue; } T result = (T) conv.convert(rawValue, context); if (result != null) { return result; } } catch (Exception e) { LOG.log(Level.FINEST, e, () -> "Converter " + conv + " failed to convert to " + type); } } return null; } ConvertQuery(String rawValue, TypeLiteral<T> type); @Override T apply(Configuration config);  }
@Test public void testIntegerQuery() { Configuration config = Configuration.current(); ConvertQuery<Integer> converter = new ConvertQuery<>("101", TypeLiteral.of(Integer.class)); Integer result = converter.apply(config); assertThat(result.longValue()).isEqualTo(101); }
@Test public void testNonGenericQuery() { Configuration config = Configuration.current(); Integer intResult = (Integer) new ConvertQuery("101", TypeLiteral.of(Integer.class)).apply(config); assertThat(intResult.longValue()).isEqualTo(101); Boolean booleanResult = (Boolean) new ConvertQuery("true", TypeLiteral.of(Boolean.class)).apply(config); assertThat(booleanResult).isEqualTo(Boolean.TRUE); }
@Test public void testNullWithoutSuccess() { Configuration config = Configuration.current(); Integer intResult = (Integer) new ConvertQuery("invalid", TypeLiteral.of(Integer.class)).apply(config); assertThat(intResult).isNull(); }