Home / Class/ ConfigDataEnvironmentPostProcessorTests Class — spring-boot Architecture

ConfigDataEnvironmentPostProcessorTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java lines 49–166

class ConfigDataEnvironmentPostProcessorTests {

	private final TestApplicationEnvironment environment = new TestApplicationEnvironment();

	private final SpringApplication application = new SpringApplication();

	private @Nullable ConfigDataEnvironment configDataEnvironment;

	private @Nullable ConfigDataEnvironmentPostProcessor postProcessor;

	@Test
	void postProcessEnvironmentWhenNoLoaderCreatesDefaultLoaderInstance() {
		setupMocksAndSpies();
		assertThat(this.configDataEnvironment).isNotNull();
		assertThat(this.postProcessor).isNotNull();
		willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
		this.postProcessor.postProcessEnvironment(this.environment, this.application);
		then(this.postProcessor).should()
			.getConfigDataEnvironment(any(),
					assertArg((resourceLoader) -> assertThat(resourceLoader).isInstanceOf(DefaultResourceLoader.class)),
					any());
		then(this.configDataEnvironment).should().processAndApply();
	}

	@Test
	void postProcessEnvironmentWhenCustomLoaderUsesSpecifiedLoaderInstance() {
		setupMocksAndSpies();
		assertThat(this.configDataEnvironment).isNotNull();
		assertThat(this.postProcessor).isNotNull();
		ResourceLoader resourceLoader = mock(ResourceLoader.class);
		this.application.setResourceLoader(resourceLoader);
		willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
		this.postProcessor.postProcessEnvironment(this.environment, this.application);
		then(this.postProcessor).should()
			.getConfigDataEnvironment(any(),
					assertArg((resourceLoaderB) -> assertThat(resourceLoaderB).isSameAs(resourceLoader)), any());
		then(this.configDataEnvironment).should().processAndApply();
	}

	@Test
	void postProcessEnvironmentWhenHasAdditionalProfilesOnSpringApplicationUsesAdditionalProfiles() {
		setupMocksAndSpies();
		assertThat(this.configDataEnvironment).isNotNull();
		assertThat(this.postProcessor).isNotNull();
		this.application.setAdditionalProfiles("dev");
		willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
		this.postProcessor.postProcessEnvironment(this.environment, this.application);
		then(this.postProcessor).should()
			.getConfigDataEnvironment(any(), any(),
					assertArg((additionalProperties) -> assertThat(additionalProperties).containsExactly("dev")));
		then(this.configDataEnvironment).should().processAndApply();
	}

	@Test
	void postProcessEnvironmentWhenNoActiveProfiles() {
		setupMocksAndSpies();
		assertThat(this.configDataEnvironment).isNotNull();
		assertThat(this.postProcessor).isNotNull();
		willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
		this.postProcessor.postProcessEnvironment(this.environment, this.application);
		then(this.postProcessor).should().getConfigDataEnvironment(any(), any(ResourceLoader.class), any());
		then(this.configDataEnvironment).should().processAndApply();
		assertThat(this.environment.getActiveProfiles()).isEmpty();
	}

	@Test
	@WithResource(name = "application.properties", content = "property=value")
	@WithResource(name = "application-dev.properties", content = "property=dev-value")
	void applyToAppliesPostProcessing() {
		int before = this.environment.getPropertySources().size();
		TestConfigDataEnvironmentUpdateListener listener = new TestConfigDataEnvironmentUpdateListener();
		ConfigDataEnvironmentPostProcessor.applyTo(this.environment, null, null, Collections.singleton("dev"),
				listener);
		assertThat(this.environment.getPropertySources()).hasSizeGreaterThan(before);
		assertThat(this.environment.getActiveProfiles()).containsExactly("dev");
		assertThat(listener.getAddedPropertySources()).isNotEmpty();
		Profiles profiles = listener.getProfiles();
		assertThat(profiles).isNotNull();
		assertThat(profiles.getActive()).containsExactly("dev");
		assertThat(listener.getAddedPropertySources().stream().anyMatch((added) -> hasDevProfile(added.getResource())))
			.isTrue();
	}

	@Test
	@WithResource(name = "application.properties", content = """
			spring.profiles.active=dev
			property=value
			#---
			spring.config.activate.on-profile=dev
			property=dev-value1
			""")
	@WithResource(name = "application-dev.properties", content = "property=dev-value2")
	void applyToCanOverrideConfigDataOptions() {
		ConfigDataEnvironmentUpdateListener listener = new ConfigDataEnvironmentUpdateListener() {

			@Override
			public Options onConfigDataOptions(ConfigData configData, PropertySource<?> propertySource,
					Options options) {
				return options.with(ConfigData.Option.IGNORE_PROFILES);
			}

		};
		ConfigDataEnvironmentPostProcessor.applyTo(this.environment, null, null, Collections.emptyList(), listener);
		assertThat(this.environment.getProperty("property")).isEqualTo("value");
		assertThat(this.environment.getActiveProfiles()).isEmpty();
	}

	private void setupMocksAndSpies() {
		this.configDataEnvironment = mock(ConfigDataEnvironment.class);
		this.postProcessor = spy(new ConfigDataEnvironmentPostProcessor(Supplier::get, new DefaultBootstrapContext()));
	}

	private boolean hasDevProfile(@Nullable ConfigDataResource resource) {
		return (resource instanceof StandardConfigDataResource standardResource)
				&& "dev".equals(standardResource.getProfile());
	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free