Home / Class/ WritableJsonTests Class — spring-boot Architecture

WritableJsonTests Class — spring-boot Architecture

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

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/json/WritableJsonTests.java lines 41–157

class WritableJsonTests {

	@TempDir
	@SuppressWarnings("NullAway.Init")
	File temp;

	@Test
	void toJsonStringReturnsString() {
		WritableJson writable = (out) -> out.append("{}");
		assertThat(writable.toJsonString()).isEqualTo("{}");
	}

	@Test
	void toJsonStringWhenIOExceptionIsThrownThrowsUncheckedIOException() {
		WritableJson writable = (out) -> {
			throw new IOException("bad");
		};
		assertThatExceptionOfType(UncheckedIOException.class).isThrownBy(() -> writable.toJsonString())
			.havingCause()
			.withMessage("bad");
	}

	@Test
	void toByteArrayReturnsByteArray() {
		WritableJson writable = (out) -> out.append("{}");
		assertThat(writable.toByteArray()).isEqualTo("{}".getBytes());
	}

	@Test
	void toResourceWritesJson() throws Exception {
		File file = new File(this.temp, "out.json");
		WritableJson writable = (out) -> out.append("{}");
		writable.toResource(new FileSystemResource(file));
		assertThat(file).content().isEqualTo("{}");
	}

	@Test
	void toResourceWithCharsetWritesJson() throws Exception {
		File file = new File(this.temp, "out.json");
		WritableJson writable = (out) -> out.append("{}");
		writable.toResource(new FileSystemResource(file), StandardCharsets.ISO_8859_1);
		assertThat(file).content(StandardCharsets.ISO_8859_1).isEqualTo("{}");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void toResourceWithCharsetWhenOutIsNullThrowsException() {
		WritableJson writable = (out) -> out.append("{}");
		assertThatIllegalArgumentException().isThrownBy(() -> writable.toResource(null, StandardCharsets.UTF_8))
			.withMessage("'out' must not be null");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void toResourceWithCharsetWhenCharsetIsNullThrowsException() {
		File file = new File(this.temp, "out.json");
		WritableJson writable = (out) -> out.append("{}");
		assertThatIllegalArgumentException().isThrownBy(() -> writable.toResource(new FileSystemResource(file), null))
			.withMessage("'charset' must not be null");
	}

	@Test
	void toOutputStreamWritesJson() throws Exception {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		WritableJson writable = (out) -> out.append("{}");
		writable.toOutputStream(outputStream);
		assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("{}");
	}

	@Test
	void toOutputStreamWithCharsetWritesJson() throws Exception {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		WritableJson writable = (out) -> out.append("{}");
		writable.toOutputStream(outputStream, StandardCharsets.ISO_8859_1);
		assertThat(outputStream.toString(StandardCharsets.ISO_8859_1)).isEqualTo("{}");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void toOutputStreamWithCharsetWhenOutIsNullThrowsException() {
		WritableJson writable = (out) -> out.append("{}");
		assertThatIllegalArgumentException().isThrownBy(() -> writable.toOutputStream(null, StandardCharsets.UTF_8))
			.withMessage("'out' must not be null");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void toOutputStreamWithCharsetWhenCharsetIsNullThrowsException() {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		WritableJson writable = (out) -> out.append("{}");
		assertThatIllegalArgumentException().isThrownBy(() -> writable.toOutputStream(outputStream, null))
			.withMessage("'charset' must not be null");
	}

	@Test
	void toWriterWritesJson() throws Exception {
		StringWriter writer = new StringWriter();
		WritableJson writable = (out) -> out.append("{}");
		writable.toWriter(writer);
		assertThat(writer).hasToString("{}");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void toWriterWhenWriterIsNullThrowsException() {
		WritableJson writable = (out) -> out.append("{}");
		assertThatIllegalArgumentException().isThrownBy(() -> writable.toWriter(null))
			.withMessage("'out' must not be null");
	}

	@Test
	void ofReturnsInstanceWithSensibleToString() {
		WritableJson writable = WritableJson.of((out) -> out.append("{}"));
		assertThat(writable).hasToString("{}");
	}

}

Analyze Your Own Codebase

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

Try Supermodel Free