target
stringlengths 20
113k
| src_fm
stringlengths 11
86.3k
| src_fm_fc
stringlengths 21
86.4k
| src_fm_fc_co
stringlengths 30
86.4k
| src_fm_fc_ms
stringlengths 42
86.8k
| src_fm_fc_ms_ff
stringlengths 43
86.8k
|
---|---|---|---|---|---|
@Test public void shouldNotBeApplicableWhenNotCurrencyType() { Type type = Boolean.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenIsApplicableAndTypeIsNull() { assertThatThrownBy(() -> converter.isApplicable(null, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Currency.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test void shouldThrowExceptionWhenTypeIsNullAndIsApplicable() { assertThatThrownBy(() -> typeConverter.isApplicable(null, null)) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, YAML); } | YamlConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, YAML); } } | YamlConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, YAML); } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, YAML); } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, YAML); } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldConvertToString() { Currency toConvert = Currency.getInstance("USD"); String converted = converter.toString(Currency.class, toConvert, emptyMap()); assertThat(converted).isEqualTo("USD"); } | @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldReturnNullWhenToStringAndValueIsNull() { String converted = converter.toString(Currency.class, null, emptyMap()); assertThat(converted).isNull(); } | @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenToStringAndTypeIsNull() { Currency toConvert = Currency.getInstance("USD"); assertThatThrownBy(() -> converter.toString(null, toConvert, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public String toString(Type type, Currency value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.getCurrencyCode(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldConvertFromString() { String toConvert = "USD"; Currency converted = converter.fromString(Currency.class, toConvert, emptyMap()); assertThat(converted).isEqualTo(Currency.getInstance("USD")); } | @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenInvalidFormat() { String toConvert = "invalid"; assertThatThrownBy(() -> converter.fromString(Currency.class, toConvert, emptyMap())) .isExactlyInstanceOf(IllegalArgumentException.class); } | @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldReturnNullWhenFromStringAndValueIsNull() { Currency converted = converter.fromString(Currency.class, null, emptyMap()); assertThat(converted).isNull(); } | @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenFromStringAndTypeIsNull() { String toConvert = "USD"; assertThatThrownBy(() -> converter.fromString(null, toConvert, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } | CurrencyConverter implements TypeConverter<Currency> { @Override public Currency fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : Currency.getInstance(value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Currency fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Currency value, Map<String, String> attributes); } |
@Test public void shouldBeApplicableWhenBooleanType() { Type type = Boolean.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isTrue(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldNotBeApplicableWhenNotBooleanType() { Type type = Integer.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldThrowExceptionWhenCheckingIfApplicableAndTypeIsNull() { assertThatThrownBy(() -> converter.isApplicable(null, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && (Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type)); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test void shouldConvertFromString() { String toConvert = "" + "integer: 1\n" + "string: test-1\n"; TestClass converted = typeConverter.fromString(TestClass.class, toConvert, null); assertThat(converted).isEqualTo(new TestClass(1, "test-1")); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldConvertToStringWhenFormatNotSpecifiedAndTrue() { Boolean b = true; String converted = converter.toString(Boolean.class, b, emptyMap()); assertThat(converted).isEqualTo("true"); } | @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldConvertToStringWhenFormatNotSpecifiedAndFalse() { Boolean b = false; String converted = converter.toString(Boolean.class, b, emptyMap()); assertThat(converted).isEqualTo("false"); } | @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldConvertToStringWhenFormatSpecifiedAndTrue() { Boolean b = true; String format = "yes/no"; Map<String, String> attributes = singletonMap("format", format); String converted = converter.toString(Boolean.class, b, attributes); assertThat(converted).isEqualTo("yes"); } | @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldConvertToStringWhenFormatSpecifiedAndFalse() { Boolean b = false; String format = "yes/no"; Map<String, String> attributes = singletonMap("format", format); String converted = converter.toString(Boolean.class, b, attributes); assertThat(converted).isEqualTo("no"); } | @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldReturnNullWhenConvertingToStringAndValueToConvertIsNull() { String converted = converter.toString(Boolean.class, null, emptyMap()); assertThat(converted).isNull(); } | @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldThrowExceptionWhenConvertingToStringAndWrongFormat() { Boolean toConvert = true; String format = "wrong format"; Map<String, String> attributes = singletonMap("format", format); assertThatThrownBy(() -> converter.toString(Boolean.class, toConvert, attributes)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("Invalid 'format' meta-attribute value, it must contain '/' separator character. Provided value is 'wrong format'."); } | @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldThrowExceptionWhenConvertingFromStringAndWrongValue() { String aString = "wrong value"; assertThatThrownBy(() -> converter.fromString(Boolean.class, aString, null)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Unable to convert to Boolean. Unknown value:"); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldThrowExceptionWhenConvertingToStringAndTypeIsNull() { Boolean b = true; assertThatThrownBy(() -> converter.toString(null, b, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public String toString(Type type, Boolean value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (attributes != null && attributes.containsKey(FORMAT)) { String format = attributes.get(FORMAT); Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); return value ? values.getLeft() : values.getRight(); } else { return Objects.toString(value, null); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldConvertFromStringWhenFormatNotSpecifiedAndTrue() { String booleanInString = "true"; Boolean fromConversion = converter.fromString(Boolean.class, booleanInString, emptyMap()); assertThat(fromConversion).isEqualTo(true); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldConvertFromStringWhenFormatNotSpecifiedAndFalse() { String booleanInString = "false"; Boolean fromConversion = converter.fromString(Boolean.class, booleanInString, emptyMap()); assertThat(fromConversion).isEqualTo(false); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test void shouldConvertCollectionFromString() { String toConvert = "" + "- integer: 1\n" + " string: test-1\n" + "- integer: 2\n" + " string: test-2\n"; TypeConverter<List<TestClass>> typeConverter = new YamlConverter<>(); Collection<TestClass> converted = typeConverter.fromString(parameterize(Collection.class, TestClass.class), toConvert, null); assertThat(converted) .hasSize(2) .containsSequence( new TestClass(1, "test-1"), new TestClass(2, "test-2")); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldConvertFromStringWhenFormatSpecifiedAndTrue() { String booleanInString = "on"; String format = "on/off"; Map<String, String> attributes = singletonMap("format", format); Boolean fromConversion = converter.fromString(Boolean.class, booleanInString, attributes); assertThat(fromConversion).isEqualTo(true); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldConvertFromStringWhenFormatSpecifiedAndFalse() { String booleanInString = "off"; String format = "on/off"; Map<String, String> attributes = singletonMap("format", format); Boolean fromConversion = converter.fromString(Boolean.class, booleanInString, attributes); assertThat(fromConversion).isEqualTo(false); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldReturnNullWhenConvertingFromStringAndValueToConvertIsNull() { Boolean fromConversion = converter.fromString(Boolean.class, null, emptyMap()); assertThat(fromConversion).isNull(); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldThrowExceptionWhenConvertingFromStringAndWrongFormat() { String booleanInString = "on"; String format = "wrong format"; Map<String, String> attributes = singletonMap("format", format); assertThatThrownBy(() -> converter.fromString(Boolean.class, booleanInString, attributes)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("Invalid 'format' meta-attribute value, it must contain '/' separator character. Provided value is 'wrong format'."); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldThrowExceptionWhenConvertingFromStringWithFormatAndWrongValue() { String booleanInString = "wrong value"; String format = "yes/no"; Map<String, String> attributes = singletonMap("format", format); assertThatThrownBy(() -> converter.fromString(Boolean.class, booleanInString, attributes)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("Unable to convert to Boolean, values must be either 'yes' or 'no' but provided value is 'wrong value'."); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldThrowExceptionWhenConvertingFromStringAndTypeIsNull() { String booleanInString = "on"; assertThatThrownBy(() -> converter.fromString(null, booleanInString, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); } | BooleanConverter implements TypeConverter<Boolean> { @Override public Boolean fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = (attributes == null) ? null : attributes.get(FORMAT); if (format != null) { Pair<String, String> values = cache.computeIfAbsent(format, this::getValues); if (value.equals(values.getLeft())) { return Boolean.TRUE; } if (value.equals(values.getRight())) { return Boolean.FALSE; } throw new IllegalArgumentException( format("Unable to convert to Boolean, values must be either '%s' or '%s' but provided value is '%s'.", values.getLeft(), values.getRight(), value)); } else { if (TRUE.equals(value)) { return Boolean.TRUE; } if (FALSE.equals(value)) { return Boolean.FALSE; } throw new IllegalArgumentException(format("Unable to convert to Boolean. Unknown value: %s", value)); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Boolean fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Boolean value, Map<String, String> attributes); static final String FORMAT; } |
@Test public void shouldBeApplicableWhenInstantType() { Type type = Instant.class; boolean applicable = instantConverter.isApplicable(type, emptyMap()); assertThat(applicable).isTrue(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldNotBeApplicableWhenNotInstantType() { Type type = Boolean.class; boolean applicable = instantConverter.isApplicable(type, emptyMap()); assertThat(applicable).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldThrowExceptionWhenCheckingIfApplicableAndTypeIsNull() { assertThatThrownBy(() -> instantConverter.isApplicable(null, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Instant.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldConvertToStringWhenFormatNotSpecified() { Instant toConvert = Instant.EPOCH; String converted = instantConverter.toString(Instant.class, toConvert, emptyMap()); assertThat(converted).isEqualTo("1970-01-01T00:00:00Z"); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test void shouldConvertMapFromString() { String toConvert = "" + "first:\n" + " integer: 1\n" + " string: test-1\n" + "second:\n" + " integer: 2\n" + " string: test-2\n"; TypeConverter<Map<String, TestClass>> typeConverter = new YamlConverter<>(); Map<String, TestClass> converted = typeConverter.fromString(parameterize(Map.class, String.class, TestClass.class), toConvert, null); assertThat(converted) .hasSize(2) .containsEntry("first", new TestClass(1, "test-1")) .containsEntry("second", new TestClass(2, "test-2")); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldConvertToStringWhenFormatSpecifiedAndDefaultZone() { Instant toConvert = Instant.EPOCH; String format = "yyyy MM dd HH:mm"; Map<String, String> attributes = singletonMap("format", format); String converted = instantConverter.toString(Instant.class, toConvert, attributes); assertThat(converted).isEqualTo("1969 12 31 18:00"); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldConvertToStringWhenFormatAndZoneSpecified() { Instant toConvert = Instant.EPOCH; String format = "yyyy MM dd HH:mm"; Map<String, String> attributes = new HashMap<>(); attributes.put("format", format); attributes.put("zone", "Europe/London"); String converted = instantConverter.toString(Instant.class, toConvert, attributes); assertThat(converted).isEqualTo("1970 01 01 01:00"); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldConvertToStringWhenFormatSpecifiedAndWrongZone() { Instant toConvert = Instant.EPOCH; String format = "yyyy MM dd HH:mm"; Map<String, String> attributes = new HashMap<>(); attributes.put("zone", "null"); attributes.put("format", format); String converted = instantConverter.toString(Instant.class, toConvert, attributes); assertThat(converted).isEqualTo("1969 12 31 18:00"); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldReturnNullWhenConvertingToStringAndValueToConvertIsNull() { String converted = instantConverter.toString(Instant.class, null, emptyMap()); assertThat(converted).isNull(); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldThrowExceptionWhenConvertingToStringAndWrongFormat() { Instant toConvert = Instant.EPOCH; String format = "invalid format"; Map<String, String> attributes = singletonMap("format", format); assertThatThrownBy(() -> instantConverter.toString(Instant.class, toConvert, attributes)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessage("Unable to convert Instant to String. Invalid format: 'invalid format'"); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldThrowExceptionWhenConvertingToStringAndTypeIsNull() { Instant toConvert = Instant.EPOCH; assertThatThrownBy(() -> instantConverter.toString(null, toConvert, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldThrowExceptionWhenConvertingFromStringAndTypeIsNull() { Instant toConvert = Instant.EPOCH; assertThatThrownBy(() -> instantConverter.toString(null, toConvert, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); } | InstantConverter extends AbstractTemporalAccessorConverter<Instant> { @Override public String toString(Type type, Instant value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } String format = null; ZoneId zoneId = DEFAULT_ZONE; if (attributes != null) { format = attributes.get(FORMAT); String zone = attributes.get(ZONE); if (ZoneId.getAvailableZoneIds().contains(zone)) { zoneId = ZoneId.of(zone); } } try { return format == null ? value.toString() : getFormatterForPattern(format).withZone(zoneId).format(value); } catch (DateTimeException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. ", getSimpleClassName(type)) + "Error occurred during printing.", e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(format("Unable to convert %s to String. " + "Invalid format: '%s'", getSimpleClassName(type), format), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String toString(Type type, Instant value, Map<String, String> attributes); static final String ZONE; } |
@Test public void shouldBeApplicableWhenStringType() { Type type = String.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isTrue(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldNotBeApplicableWhenNotStringType() { Type type = Boolean.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && String.class.isAssignableFrom((Class<?>) type); } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldConvertFromStringWhenFormatNotSpecified() { String in = "One\\\\Two"; String expected = "One\\Two"; String out = converter.fromString(String.class, in, null); assertThat(out).isEqualTo(expected); } | @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test void shouldReturnNullWhenFromStringAndValueIsNull() { TestClass converted = typeConverter.fromString(TestClass.class, null, null); assertThat(converted).isNull(); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldExecuteMainWithoutExceptions() { GenericsExample.main(new String[0]); } | public static void main(String[] args) { new GenericsExample().run(); } | GenericsExample { public static void main(String[] args) { new GenericsExample().run(); } } | GenericsExample { public static void main(String[] args) { new GenericsExample().run(); } } | GenericsExample { public static void main(String[] args) { new GenericsExample().run(); } static void main(String[] args); } | GenericsExample { public static void main(String[] args) { new GenericsExample().run(); } static void main(String[] args); } |
@Test public void shouldConvertFromStringWhenFormatSpecifiedAndFalse() { String in = "One\\\\Two"; String escape = "false"; Map<String, String> attributes = singletonMap("escape", escape); String out = converter.fromString(String.class, in, attributes); assertThat(out).isEqualTo(in); } | @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldConvertFromStringWhenFormatSpecifiedAndTrue() { String in = "One\\\\Two"; String escape = "true"; Map<String, String> attributes = singletonMap("escape", escape); String out = converter.fromString(String.class, in, attributes); assertThat(out).isEqualTo("One\\Two"); } | @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldThrowExceptionWhenConvertingFromStringAndWrongEscapeValue() { String in = "One\\Two"; String escape = "wrong value"; Map<String, String> attributes = singletonMap("escape", escape); assertThatThrownBy(() -> converter.fromString(String.class, in, attributes)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("Invalid 'escape' meta-attribute value, it must be either 'true' or 'false', but 'wrong value' is provided."); } | @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? unescapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldConvertToStringWhenFormatNotSpecified() { String in = "One\\Two"; String expected = "One\\\\Two"; String out = converter.toString(String.class, in, null); assertThat(out).isEqualTo(expected); } | @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldConvertToStringWhenFormatSpecifiedAndFalse() { String in = "One\\Two"; String escape = "false"; Map<String, String> attributes = singletonMap(ESCAPE, escape); String out = converter.toString(String.class, in, attributes); assertThat(out).isEqualTo(in); } | @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldConvertToStringWhenFormatSpecifiedAndTrue() { String in = "One\\Two"; String escape = "true"; Map<String, String> attributes = singletonMap(ESCAPE, escape); String out = converter.toString(String.class, in, attributes); assertThat(out).isEqualTo("One\\\\Two"); } | @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldThrowExceptionWhenConvertingToStringAndWrongEscapeValue() { String in = "One\\Two"; String escape = "wrong value"; Map<String, String> attributes = singletonMap(ESCAPE, escape); assertThatThrownBy(() -> converter.toString(String.class, in, attributes)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("Invalid 'escape' meta-attribute value, it must be either 'true' or 'false', but 'wrong value' is provided."); } | @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); } | StringConverter implements TypeConverter<String> { @Override public String toString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return shouldEscape(attributes) ? escapeJava(value) : value; } StringConverter(boolean escape); StringConverter(); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override String fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, String value, Map<String, String> attributes); static final String ESCAPE; } |
@Test public void shouldProperlyIndicateSupportedTypes() { assertThat(typeConverter.isApplicable(SUPPORTED_LIST_TYPE, null)).isTrue(); assertThat(typeConverter.isApplicable(SUPPORTED_MAP_TYPE, null)).isTrue(); assertThat(typeConverter.isApplicable(SUPPORTED_COMPLEX_TYPE, null)).isTrue(); assertThat(typeConverter.isApplicable(NOT_SUPPORTED_COMPLEX_TYPE, null)).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType() instanceof Class) { Class<?> rawType = (Class<?>) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (List.class.isAssignableFrom(rawType) && actualTypeArguments.length == 1) { Type itemType = actualTypeArguments[0]; return isApplicable(itemType, attributes) || innerTypeConverter.isApplicable(itemType, attributes); } else if (Map.class.isAssignableFrom(rawType) && actualTypeArguments.length == 2) { Type keyType = actualTypeArguments[0]; Type valueType = actualTypeArguments[1]; return (isApplicable(keyType, attributes) || innerTypeConverter.isApplicable(keyType, attributes)) && (isApplicable(valueType, attributes) || innerTypeConverter.isApplicable(valueType, attributes)); } } } return false; } | JsonLikeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType() instanceof Class) { Class<?> rawType = (Class<?>) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (List.class.isAssignableFrom(rawType) && actualTypeArguments.length == 1) { Type itemType = actualTypeArguments[0]; return isApplicable(itemType, attributes) || innerTypeConverter.isApplicable(itemType, attributes); } else if (Map.class.isAssignableFrom(rawType) && actualTypeArguments.length == 2) { Type keyType = actualTypeArguments[0]; Type valueType = actualTypeArguments[1]; return (isApplicable(keyType, attributes) || innerTypeConverter.isApplicable(keyType, attributes)) && (isApplicable(valueType, attributes) || innerTypeConverter.isApplicable(valueType, attributes)); } } } return false; } } | JsonLikeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType() instanceof Class) { Class<?> rawType = (Class<?>) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (List.class.isAssignableFrom(rawType) && actualTypeArguments.length == 1) { Type itemType = actualTypeArguments[0]; return isApplicable(itemType, attributes) || innerTypeConverter.isApplicable(itemType, attributes); } else if (Map.class.isAssignableFrom(rawType) && actualTypeArguments.length == 2) { Type keyType = actualTypeArguments[0]; Type valueType = actualTypeArguments[1]; return (isApplicable(keyType, attributes) || innerTypeConverter.isApplicable(keyType, attributes)) && (isApplicable(valueType, attributes) || innerTypeConverter.isApplicable(valueType, attributes)); } } } return false; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); } | JsonLikeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType() instanceof Class) { Class<?> rawType = (Class<?>) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (List.class.isAssignableFrom(rawType) && actualTypeArguments.length == 1) { Type itemType = actualTypeArguments[0]; return isApplicable(itemType, attributes) || innerTypeConverter.isApplicable(itemType, attributes); } else if (Map.class.isAssignableFrom(rawType) && actualTypeArguments.length == 2) { Type keyType = actualTypeArguments[0]; Type valueType = actualTypeArguments[1]; return (isApplicable(keyType, attributes) || innerTypeConverter.isApplicable(keyType, attributes)) && (isApplicable(valueType, attributes) || innerTypeConverter.isApplicable(valueType, attributes)); } } } return false; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override String toString(Type type, Object value, Map<String, String> attributes); } | JsonLikeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType() instanceof Class) { Class<?> rawType = (Class<?>) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (List.class.isAssignableFrom(rawType) && actualTypeArguments.length == 1) { Type itemType = actualTypeArguments[0]; return isApplicable(itemType, attributes) || innerTypeConverter.isApplicable(itemType, attributes); } else if (Map.class.isAssignableFrom(rawType) && actualTypeArguments.length == 2) { Type keyType = actualTypeArguments[0]; Type valueType = actualTypeArguments[1]; return (isApplicable(keyType, attributes) || innerTypeConverter.isApplicable(keyType, attributes)) && (isApplicable(valueType, attributes) || innerTypeConverter.isApplicable(valueType, attributes)); } } } return false; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override String toString(Type type, Object value, Map<String, String> attributes); static final String FORMAT; static final String COMPACT; static final String JSON; static final String EMPTY_STRING; static final String JSON_NULL; static final String COMPACT_JSON_NULL; static final String COMPACT_JSON_EMPTY; } |
@Test public void shouldProperlyConsumeJsonNullsAndAddNullValuesToBuilder() { boolean compact = false; ObjectBuilder builder = Mockito.mock(ObjectBuilder.class); assertThat(typeConverter.consumeNull(JSON_NULL, 0, builder, compact)).isEqualTo(JSON_NULL.length()); assertThat(typeConverter.consumeNull(JSON_NULL + ',', 0, builder, compact)).isEqualTo(JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL, 3, builder, compact)).isEqualTo(JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + ',', 3, builder, compact)).isEqualTo(JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + ':', 3, builder, compact)).isEqualTo(JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + '}', 3, builder, compact)).isEqualTo(JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + ']', 3, builder, compact)).isEqualTo(JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + ',', 2, builder, compact)).isEqualTo(0); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + '{', 3, builder, compact)).isEqualTo(0); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + '[', 3, builder, compact)).isEqualTo(0); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + '[', 3, builder, compact)).isEqualTo(0); verify(builder, times(7)).addValue(eq(null)); } | int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override String toString(Type type, Object value, Map<String, String> attributes); } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override String toString(Type type, Object value, Map<String, String> attributes); static final String FORMAT; static final String COMPACT; static final String JSON; static final String EMPTY_STRING; static final String JSON_NULL; static final String COMPACT_JSON_NULL; static final String COMPACT_JSON_EMPTY; } |
@Test public void shouldProperlyConsumeCompactJsonNullsAndAddNullValuesToBuilder() { boolean compact = true; ObjectBuilder builder = Mockito.mock(ObjectBuilder.class); assertThat(typeConverter.consumeNull(COMPACT_JSON_NULL, 0, builder, compact)).isEqualTo(COMPACT_JSON_NULL.length()); assertThat(typeConverter.consumeNull(COMPACT_JSON_NULL + ',', 0, builder, compact)).isEqualTo(COMPACT_JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL, 3, builder, compact)).isEqualTo(COMPACT_JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + ',', 3, builder, compact)).isEqualTo(COMPACT_JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + ':', 3, builder, compact)).isEqualTo(COMPACT_JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + '}', 3, builder, compact)).isEqualTo(COMPACT_JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + ']', 3, builder, compact)).isEqualTo(COMPACT_JSON_NULL.length()); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + ',', 2, builder, compact)).isEqualTo(0); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + '{', 3, builder, compact)).isEqualTo(0); assertThat(typeConverter.consumeNull("ABC" + COMPACT_JSON_NULL + '{', 3, builder, compact)).isEqualTo(0); assertThat(typeConverter.consumeNull("ABC" + JSON_NULL + '[', 3, builder, compact)).isEqualTo(0); verify(builder, times(7)).addValue(eq(null)); } | int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override String toString(Type type, Object value, Map<String, String> attributes); } | JsonLikeConverter implements TypeConverter<Object> { int consumeNull(CharSequence value, int current, ObjectBuilder builder, boolean compact) { int found = value.length(); boolean consume = false; if (compact) { if (value.charAt(current) == COMPACT_JSON_NULL.charAt(0)) { if (found != COMPACT_JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == COMPACT_JSON_NULL.length() + current && COMPACT_JSON_NULL.equals(value.subSequence(current, found).toString()); } } else { if (value.charAt(current) == JSON_NULL.charAt(0)) { if (found != JSON_NULL.length() + current) { found = notEscapedIndexOf(value, current, COMMA, COLON, RIGHT_CURLY_BRACE, RIGHT_SQUARE_BRACKET); } consume = found == JSON_NULL.length() + current && JSON_NULL.equals(value.subSequence(current, found).toString()); } } if (consume) { builder.addValue(null); return found - current; } return 0; } JsonLikeConverter(TypeConverter<?> innerTypeConverter); @SuppressWarnings("unchecked") JsonLikeConverter(TypeConverter<?> innerTypeConverter, boolean compactMode); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override String toString(Type type, Object value, Map<String, String> attributes); static final String FORMAT; static final String COMPACT; static final String JSON; static final String EMPTY_STRING; static final String JSON_NULL; static final String COMPACT_JSON_NULL; static final String COMPACT_JSON_EMPTY; } |
@Test void shouldThrowExceptionWhenImproperYamlFormat() { String toConvert = "" + "<integer>1</integer>" + "<string>test-1</string>"; assertThatThrownBy(() -> typeConverter.fromString(TestClass.class, toConvert, null)); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldSupportConvertersFromTheChain() { TypeConverter<?> converter = new ChainedTypeConverter(new StringConverter(false), new IntegerConverter()); assertThat(converter.isApplicable(String.class, null)).isTrue(); assertThat(converter.isApplicable(Integer.class, null)).isTrue(); assertThat(converter.isApplicable(Long.class, null)).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes, false) != null; } | ChainedTypeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes, false) != null; } } | ChainedTypeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes, false) != null; } ChainedTypeConverter(List<TypeConverter<?>> converters); ChainedTypeConverter(TypeConverter<?>... converters); } | ChainedTypeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes, false) != null; } ChainedTypeConverter(List<TypeConverter<?>> converters); ChainedTypeConverter(TypeConverter<?>... converters); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Object value, Map<String, String> attributes); } | ChainedTypeConverter implements TypeConverter<Object> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes, false) != null; } ChainedTypeConverter(List<TypeConverter<?>> converters); ChainedTypeConverter(TypeConverter<?>... converters); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Object value, Map<String, String> attributes); } |
@Test public void shouldThrowIAEWhenTypeIsNotSupported() { assertThrows(IllegalArgumentException.class, () -> { TypeConverter<Object> converter = new ChainedTypeConverter(new StringConverter()); converter.fromString(Long.class, "10", null); }); } | @Override public Object fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes).fromString(type, value, attributes); } | ChainedTypeConverter implements TypeConverter<Object> { @Override public Object fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes).fromString(type, value, attributes); } } | ChainedTypeConverter implements TypeConverter<Object> { @Override public Object fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes).fromString(type, value, attributes); } ChainedTypeConverter(List<TypeConverter<?>> converters); ChainedTypeConverter(TypeConverter<?>... converters); } | ChainedTypeConverter implements TypeConverter<Object> { @Override public Object fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes).fromString(type, value, attributes); } ChainedTypeConverter(List<TypeConverter<?>> converters); ChainedTypeConverter(TypeConverter<?>... converters); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Object value, Map<String, String> attributes); } | ChainedTypeConverter implements TypeConverter<Object> { @Override public Object fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return converterFor(type, attributes).fromString(type, value, attributes); } ChainedTypeConverter(List<TypeConverter<?>> converters); ChainedTypeConverter(TypeConverter<?>... converters); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override Object fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Object value, Map<String, String> attributes); } |
@Test public void shouldBeApplicableWhenFloatType() { Type type = Float.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isTrue(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } |
@Test public void shouldNotBeApplicableWhenNotFloatType() { Type type = Boolean.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenCheckingIfApplicableAndTypeIsNull() { assertThatThrownBy(() -> converter.isApplicable(null, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } | FloatConverter extends AbstractNumberConverter<Float> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Float.class, Float.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } |
@Test public void shouldBeApplicableWhenUrlType() { Type type = URL.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isTrue(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldNotBeApplicableWhenNotUrlType() { Type type = Boolean.class; boolean applicable = converter.isApplicable(type, emptyMap()); assertThat(applicable).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenCheckingIfApplicableAndTypeIsNull() { assertThatThrownBy(() -> converter.isApplicable(null, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && URL.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldConvertToString() throws MalformedURLException { String urlString = "http: URL toConvert = new URL(urlString); String converted = converter.toString(URL.class, toConvert, emptyMap()); assertThat(converted).isEqualTo(urlString); } | @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldReturnNullWhenConvertingToStringAndValueToConvertIsNull() { String converted = converter.toString(URL.class, null, emptyMap()); assertThat(converted).isNull(); } | @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public String toString(Type type, URL value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return Objects.toString(value, null); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test void shouldThrowExceptionWhenTypeIsNullAndFromString() { assertThatThrownBy(() -> typeConverter.fromString(null, null, null)) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldCovertFromString() throws MalformedURLException { String urlInString = "http: URL fromConversion = converter.fromString(URL.class, urlInString, emptyMap()); URL expected = new URL(urlInString); assertThat(fromConversion).isEqualTo(expected); } | @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenMalformedUrl() { String malformedUrlString = "malformed URL"; assertThatThrownBy(() -> converter.fromString(URL.class, malformedUrlString, emptyMap())) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessage("Unable to convert to URL: malformed URL"); } | @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenConvertingFromStringAndTypeIsNull() { String urlInString = "http: assertThatThrownBy(() -> converter.fromString(null, urlInString, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldReturnNullWhenConvertingFromStringAndValueToConvertIsNull() { URL fromConversion = converter.fromString(URL.class, null, emptyMap()); assertThat(fromConversion).isNull(); } | @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } | UrlConverter implements TypeConverter<URL> { @Override public URL fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Unable to convert to URL: %s", value), e); } } @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override URL fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, URL value, Map<String, String> attributes); } |
@Test public void shouldBeApplicableWhenShortType() { Type type = Short.class; boolean applicable = shortTypeConverter.isApplicable(type, emptyMap()); assertThat(applicable).isTrue(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } |
@Test public void shouldNotBeApplicableWhenNotShortType() { Type type = Boolean.class; boolean applicable = shortTypeConverter.isApplicable(type, emptyMap()); assertThat(applicable).isFalse(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenCheckingIfApplicableAndTypeIsNull() { assertThatThrownBy(() -> shortTypeConverter.isApplicable(null, emptyMap())) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } | ShortConverter extends AbstractNumberConverter<Short> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { return isApplicable(type, Short.class, Short.TYPE); } @Override boolean isApplicable(Type type, Map<String, String> attributes); } |
@Test public void shouldAcceptEnumType() { Type type = TestEnum.class; boolean isApplicable = enumTypeAdapter.isApplicable(type, null); assertThat(isApplicable).isTrue(); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Enum.class.isAssignableFrom((Class<?>) type); } | EnumConverter implements TypeConverter<Enum<?>> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Enum.class.isAssignableFrom((Class<?>) type); } } | EnumConverter implements TypeConverter<Enum<?>> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Enum.class.isAssignableFrom((Class<?>) type); } } | EnumConverter implements TypeConverter<Enum<?>> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Enum.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } | EnumConverter implements TypeConverter<Enum<?>> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return type instanceof Class<?> && Enum.class.isAssignableFrom((Class<?>) type); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } |
@Test public void shouldConvertFromString() { Enum<?> testEnum = enumTypeAdapter.fromString(TestEnum.class, "FIRST", null); assertThat(testEnum).isEqualTo(TestEnum.FIRST); } | @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } |
@Test public void shouldThrowExceptionWhenConvertingFromStringAndWrongValue() { String value = "WRONG_VALUE"; assertThatThrownBy(() -> enumTypeAdapter.fromString(TestEnum.class, value, null)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Unable to convert a value to an enumeration:"); } | @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } | EnumConverter implements TypeConverter<Enum<?>> { @SuppressWarnings("unchecked") @Override public Enum<?> fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : toEnumValue((Class<Enum<?>>) type, value); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } |
@Test void shouldConvertToString() { TestClass toConvert = new TestClass(1, "test-1"); String converted = typeConverter.toString(TestClass.class, toConvert, null); assertThat(converted).isEqualTo("" + "integer: 1\n" + "string: test-1\n"); } | @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldConvertToString() { String value = enumTypeAdapter.toString(TestEnum.class, TestEnum.SECOND_VALUE, null); assertThat(value).isEqualTo(TestEnum.SECOND_VALUE.name()); } | @Override public String toString(Type type, Enum<?> value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.name(); } | EnumConverter implements TypeConverter<Enum<?>> { @Override public String toString(Type type, Enum<?> value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.name(); } } | EnumConverter implements TypeConverter<Enum<?>> { @Override public String toString(Type type, Enum<?> value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.name(); } } | EnumConverter implements TypeConverter<Enum<?>> { @Override public String toString(Type type, Enum<?> value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.name(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } | EnumConverter implements TypeConverter<Enum<?>> { @Override public String toString(Type type, Enum<?> value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); return value == null ? null : value.name(); } @Override boolean isApplicable(Type type, Map<String, String> attributes); @SuppressWarnings("unchecked") @Override Enum<?> fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, Enum<?> value, Map<String, String> attributes); } |
@Test public void decryptUnencryptedValueAndResetProvider() { String encrypted = "encrypted"; String decrypted = "decrypted"; ConfigurationValue value = new ConfigurationValue(null, encrypted, true, Encrypted.DEFAULT, null); processor = new ConfigurationValueDecryptingProcessor(decrypter); when(decrypter.getName()).thenReturn(Encrypted.DEFAULT); when(decrypter.decrypt(encrypted)).thenReturn(decrypted); ConfigurationValue processed = processor.process(value); assertThat(processed).isSameAs(value); assertThat(value.isEncrypted()).isFalse(); assertThat(value.getValue()).isEqualTo(decrypted); verify(decrypter, times(1)).decrypt(anyString()); verify(decrypter, times(1)).getName(); } | @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } |
@Test public void shouldNotDecryptUnencryptedValue() { String val = "value"; ConfigurationValue value = new ConfigurationValue(null, val, true, null, null); processor = new ConfigurationValueDecryptingProcessor(decrypter); processor.process(value); assertThat(value.isEncrypted()).isFalse(); assertThat(value.getValue()).isEqualTo(val); verify(decrypter, never()).getName(); verify(decrypter, never()).decrypt(anyString()); } | @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } |
@Test public void shouldNotDecryptEncryptedValueWhenProviderNameDiffers() { String encrypted = "encrypted"; String decrypted = "decrypted"; String encryptionProvider = Encrypted.DEFAULT; ConfigurationValue value = new ConfigurationValue(null, encrypted, true, encryptionProvider, null); processor = new ConfigurationValueDecryptingProcessor(decrypter); when(decrypter.getName()).thenReturn("notMatchingProviderName"); when(decrypter.decrypt(encrypted)).thenReturn(decrypted); ConfigurationValue processed = processor.process(value); assertThat(processed).isSameAs(value); assertThat(value.isEncrypted()).isTrue(); assertThat(value.getEncryptionProvider()).isEqualTo(encryptionProvider); verify(decrypter, times(1)).getName(); verify(decrypter, never()).decrypt(anyString()); } | @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } |
@Test public void shouldThrowIllegalArgumentExceptionFromNoOpConfigurationValueDecrypter() { assertThrows(IllegalArgumentException.class, () -> { processor = new ConfigurationValueDecryptingProcessor(); processor.process(new ConfigurationValue(null, null, true, Encrypted.DEFAULT, null)); }); } | @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } | ConfigurationValueDecryptingProcessor implements ConfigurationValueProcessor { @Override public ConfigurationValue process(ConfigurationValue value) { requireNonNull(value, "value cannot be null"); if (value.isEncrypted() && Objects.equals(value.getEncryptionProvider(), decrypter.getName())) { String encrypted = value.getValue(); String decrypted = decrypter.decrypt(encrypted); value.setDecryptedValue(decrypted); } return value; } ConfigurationValueDecryptingProcessor(); ConfigurationValueDecryptingProcessor(ConfigurationValueDecrypter decrypter); void setDecrypter(ConfigurationValueDecrypter decrypter); @Override ConfigurationValue process(ConfigurationValue value); } |
@Test void shouldConvertCollectionToString() { List<TestClass> toConvert = asList( new TestClass(1, "test-1"), new TestClass(2, "test-2") ); TypeConverter<List<TestClass>> typeConverter = new YamlConverter<>(); String converted = typeConverter.toString(parameterize(Collection.class, TestClass.class), toConvert, null); assertThat(converted).isEqualTo("" + "- integer: 1\n" + " string: test-1\n" + "- integer: 2\n" + " string: test-2\n"); } | @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldConvertMapToString() { Map<String, TestClass> toConvert = new HashMap<>(); toConvert.put("first", new TestClass(1, "test-1")); toConvert.put("second", new TestClass(2, "test-2")); TypeConverter<Map<String, TestClass>> typeConverter = new YamlConverter<>(); String converted = typeConverter.toString(parameterize(Map.class, String.class, TestClass.class), toConvert, null); assertThat(converted).isEqualTo("" + "first:\n" + " integer: 1\n" + " string: test-1\n" + "second:\n" + " integer: 2\n" + " string: test-2\n"); } | @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldReturnNullWhenToStringAndValueIsNull() { String converted = typeConverter.toString(TestClass.class, null, null); assertThat(converted).isNull(); } | @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldThrowExceptionWhenTypeIsNullAndToString() { assertThatThrownBy(() -> typeConverter.toString(null, null, null)) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | YamlConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } YamlConverter(); YamlConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldThrowExceptionWhenTypeIsNullAndIsApplicable() { assertThatThrownBy(() -> typeConverter.isApplicable(null, null)) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, JSON); } | JsonConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, JSON); } } | JsonConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, JSON); } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, JSON); } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public boolean isApplicable(Type type, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); String converter = (attributes == null) ? null : attributes.get(CONVERTER); return ignoreConverterAttribute || Objects.equals(converter, JSON); } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldConvertFromString() { String toConvert = "" + "{" + " \"integer\": \"10\"," + " \"string\": \"test\"" + "}"; TestClass converted = typeConverter.fromString(TestClass.class, toConvert, null); assertThat(converted).isNotNull(); assertThat(converted.integer).isEqualTo(10); assertThat(converted.string).isEqualTo("test"); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldConvertListFromString() { String toConvert = "" + "[" + " {" + " \"integer\": \"1\"," + " \"string\": \"test-1\"" + " }," + " {" + " \"integer\": \"2\"," + " \"string\": \"test-2\"" + " }" + "]"; TypeConverter<List<TestClass>> listConverter = new JsonConverter<>(); List<TestClass> converted = listConverter.fromString(parameterize(List.class, TestClass.class), toConvert, null); assertThat(converted) .hasSize(2) .containsSequence( new TestClass(1, "test-1"), new TestClass(2, "test-2")); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test public void shouldExecuteMainWithoutExceptions() { MetaDataExample.main(new String[0]); } | public static void main(String[] args) { new MetaDataExample().run(); } | MetaDataExample { public static void main(String[] args) { new MetaDataExample().run(); } } | MetaDataExample { public static void main(String[] args) { new MetaDataExample().run(); } } | MetaDataExample { public static void main(String[] args) { new MetaDataExample().run(); } static void main(String[] args); } | MetaDataExample { public static void main(String[] args) { new MetaDataExample().run(); } static void main(String[] args); } |
@Test void shouldConvertMapFromString() { String toConvert = "" + "{" + " \"one\": {" + " \"integer\": \"1\"," + " \"string\": \"test-1\"" + " }," + " \"two\": {" + " \"integer\": \"2\"," + " \"string\": \"test-2\"" + " }" + "}"; TypeConverter<Map<String, TestClass>> listConverter = new JsonConverter<>(); Map<String, TestClass> converted = listConverter.fromString(parameterize(Map.class, String.class, TestClass.class), toConvert, null); assertThat(converted) .hasSize(2) .containsExactly( entry("one", new TestClass(1, "test-1")), entry("two", new TestClass(2, "test-2"))); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldThrowAssertionErrorWhenTryToConvertFromStringWithInvalidProperty() { String toConvert = "" + "{" + " \"int\": \"10\"," + " \"string\": \"test\"" + "}"; assertThatThrownBy(() -> typeConverter.fromString(TestClass.class, toConvert, null)) .isExactlyInstanceOf(IllegalStateException.class) .hasCauseInstanceOf(IOException.class); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldThrowAssertionErrorWhenTryToConvertFromInvalidJsonString() { String toConvert = "" + " \"integer\": \"10\"," + " \"string\": \"test\"" + "}"; assertThatThrownBy(() -> typeConverter.fromString(TestClass.class, toConvert, null)) .isExactlyInstanceOf(IllegalStateException.class) .hasCauseInstanceOf(IOException.class); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldReturnNullWhenFromStringAndValueIsNull() { TestClass converted = typeConverter.fromString(TestClass.class, null, null); assertThat(converted).isNull(); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldThrowExceptionWhenTypeIsNullAndFromString() { assertThatThrownBy(() -> typeConverter.fromString(null, null, null)) .isExactlyInstanceOf(NullPointerException.class) .hasMessage("type cannot be null"); } | @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public T fromString(Type type, String value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { JavaType javaType = TypeFactory.defaultInstance().constructType(type); ObjectReader objectReader = objectMapper.readerFor(javaType); return objectReader.readValue(value); } catch (IOException e) { throw new IllegalStateException(e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
@Test void shouldConvertToString() { TestClass toConvert = new TestClass(); toConvert.integer = 10; toConvert.string = "test"; String converted = typeConverter.toString(TestClass.class, toConvert, null); assertThat(converted).isEqualTo("" + "{" + "\"integer\":10," + "\"string\":\"test\"" + "}"); } | @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } | JsonConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } } | JsonConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); } | JsonConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } | JsonConverter implements TypeConverter<T> { @Override public String toString(Type type, T value, Map<String, String> attributes) { requireNonNull(type, "type cannot be null"); if (value == null) { return null; } try { ObjectWriter objectWriter = objectMapper.writer(); return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { throw new UncheckedIOException("Unable to process JSON.", e); } } JsonConverter(); JsonConverter(boolean ignoreConverterAttribute); @Override boolean isApplicable(Type type, Map<String, String> attributes); @Override T fromString(Type type, String value, Map<String, String> attributes); @Override String toString(Type type, T value, Map<String, String> attributes); } |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.