Home / Class/ ConfigurationPropertySourcesTests Class — spring-boot Architecture

ConfigurationPropertySourcesTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java lines 47–236

class ConfigurationPropertySourcesTests {

	@Test
	void attachShouldAddAdapterAtBeginning() {
		ConfigurableEnvironment environment = new StandardEnvironment();
		MutablePropertySources sources = environment.getPropertySources();
		sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
		sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568")));
		int size = sources.size();
		ConfigurationPropertySources.attach(environment);
		assertThat(sources).hasSize(size + 1);
		PropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
		assertThat(resolver.getProperty("server.port")).isEqualTo("1234");
	}

	@Test
	void attachShouldReattachInMergedSetup() {
		ConfigurableEnvironment parent = new StandardEnvironment();
		ConfigurationPropertySources.attach(parent);
		ConfigurableEnvironment child = new StandardEnvironment();
		child.merge(parent);
		child.getPropertySources()
			.addLast(new MapPropertySource("config", Collections.singletonMap("my.example_property", "1234")));
		ConfigurationPropertySources.attach(child);
		assertThat(child.getProperty("my.example-property")).isEqualTo("1234");
	}

	@Test
	void attachWhenAlreadyAttachedWithSameSourcesShouldReturnExistingInstance() {
		ConfigurableEnvironment environment = new StandardEnvironment();
		MutablePropertySources sources = environment.getPropertySources();
		sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
		sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568")));
		ConfigurationPropertySources.attach(environment);
		Iterable<ConfigurationPropertySource> first = ConfigurationPropertySources.get(environment);
		ConfigurationPropertySources.attach(environment);
		Iterable<ConfigurationPropertySource> second = ConfigurationPropertySources.get(environment);
		assertThat(first).isSameAs(second);
	}

	@Test
	void getWhenNotAttachedShouldReturnAdapted() {
		ConfigurableEnvironment environment = new StandardEnvironment();
		assertThat(ConfigurationPropertySources.get(environment)).isNotEmpty();
	}

	@Test
	void getWhenAttachedShouldReturnAttached() {
		ConfigurableEnvironment environment = new StandardEnvironment();
		MutablePropertySources sources = environment.getPropertySources();
		sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
		int expectedSize = sources.size();
		ConfigurationPropertySources.attach(environment);
		assertThat(ConfigurationPropertySources.get(environment)).hasSize(expectedSize);
	}

	@Test
	void environmentPropertyExpansionShouldWorkWhenAttached() {
		ConfigurableEnvironment environment = new StandardEnvironment();
		Map<String, Object> source = new LinkedHashMap<>();
		source.put("fooBar", "Spring ${barBaz} ${bar-baz}");
		source.put("barBaz", "Boot");
		PropertySource<?> propertySource = new MapPropertySource("test", source);
		environment.getPropertySources().addFirst(propertySource);
		ConfigurationPropertySources.attach(environment);
		assertThat(environment.getProperty("foo-bar")).isEqualTo("Spring Boot Boot");
	}

	@Test
	void fromPropertySourceShouldReturnSpringConfigurationPropertySource() {
		PropertySource<?> source = new MapPropertySource("foo", Collections.singletonMap("foo", "bar"));
		ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySources.from(source)
			.iterator()
			.next();
		assertThat(configurationPropertySource).isInstanceOf(SpringConfigurationPropertySource.class);
	}

	@Test
	void fromPropertySourceShouldFlattenPropertySources() {
		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")));
		Iterable<ConfigurationPropertySource> configurationSources = ConfigurationPropertySources.from(sources);
		assertThat(configurationSources.iterator()).toIterable().hasSize(5);
	}

	@Test // gh-20625
	void environmentPropertyAccessWhenImmutableShouldBePerformant() {
		long baseline = testPropertySourcePerformance(false);
		long immutable = testPropertySourcePerformance(true);
		assertThat(immutable).isLessThan(baseline / 2);
	}

	@Test // gh-20625
	void environmentPropertyAccessWhenMutableWithCacheShouldBePerformant() {
		StandardEnvironment environment = createPerformanceTestEnvironment(false);
		long uncached = testPropertySourcePerformance(environment);
		ConfigurationPropertyCaching.get(environment).enable();
		long cached = testPropertySourcePerformance(environment);
		assertThat(cached).isLessThan(uncached / 2);
	}

	@Test // gh-20625
	@Disabled("for manual testing")
	void environmentPropertyAccessWhenMutableShouldBeTolerable() {
		assertThat(testPropertySourcePerformance(false)).isLessThan(5000);
	}

	@Test // gh-21416
	void descendantOfPropertyAccessWhenMutableWithCacheShouldBePerformant() {
		Function<StandardEnvironment, Long> descendantOfPerformance = (environment) -> {
			Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
			ConfigurationPropertyName missing = ConfigurationPropertyName.of("missing");
			long start = System.nanoTime();
			for (int i = 0; i < 1000; i++) {
				for (ConfigurationPropertySource source : sources) {
					assertThat(source.containsDescendantOf(missing)).isEqualTo(ConfigurationPropertyState.ABSENT);
				}
			}
			return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
		};
		StandardEnvironment environment = createPerformanceTestEnvironment(false);
		long baseline = descendantOfPerformance.apply(environment);
		ConfigurationPropertyCaching.get(environment).enable();
		long cached = descendantOfPerformance.apply(environment);
		assertThat(cached).isLessThan(baseline / 2);
	}

	private long testPropertySourcePerformance(boolean immutable) {
		StandardEnvironment environment = createPerformanceTestEnvironment(immutable);
		return testPropertySourcePerformance(environment);
	}

	private StandardEnvironment createPerformanceTestEnvironment(boolean immutable) {
		StandardEnvironment environment = new StandardEnvironment();
		MutablePropertySources propertySources = environment.getPropertySources();
		for (int i = 0; i < 100; i++) {
			propertySources.addLast(new TestPropertySource(i, immutable));
		}
		ConfigurationPropertySources.attach(environment);
		return environment;
	}

	private long testPropertySourcePerformance(StandardEnvironment environment) {
		long start = System.nanoTime();
		for (int i = 0; i < 1000; i++) {
			environment.getProperty("missing" + i);
		}
		long total = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
		assertThat(environment.getProperty("test-10-property-80")).isEqualTo("test-10-property-80-value");
		return total;
	}

	static class TestPropertySource extends MapPropertySource implements PropertySourceInfo {

		private final boolean immutable;

		TestPropertySource(int index, boolean immutable) {
			super("test-" + index, createProperties(index));
			this.immutable = immutable;
		}

		private static Map<String, Object> createProperties(int index) {
			Map<String, Object> map = new LinkedHashMap<>();
			for (int i = 0; i < 1000; i++) {
				String name = "test-" + index + "-property-" + i;
				String value = name + "-value";
				map.put(name, value);
			}
			return map;
		}

		@Override
		public boolean isImmutable() {
			return this.immutable;
		}

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free