Home / Class/ NumberToPeriodConverterTests Class — spring-boot Architecture

NumberToPeriodConverterTests Class — spring-boot Architecture

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

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/convert/NumberToPeriodConverterTests.java lines 41–83

class NumberToPeriodConverterTests {

	@ConversionServiceTest
	void convertWhenSimpleWithoutSuffixShouldReturnPeriod(ConversionService conversionService) {
		assertThat(convert(conversionService, 10)).hasDays(10);
		assertThat(convert(conversionService, +10)).hasDays(10);
		assertThat(convert(conversionService, -10)).hasDays(-10);
	}

	@ConversionServiceTest
	void convertWhenSimpleWithoutSuffixButWithAnnotationShouldReturnPeriod(ConversionService conversionService) {
		assertThat(convert(conversionService, 10, ChronoUnit.DAYS)).hasDays(10);
		assertThat(convert(conversionService, -10, ChronoUnit.DAYS)).hasDays(-10);
		assertThat(convert(conversionService, 10, ChronoUnit.WEEKS)).isEqualTo(Period.ofWeeks(10));
		assertThat(convert(conversionService, -10, ChronoUnit.WEEKS)).isEqualTo(Period.ofWeeks(-10));
		assertThat(convert(conversionService, 10, ChronoUnit.MONTHS)).hasMonths(10);
		assertThat(convert(conversionService, -10, ChronoUnit.MONTHS)).hasMonths(-10);
		assertThat(convert(conversionService, 10, ChronoUnit.YEARS)).hasYears(10);
		assertThat(convert(conversionService, -10, ChronoUnit.YEARS)).hasYears(-10);
	}

	private @Nullable Period convert(ConversionService conversionService, Integer source) {
		return conversionService.convert(source, Period.class);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private @Nullable Period convert(ConversionService conversionService, Integer source,
			@Nullable ChronoUnit defaultUnit) {
		TypeDescriptor targetType = mock(TypeDescriptor.class);
		if (defaultUnit != null) {
			PeriodUnit unitAnnotation = AnnotationUtils
				.synthesizeAnnotation(Collections.singletonMap("value", defaultUnit), PeriodUnit.class, null);
			given(targetType.getAnnotation(PeriodUnit.class)).willReturn(unitAnnotation);
		}
		given(targetType.getType()).willReturn((Class) Period.class);
		return (Period) conversionService.convert(source, TypeDescriptor.forObject(source), targetType);
	}

	static Stream<? extends Arguments> conversionServices() {
		return ConversionServiceArguments.with(new NumberToPeriodConverter());
	}

}

Analyze Your Own Codebase

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

Try Supermodel Free