Home / Class/ InputStreamSourceToByteArrayConverterTests Class — spring-boot Architecture

InputStreamSourceToByteArrayConverterTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/convert/InputStreamSourceToByteArrayConverterTests.java lines 43–109

class InputStreamSourceToByteArrayConverterTests {

	@ConversionServiceTest
	void convertConvertsSource(ConversionService conversionService) {
		InputStreamSource source = () -> new ByteArrayInputStream(new byte[] { 0, 1, 2 });
		assertThat(conversionService.convert(source, byte[].class)).containsExactly(0, 1, 2);
	}

	@ConversionServiceTest
	void convertWhenFailsWithIOExceptionThrowsException(ConversionService conversionService) throws Exception {
		InputStreamSource source = mock(InputStreamSource.class);
		given(source.getInputStream()).willThrow(IOException.class);
		assertThatExceptionOfType(ConversionFailedException.class)
			.isThrownBy(() -> conversionService.convert(source, byte[].class))
			.havingCause()
			.isInstanceOf(IllegalStateException.class)
			.withMessageContaining("Unable to read from input stream source");
	}

	@ConversionServiceTest
	void convertWhenFailsWithIOExceptionFromOriginProviderThrowsException(ConversionService conversionService)
			throws Exception {
		Origin origin = new TestOrigin("mylocation");
		InputStreamSource source = mock(InputStreamSource.class, withSettings().extraInterfaces(OriginProvider.class));
		given(source.getInputStream()).willThrow(IOException.class);
		given(((OriginProvider) source).getOrigin()).willReturn(origin);
		assertThatExceptionOfType(ConversionFailedException.class)
			.isThrownBy(() -> conversionService.convert(source, byte[].class))
			.havingCause()
			.isInstanceOf(IllegalStateException.class)
			.withMessageContaining("Unable to read from mylocation");
	}

	@ConversionServiceTest
	void convertWhenFailsWithIOExceptionFromResourceThrowsException(ConversionService conversionService)
			throws Exception {
		Resource source = mock(Resource.class);
		given(source.getInputStream()).willThrow(IOException.class);
		given(source.getDescription()).willReturn("myresource");
		assertThatExceptionOfType(ConversionFailedException.class)
			.isThrownBy(() -> conversionService.convert(source, byte[].class))
			.havingCause()
			.isInstanceOf(IllegalStateException.class)
			.withMessageContaining("Unable to read from myresource");
	}

	static Stream<? extends Arguments> conversionServices() {
		return ConversionServiceArguments
			.with((service) -> service.addConverter(new InputStreamSourceToByteArrayConverter()));
	}

	private static class TestOrigin implements Origin {

		private final String string;

		TestOrigin(String string) {
			this.string = string;
		}

		@Override
		public String toString() {
			return this.string;
		}

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free