Home / Class/ WebConversionServiceTests Class — spring-boot Architecture

WebConversionServiceTests Class — spring-boot Architecture

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

Entity Profile

Source Code

core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java lines 43–187

class WebConversionServiceTests {

	@Test
	void defaultDateFormat() {
		WebConversionService conversionService = new WebConversionService(new DateTimeFormatters());
		LocalDate date = LocalDate.of(2020, 4, 26);
		assertThat(conversionService.convert(date, String.class))
			.isEqualTo(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(date));
	}

	@Test
	void isoDateFormat() {
		WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().dateFormat("iso"));
		LocalDate date = LocalDate.of(2020, 4, 26);
		assertThat(conversionService.convert(date, String.class))
			.isEqualTo(DateTimeFormatter.ISO_LOCAL_DATE.format(date));
	}

	@Test
	void customDateFormatWithJavaUtilDate() {
		customDateFormat(Date.from(ZonedDateTime.of(2018, 1, 1, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant()));
	}

	@Test
	void customDateFormatWithJavaTime() {
		customDateFormat(java.time.LocalDate.of(2018, 1, 1));
	}

	@Test
	void defaultTimeFormat() {
		WebConversionService conversionService = new WebConversionService(new DateTimeFormatters());
		LocalTime time = LocalTime.of(12, 45, 23);
		assertThat(conversionService.convert(time, String.class))
			.isEqualTo(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(time));
	}

	@Test
	void isoTimeFormat() {
		WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().timeFormat("iso"));
		LocalTime time = LocalTime.of(12, 45, 23);
		assertThat(conversionService.convert(time, String.class))
			.isEqualTo(DateTimeFormatter.ISO_LOCAL_TIME.format(time));
	}

	@Test
	void isoOffsetTimeFormat() {
		isoOffsetTimeFormat(new DateTimeFormatters().timeFormat("isooffset"));
	}

	@Test
	void hyphenatedIsoOffsetTimeFormat() {
		isoOffsetTimeFormat(new DateTimeFormatters().timeFormat("iso-offset"));
	}

	private void isoOffsetTimeFormat(DateTimeFormatters formatters) {
		WebConversionService conversionService = new WebConversionService(formatters);
		OffsetTime offsetTime = OffsetTime.of(LocalTime.of(12, 45, 23), ZoneOffset.ofHoursMinutes(1, 30));
		assertThat(conversionService.convert(offsetTime, String.class))
			.isEqualTo(DateTimeFormatter.ISO_OFFSET_TIME.format(offsetTime));
	}

	@Test
	void customTimeFormat() {
		WebConversionService conversionService = new WebConversionService(
				new DateTimeFormatters().timeFormat("HH*mm*ss"));
		LocalTime time = LocalTime.of(12, 45, 23);
		assertThat(conversionService.convert(time, String.class)).isEqualTo("12*45*23");
	}

	@Test
	void defaultDateTimeFormat() {
		WebConversionService conversionService = new WebConversionService(new DateTimeFormatters());
		LocalDateTime dateTime = LocalDateTime.of(2020, 4, 26, 12, 45, 23);
		assertThat(conversionService.convert(dateTime, String.class))
			.isEqualTo(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(dateTime));
	}

	@Test
	void isoDateTimeFormat() {
		WebConversionService conversionService = new WebConversionService(
				new DateTimeFormatters().dateTimeFormat("iso"));
		LocalDateTime dateTime = LocalDateTime.of(2020, 4, 26, 12, 45, 23);
		assertThat(conversionService.convert(dateTime, String.class))
			.isEqualTo(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(dateTime));
	}

	@Test
	void isoOffsetDateTimeFormat() {
		isoOffsetDateTimeFormat(new DateTimeFormatters().dateTimeFormat("isooffset"));
	}

	@Test
	void hyphenatedIsoOffsetDateTimeFormat() {
		isoOffsetDateTimeFormat(new DateTimeFormatters().dateTimeFormat("iso-offset"));
	}

	private void isoOffsetDateTimeFormat(DateTimeFormatters formatters) {
		WebConversionService conversionService = new WebConversionService(formatters);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(LocalDate.of(2020, 4, 26), LocalTime.of(12, 45, 23),
				ZoneOffset.ofHoursMinutes(1, 30));
		assertThat(conversionService.convert(offsetDateTime, String.class))
			.isEqualTo(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime));
	}

	@Test
	void customDateTimeFormat() {
		WebConversionService conversionService = new WebConversionService(
				new DateTimeFormatters().dateTimeFormat("dd*MM*yyyy HH*mm*ss"));
		LocalDateTime dateTime = LocalDateTime.of(2020, 4, 26, 12, 45, 23);
		assertThat(conversionService.convert(dateTime, String.class)).isEqualTo("26*04*2020 12*45*23");
	}

	@Test
	void convertFromStringToLocalDate() {
		WebConversionService conversionService = new WebConversionService(
				new DateTimeFormatters().dateFormat("yyyy-MM-dd"));
		LocalDate date = conversionService.convert("2018-01-01", LocalDate.class);
		assertThat(date).isEqualTo(java.time.LocalDate.of(2018, 1, 1));
	}

	@Test
	void convertFromStringToLocalDateWithIsoFormatting() {
		WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().dateFormat("iso"));
		LocalDate date = conversionService.convert("2018-01-01", LocalDate.class);
		assertThat(date).isEqualTo(java.time.LocalDate.of(2018, 1, 1));
	}

	@Test
	void convertFromStringToDateWithIsoFormatting() {
		WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().dateFormat("iso"));
		Date date = conversionService.convert("2018-01-01", Date.class);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		assertThat(calendar.get(Calendar.YEAR)).isEqualTo(2018);
		assertThat(calendar.get(Calendar.MONTH)).isZero();
		assertThat(calendar.get(Calendar.DAY_OF_MONTH)).isOne();
	}

	private void customDateFormat(Object input) {
		WebConversionService conversionService = new WebConversionService(
				new DateTimeFormatters().dateFormat("dd*MM*yyyy"));
		assertThat(conversionService.convert(input, String.class)).isEqualTo("01*01*2018");
	}

}

Analyze Your Own Codebase

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

Try Supermodel Free