Home / Class/ CharSequenceToObjectConverterTests Class — spring-boot Architecture

CharSequenceToObjectConverterTests Class — spring-boot Architecture

Architecture documentation for the CharSequenceToObjectConverterTests class in CharSequenceToObjectConverterTests.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/convert/CharSequenceToObjectConverterTests.java lines 38–112

class CharSequenceToObjectConverterTests {

	@ConversionServiceTest
	void convertWhenCanConvertViaToString(ConversionService conversionService) {
		assertThat(conversionService.convert(new StringBuilder("1"), Integer.class)).isOne();
	}

	@ConversionServiceTest
	void convertWhenCanConvertDirectlySkipsStringConversion(ConversionService conversionService) {
		assertThat(conversionService.convert(new String("1"), Long.class)).isOne();
		if (!ConversionServiceArguments.isApplicationConversionService(conversionService)) {
			assertThat(conversionService.convert(new StringBuilder("1"), Long.class)).isEqualTo(2);
		}
	}

	@Test
	@SuppressWarnings("unchecked")
	void convertWhenTargetIsList() {
		ConversionService conversionService = new ApplicationConversionService();
		StringBuilder source = new StringBuilder("1,2,3");
		TypeDescriptor sourceType = TypeDescriptor.valueOf(StringBuilder.class);
		TypeDescriptor targetType = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
		List<String> converted = (List<String>) conversionService.convert(source, sourceType, targetType);
		assertThat(converted).containsExactly("1", "2", "3");
	}

	@Test
	@SuppressWarnings("unchecked")
	void convertWhenTargetIsListAndNotUsingApplicationConversionService() {
		FormattingConversionService conversionService = new DefaultFormattingConversionService();
		conversionService.addConverter(new CharSequenceToObjectConverter(conversionService));
		StringBuilder source = new StringBuilder("1,2,3");
		TypeDescriptor sourceType = TypeDescriptor.valueOf(StringBuilder.class);
		TypeDescriptor targetType = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
		List<String> converted = (List<String>) conversionService.convert(source, sourceType, targetType);
		assertThat(converted).containsExactly("1", "2", "3");
	}

	static Stream<? extends Arguments> conversionServices() {
		return ConversionServiceArguments.with((conversionService) -> {
			conversionService.addConverter(new StringToIntegerConverter());
			conversionService.addConverter(new StringToLongConverter());
			conversionService.addConverter(new CharSequenceToLongConverter());
			conversionService.addConverter(new CharSequenceToObjectConverter(conversionService));
		});
	}

	static class StringToIntegerConverter implements Converter<String, Integer> {

		@Override
		public Integer convert(String source) {
			return Integer.valueOf(source);
		}

	}

	static class StringToLongConverter implements Converter<String, Long> {

		@Override
		public Long convert(String source) {
			return Long.valueOf(source);
		}

	}

	static class CharSequenceToLongConverter implements Converter<CharSequence, Long> {

		@Override
		public Long convert(CharSequence source) {
			return Long.parseLong(source.toString()) + 1;
		}

	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free