Home / Class/ SpringProfileArbiterTests Class — spring-boot Architecture

SpringProfileArbiterTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringProfileArbiterTests.java lines 51–210

@ExtendWith(OutputCaptureExtension.class)
@ClassPathExclusions("logback-*.jar")
@ConfigureClasspathToPreferLog4j2
class SpringProfileArbiterTests {

	private CapturedOutput output;

	private TestLog4J2LoggingSystem loggingSystem;

	private final MockEnvironment environment = new MockEnvironment();

	private final LoggingInitializationContext initializationContext = new LoggingInitializationContext(
			this.environment);

	private Logger logger;

	@BeforeEach
	void setup(CapturedOutput output, TestInfo testInfo) {
		this.output = output;
		this.loggingSystem = new TestLog4J2LoggingSystem(testInfo.getDisplayName());
		this.logger = this.loggingSystem.getLoggerContext().getLogger(getClass().getName());
		cleanUpPropertySources();
	}

	@AfterEach
	void cleanUp() {
		this.loggingSystem.cleanUp();
		cleanUpPropertySources();
		for (LoggingSystemProperty property : LoggingSystemProperty.values()) {
			System.getProperties().remove(property.getEnvironmentVariableName());
		}
	}

	@SuppressWarnings("unchecked")
	private void cleanUpPropertySources() { // https://issues.apache.org/jira/browse/LOG4J2-3618
		PropertiesUtil properties = PropertiesUtil.getProperties();
		Object environment = ReflectionTestUtils.getField(properties, "environment");
		assertThat(environment).isNotNull();
		Set<PropertySource> sources = (Set<PropertySource>) ReflectionTestUtils.getField(environment, "sources");
		assertThat(sources).isNotNull();
		sources.removeIf((candidate) -> candidate instanceof SpringEnvironmentPropertySource
				|| candidate instanceof SpringBootPropertySource);
	}

	@Test
	@WithProductionProfileXmlResource
	void profileActive() {
		this.environment.setActiveProfiles("production");
		initialize("production-profile.xml");
		this.logger.trace("Hello");
		assertThat(this.output).contains("Hello");
	}

	@Test
	@WithMultiProfileNamesXmlResource
	void multipleNamesFirstProfileActive() {
		this.environment.setActiveProfiles("production");
		initialize("multi-profile-names.xml");
		this.logger.trace("Hello");
		assertThat(this.output).contains("Hello");
	}

	@Test
	@WithMultiProfileNamesXmlResource
	void multipleNamesSecondProfileActive() {
		this.environment.setActiveProfiles("test");
		initialize("multi-profile-names.xml");
		this.logger.trace("Hello");
		assertThat(this.output).contains("Hello");
	}

	@Test
	@WithProductionProfileXmlResource
	void profileNotActive() {
		initialize("production-profile.xml");
		this.logger.trace("Hello");
		assertThat(this.output).doesNotContain("Hello");
	}

	@Test
	@WithProfileExpressionXmlResource
	void profileExpressionMatchFirst() {
		this.environment.setActiveProfiles("production");
		initialize("profile-expression.xml");
		this.logger.trace("Hello");
		assertThat(this.output).contains("Hello");
	}

	@Test
	@WithProfileExpressionXmlResource
	void profileExpressionMatchSecond() {
		this.environment.setActiveProfiles("test");
		initialize("profile-expression.xml");
		this.logger.trace("Hello");
		assertThat(this.output).contains("Hello");
	}

	@Test
	@WithProfileExpressionXmlResource
	void profileExpressionNoMatch() {
		this.environment.setActiveProfiles("development");
		initialize("profile-expression.xml");
		this.logger.trace("Hello");
		assertThat(this.output).doesNotContain("Hello");
	}

	private void initialize(String config) {
		this.environment.setProperty("logging.log4j2.config.override", "classpath:" + config);
		this.loggingSystem.initialize(this.initializationContext, null, null);
	}

	@Target(ElementType.METHOD)
	@Retention(RetentionPolicy.RUNTIME)
	@WithResource(name = "multi-profile-names.xml", content = """
			<?xml version="1.0" encoding="UTF-8"?>
			<Configuration>
			    <Loggers>
				    <SpringProfile name="production, test">
						<Logger name="org.springframework.boot.logging.log4j2" level="TRACE" />
					</SpringProfile>
				</Loggers>
			</Configuration>
			""")
	private @interface WithMultiProfileNamesXmlResource {

	}

	@Target(ElementType.METHOD)
	@Retention(RetentionPolicy.RUNTIME)
	@WithResource(name = "profile-expression.xml", content = """
			<?xml version="1.0" encoding="UTF-8"?>
			<Configuration>
			    <Loggers>
				    <SpringProfile name="production | test">
						<Logger name="org.springframework.boot.logging.log4j2" level="TRACE" />
					</SpringProfile>
				</Loggers>
			</Configuration>
			""")
	private @interface WithProfileExpressionXmlResource {

	}

	@Target(ElementType.METHOD)
	@Retention(RetentionPolicy.RUNTIME)
	@WithResource(name = "production-profile.xml", content = """
			<?xml version="1.0" encoding="UTF-8"?>
			<Configuration>
			    <Loggers>
				    <SpringProfile name="production">
						<Logger name="org.springframework.boot.logging.log4j2" level="TRACE" />
					</SpringProfile>
				</Loggers>
			</Configuration>
			""")
	private @interface WithProductionProfileXmlResource {

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free