Home / Class/ StructuredLoggingJsonPropertiesTests Class — spring-boot Architecture

StructuredLoggingJsonPropertiesTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesTests.java lines 48–258

class StructuredLoggingJsonPropertiesTests {

	@Test
	void getWhenHasNoStackTracePropertiesBindsFromEnvironment() {
		MockEnvironment environment = new MockEnvironment();
		setupJsonProperties(environment);
		StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
		assertThat(properties).isEqualTo(new StructuredLoggingJsonProperties(Set.of("a", "b"), Set.of("c", "d"),
				Map.of("e", "f"), Map.of("g", "h"), null, null, Set.of(TestCustomizer.class)));
	}

	@Test
	void getWhenHasStackTracePropertiesBindsFromEnvironment() {
		MockEnvironment environment = new MockEnvironment();
		setupJsonProperties(environment);
		environment.setProperty("logging.structured.json.stacktrace.printer", "standard");
		environment.setProperty("logging.structured.json.stacktrace.root", "first");
		environment.setProperty("logging.structured.json.stacktrace.max-length", "1024");
		environment.setProperty("logging.structured.json.stacktrace.max-throwable-depth", "5");
		environment.setProperty("logging.structured.json.stacktrace.include-common-frames", "true");
		environment.setProperty("logging.structured.json.stacktrace.include-hashes", "true");
		StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
		assertThat(properties).isNotNull();
		assertThat(properties.stackTrace())
			.isEqualTo(new StructuredLoggingJsonProperties.StackTrace("standard", Root.FIRST, 1024, 5, true, true));
	}

	private void setupJsonProperties(MockEnvironment environment) {
		environment.setProperty("logging.structured.json.include", "a,b");
		environment.setProperty("logging.structured.json.exclude", "c,d");
		environment.setProperty("logging.structured.json.rename.e", "f");
		environment.setProperty("logging.structured.json.add.g", "h");
		environment.setProperty("logging.structured.json.customizer", TestCustomizer.class.getName());
	}

	@Test
	void getWhenNoBoundPropertiesReturnsNull() {
		MockEnvironment environment = new MockEnvironment();
		StructuredLoggingJsonProperties.get(environment);
	}

	@Test
	void shouldRegisterRuntimeHints() throws Exception {
		RuntimeHints hints = new RuntimeHints();
		new StructuredLoggingJsonPropertiesRuntimeHints().registerHints(hints, getClass().getClassLoader());
		assertThat(RuntimeHintsPredicates.reflection().onType(StructuredLoggingJsonProperties.class)).accepts(hints);
		assertThat(
				RuntimeHintsPredicates.reflection()
					.onConstructorInvocation(StructuredLoggingJsonProperties.class.getDeclaredConstructor(Set.class,
							Set.class, Map.class, Map.class, StackTrace.class, Context.class, Set.class)))
			.accepts(hints);
		assertThat(RuntimeHintsPredicates.reflection()
			.onConstructorInvocation(StackTrace.class.getDeclaredConstructor(String.class, Root.class, Integer.class,
					Integer.class, Boolean.class, Boolean.class)))
			.accepts(hints);
		assertThat(RuntimeHintsPredicates.reflection()
			.onConstructorInvocation(Context.class.getDeclaredConstructor(boolean.class, String.class))).accepts(hints);
	}

	@Test
	void structuredLoggingJsonPropertiesRuntimeHintsIsRegistered() {
		assertThat(AotServices.factories().load(RuntimeHintsRegistrar.class))
			.anyMatch(StructuredLoggingJsonPropertiesRuntimeHints.class::isInstance);
	}

	@Nested
	class StackTraceTests {

		@Test
		void createPrinterWhenEmptyReturnsNull() {
			StackTrace properties = new StackTrace(null, null, null, null, null, null);
			assertThat(properties.createPrinter()).isNull();
		}

		@Test
		void createPrinterWhenNoPrinterAndNotEmptyReturnsStandard() {
			StackTrace properties = new StackTrace(null, Root.LAST, null, null, null, null);
			assertThat(properties.createPrinter()).isInstanceOf(StandardStackTracePrinter.class);
		}

		@Test
		void createPrinterWhenLoggingSystemReturnsNull() {
			StackTrace properties = new StackTrace("logging-system", null, null, null, null, null);
			assertThat(properties.createPrinter()).isNull();
		}

		@Test
		void createPrinterWhenLoggingSystemRelaxedReturnsNull() {
			StackTrace properties = new StackTrace("LoggingSystem", null, null, null, null, null);
			assertThat(properties.createPrinter()).isNull();
		}

