Home / Class/ SpringConfigurationPropertySourcesTests Class — spring-boot Architecture

SpringConfigurationPropertySourcesTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java lines 42–198

class SpringConfigurationPropertySourcesTests {

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void createWhenPropertySourcesIsNullShouldThrowException() {
		assertThatIllegalArgumentException().isThrownBy(() -> new SpringConfigurationPropertySources(null))
			.withMessageContaining("'sources' must not be null");
	}

	@Test
	void shouldAdaptPropertySource() {
		MutablePropertySources sources = new MutablePropertySources();
		sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
		Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
		ConfigurationPropertyName name = ConfigurationPropertyName.of("a");
		ConfigurationProperty configurationProperty = iterator.next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("b");
		assertThat(iterator.hasNext()).isFalse();
	}

	@Test
	void shouldAdaptSystemEnvironmentPropertySource() {
		MutablePropertySources sources = new MutablePropertySources();
		sources.addLast(new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
				Collections.singletonMap("SERVER_PORT", "1234")));
		Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
		ConfigurationPropertyName name = ConfigurationPropertyName.of("server.port");
		ConfigurationProperty configurationProperty = iterator.next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("1234");
		assertThat(iterator.hasNext()).isFalse();
	}

	@Test
	void shouldExtendedAdaptSystemEnvironmentPropertySource() {
		MutablePropertySources sources = new MutablePropertySources();
		sources.addLast(new SystemEnvironmentPropertySource(
				"test-" + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
				Collections.singletonMap("SERVER_PORT", "1234")));
		Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
		ConfigurationPropertyName name = ConfigurationPropertyName.of("server.port");
		ConfigurationProperty configurationProperty = iterator.next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("1234");
		assertThat(iterator.hasNext()).isFalse();
	}

	@Test
	void shouldNotAdaptSystemEnvironmentPropertyOverrideSource() {
		MutablePropertySources sources = new MutablePropertySources();
		sources
			.addLast(new SystemEnvironmentPropertySource("override", Collections.singletonMap("server.port", "1234")));
		Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
		ConfigurationPropertyName name = ConfigurationPropertyName.of("server.port");
		ConfigurationProperty configurationProperty = iterator.next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("1234");
		assertThat(iterator.hasNext()).isFalse();
	}

	@Test
	void shouldAdaptSystemEnvironmentPropertySourceWithUnderscoreValue() {
		MutablePropertySources sources = new MutablePropertySources();
		sources.addLast(new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
				Collections.singletonMap("_", "1234")));
		Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
		ConfigurationPropertyName name = ConfigurationPropertyName.of("bar");
		assertThat(iterator.next().getConfigurationProperty(name)).isNull();
		assertThat(iterator.hasNext()).isFalse();
	}

	@Test
	void shouldAdaptMultiplePropertySources() {
		MutablePropertySources sources = new MutablePropertySources();
		sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
		sources.addLast(new MapPropertySource("test1", Collections.singletonMap("server.po-rt", "4567")));
		sources.addLast(new MapPropertySource("test2", Collections.singletonMap("a", "b")));
		Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
		ConfigurationPropertyName name = ConfigurationPropertyName.of("server.port");
		ConfigurationProperty configurationProperty = iterator.next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("1234");
		configurationProperty = iterator.next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("4567");
		configurationProperty = iterator.next().getConfigurationProperty(ConfigurationPropertyName.of("a"));
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("b");
		assertThat(iterator.hasNext()).isFalse();
	}

	@Test
	void shouldFlattenEnvironment() {
		StandardEnvironment environment = new StandardEnvironment();
		environment.getPropertySources().addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", "bar")));
		environment.getPropertySources().addFirst(new MapPropertySource("far", Collections.singletonMap("far", "far")));
		MutablePropertySources sources = new MutablePropertySources();
		sources.addFirst(new PropertySource<Environment>("env", environment) {

			@Override
			public @Nullable String getProperty(String key) {
				return this.source.getProperty(key);
			}

		});
		sources.addLast(new MapPropertySource("baz", Collections.singletonMap("baz", "barf")));
		SpringConfigurationPropertySources configurationSources = new SpringConfigurationPropertySources(sources);
		assertThat(configurationSources.iterator()).toIterable().hasSize(5);
	}

	@Test
	void shouldTrackChanges() {
		MutablePropertySources sources = new MutablePropertySources();
		SpringConfigurationPropertySources configurationSources = new SpringConfigurationPropertySources(sources);
		assertThat(configurationSources.iterator()).toIterable().isEmpty();
		MapPropertySource source1 = new MapPropertySource("test1", Collections.singletonMap("a", "b"));
		sources.addLast(source1);
		assertThat(configurationSources.iterator()).toIterable().hasSize(1);
		MapPropertySource source2 = new MapPropertySource("test2", Collections.singletonMap("b", "c"));
		sources.addLast(source2);
		assertThat(configurationSources.iterator()).toIterable().hasSize(2);
	}

	@Test
	void shouldTrackWhenSourceHasIdenticalName() {
		MutablePropertySources sources = new MutablePropertySources();
		SpringConfigurationPropertySources configurationSources = new SpringConfigurationPropertySources(sources);
		ConfigurationPropertyName name = ConfigurationPropertyName.of("a");
		MapPropertySource source1 = new MapPropertySource("test", Collections.singletonMap("a", "s1"));
		sources.addLast(source1);
		ConfigurationProperty configurationProperty = configurationSources.iterator()
			.next()
			.getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("s1");
		MapPropertySource source2 = new MapPropertySource("test", Collections.singletonMap("a", "s2"));
		sources.remove("test");
		sources.addLast(source2);
		configurationProperty = configurationSources.iterator().next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isEqualTo("s2");
	}

	@Test // gh-21659
	void shouldAdaptRandomPropertySource() {
		MutablePropertySources sources = new MutablePropertySources();
		sources.addFirst(new RandomValuePropertySource());
		Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
		ConfigurationPropertyName name = ConfigurationPropertyName.of("random.int");
		ConfigurationProperty configurationProperty = iterator.next().getConfigurationProperty(name);
		assertThat(configurationProperty).isNotNull();
		assertThat(configurationProperty.getValue()).isNotNull();
		assertThat(iterator.hasNext()).isFalse();
	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free