Home / Class/ SpringEnvironmentLookupTests Class — spring-boot Architecture

SpringEnvironmentLookupTests Class — spring-boot Architecture

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

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java lines 36–84

class SpringEnvironmentLookupTests {

	private MockEnvironment environment;

	private LoggerContext loggerContext;

	@BeforeEach
	void setup() {
		this.environment = new MockEnvironment();
		this.loggerContext = (LoggerContext) LogManager.getContext(false);
		this.loggerContext.putObject(Log4J2LoggingSystem.ENVIRONMENT_KEY, this.environment);
	}

	@AfterEach
	void cleanup() {
		this.loggerContext.removeObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
	}

	@Test
	void lookupWhenFoundInEnvironmentReturnsValue() {
		this.environment.setProperty("test", "test");
		Interpolator lookup = createLookup(this.loggerContext);
		assertThat(lookup.lookup("spring:test")).isEqualTo("test");
	}

	@Test
	void lookupWhenNotFoundInEnvironmentReturnsNull() {
		Interpolator lookup = createLookup(this.loggerContext);
		assertThat(lookup.lookup("spring:test")).isNull();
	}

	@Test
	void lookupWhenNoSpringEnvironmentThrowsException() {
		this.loggerContext.removeObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
		Interpolator lookup = createLookup(this.loggerContext);
		assertThatIllegalStateException().isThrownBy(() -> lookup.lookup("spring:test"))
			.withMessage("Unable to obtain Spring Environment from LoggerContext. "
					+ "This can happen if your log4j2 configuration filename does not end with '-spring' "
					+ "(for example using 'log4j2.xml' instead of 'log4j2-spring.xml')");
	}

	private Interpolator createLookup(LoggerContext context) {
		Interpolator lookup = new Interpolator();
		lookup.setConfiguration(context.getConfiguration());
		lookup.setLoggerContext(context);
		return lookup;
	}

}

Analyze Your Own Codebase

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

Try Supermodel Free