		@Test
		void createPrinterWhenStandardReturnsStandardPrinter() {
			StackTrace properties = new StackTrace("standard", null, null, null, null, null);
			assertThat(properties.createPrinter()).isInstanceOf(StandardStackTracePrinter.class);
		}

		@Test
		void createPrinterWhenStandardRelaxedReturnsStandardPrinter() {
			StackTrace properties = new StackTrace("STANDARD", null, null, null, null, null);
			assertThat(properties.createPrinter()).isInstanceOf(StandardStackTracePrinter.class);
		}

		@Test
		void createPrinterWhenStandardAppliesCustomizations() {
			Exception exception = TestException.create();
			StackTrace properties = new StackTrace(null, Root.FIRST, 300, 2, true, false);
			StandardStackTracePrinter printer = (StandardStackTracePrinter) properties.createPrinter();
			assertThat(printer).isNotNull();
			printer = printer.withLineSeparator("\n");
			String actual = TestException.withoutLineNumbers(printer.printStackTraceToString(exception));
			assertThat(actual).isEqualToNormalizingNewlines("""
					java.lang.RuntimeException: root
						at org.springframework.boot.logging.TestException.createTestException(TestException.java:NN)
						at org.springframework.boot.logging.TestException$CreatorThread.run(TestException.java:NN)
					Wrapped by: java.lang.RuntimeException: cause
						at org.springframework.boot.log...""");
		}

		@Test
		void createPrinterWhenStandardWithHashesPrintsHash() {
			Exception exception = TestException.create();
			StackTrace properties = new StackTrace(null, null, null, null, null, true);
			StackTracePrinter printer = properties.createPrinter();
			assertThat(printer).isNotNull();
			String actual = printer.printStackTraceToString(exception);
			assertThat(actual).containsPattern("<#[0-9a-z]{8}>");
		}

		@Test
		void createPrinterWhenClassNameCreatesPrinter() {
			Exception exception = TestException.create();
			StackTrace properties = new StackTrace(TestStackTracePrinter.class.getName(), null, null, null, true, null);
			StackTracePrinter printer = properties.createPrinter();
			assertThat(printer).isNotNull();
			assertThat(printer.printStackTraceToString(exception)).isEqualTo("java.lang.RuntimeException: exception");
		}

		@Test
		void createPrinterWhenClassNameInjectsConfiguredPrinter() {
			Exception exception = TestException.create();
			StackTrace properties = new StackTrace(TestStackTracePrinterCustomized.class.getName(), Root.FIRST, 300, 2,
					true, null);
			StackTracePrinter printer = properties.createPrinter();
			assertThat(printer).isNotNull();
			String actual = TestException.withoutLineNumbers(printer.printStackTraceToString(exception));
			assertThat(actual).isEqualTo("RuntimeExceptionroot!	at org.springfr...");
		}

		@Test
		void hasCustomPrinterShouldReturnFalseWhenPrinterIsEmpty() {
			StackTrace stackTrace = new StackTrace("", null, null, null, null, null);
			assertThat(stackTrace.hasCustomPrinter()).isFalse();
		}

		@Test
		void hasCustomPrinterShouldReturnFalseWhenPrinterHasLoggingSystem() {
			StackTrace stackTrace = new StackTrace("loggingsystem", null, null, null, null, null);
			assertThat(stackTrace.hasCustomPrinter()).isFalse();
		}

		@Test
		void hasCustomPrinterShouldReturnFalseWhenPrinterHasStandard() {
			StackTrace stackTrace = new StackTrace("standard", null, null, null, null, null);
			assertThat(stackTrace.hasCustomPrinter()).isFalse();
		}

		@Test
		void hasCustomPrinterShouldReturnTrueWhenPrinterHasCustom() {
			StackTrace stackTrace = new StackTrace("custom-printer", null, null, null, null, null);
			assertThat(stackTrace.hasCustomPrinter()).isTrue();
		}

	}

	static class TestCustomizer implements StructuredLoggingJsonMembersCustomizer<String> {

		@Override
		public void customize(Members<String> members) {
		}

	}

	static class TestStackTracePrinter implements StackTracePrinter {

		@Override
		public void printStackTrace(Throwable throwable, Appendable out) throws IOException {
			out.append(throwable.toString());
		}

	}

	static class TestStackTracePrinterCustomized implements StackTracePrinter {

		private final StandardStackTracePrinter printer;

		TestStackTracePrinterCustomized(StandardStackTracePrinter printer) {
			this.printer = printer.withMaximumLength(40)
				.withLineSeparator("!")
				.withFormatter((throwable) -> ClassUtils.getShortName(throwable.getClass()) + throwable.getMessage());
		}

		@Override
		public void printStackTrace(Throwable throwable, Appendable out) throws IOException {
			this.printer.printStackTrace(throwable, out);
		}

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free