Home / Class/ ConfigDataEnvironmentContributorPlaceholdersResolverTests Class — spring-boot Architecture

ConfigDataEnvironmentContributorPlaceholdersResolverTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorPlaceholdersResolverTests.java lines 47–171

class ConfigDataEnvironmentContributorPlaceholdersResolverTests {

	private final ConversionService conversionService = DefaultConversionService.getSharedInstance();

	@Test
	void resolvePlaceholdersWhenNotStringReturnsResolved() {
		ConfigDataEnvironmentContributorPlaceholdersResolver resolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(
				Collections.emptyList(), null, null, false, this.conversionService);
		assertThat(resolver.resolvePlaceholders(123)).isEqualTo(123);
	}

	@Test
	void resolvePlaceholdersWhenNotFoundReturnsOriginal() {
		ConfigDataEnvironmentContributorPlaceholdersResolver resolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(
				Collections.emptyList(), null, null, false, this.conversionService);
		assertThat(resolver.resolvePlaceholders("${test}")).isEqualTo("${test}");
	}

	@Test
	void resolvePlaceholdersWhenFoundReturnsFirstMatch() {
		List<ConfigDataEnvironmentContributor> contributors = new ArrayList<>();
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s1", "nope", "t1"), true,
				this.conversionService));
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s2", "test", "t2"), true,
				this.conversionService));
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s3", "test", "t3"), true,
				this.conversionService));
		ConfigDataEnvironmentContributorPlaceholdersResolver resolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(
				contributors, null, null, true, this.conversionService);
		assertThat(resolver.resolvePlaceholders("${test}")).isEqualTo("t2");
	}

	@Test
	void shouldUseConversionService() {
		GenericConversionService conversionService = new GenericConversionService();
		conversionService.addConverter(CustomValue.class, String.class, (input) -> "custom-value");
		List<ConfigDataEnvironmentContributor> contributors = new ArrayList<>();
		contributors.add(new TestConfigDataEnvironmentContributor(
				new TestPropertySource("s1", Map.of("test", new CustomValue())), true, conversionService));
		ConfigDataEnvironmentContributorPlaceholdersResolver resolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(
				contributors, null, null, true, conversionService);
		assertThat(resolver.resolvePlaceholders("${test}")).isEqualTo("custom-value");
	}

	@Test
	void resolvePlaceholdersWhenFoundInInactiveThrowsException() {
		List<ConfigDataEnvironmentContributor> contributors = new ArrayList<>();
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s1", "nope", "t1"), true,
				this.conversionService));
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s2", "test", "t2"), true,
				this.conversionService));
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s3", "test", "t3"), false,
				this.conversionService));
		ConfigDataEnvironmentContributorPlaceholdersResolver resolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(
				contributors, null, null, true, this.conversionService);
		assertThatExceptionOfType(InactiveConfigDataAccessException.class)
			.isThrownBy(() -> resolver.resolvePlaceholders("${test}"))
			.satisfies(propertyNameAndOriginOf("test", "s3"));
	}

	@Test
	void resolvePlaceholderWhenFoundInInactiveAndIgnoringReturnsResolved() {
		List<ConfigDataEnvironmentContributor> contributors = new ArrayList<>();
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s1", "nope", "t1"), true,
				this.conversionService));
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s2", "test", "t2"), true,
				this.conversionService));
		contributors.add(new TestConfigDataEnvironmentContributor(new TestPropertySource("s3", "test", "t3"), false,
				this.conversionService));
		ConfigDataEnvironmentContributorPlaceholdersResolver resolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(
				contributors, null, null, false, this.conversionService);
		assertThat(resolver.resolvePlaceholders("${test}")).isEqualTo("t2");
	}

	private Consumer<InactiveConfigDataAccessException> propertyNameAndOriginOf(String propertyName, String origin) {
		return (ex) -> {
			assertThat(ex.getPropertyName()).isEqualTo(propertyName);
			PropertySourceOrigin actualOrigin = (PropertySourceOrigin) (ex.getOrigin());
			assertThat(actualOrigin).isNotNull();
			assertThat(actualOrigin.getPropertySource().getName()).isEqualTo(origin);
		};
	}

	static class TestPropertySource extends MapPropertySource implements OriginLookup<String> {

		TestPropertySource(String name, String key, String value) {
			this(name, Collections.singletonMap(key, value));
		}

		TestPropertySource(String name, Map<String, Object> source) {
			super(name, source);
		}

		@Override
		public @Nullable Origin getOrigin(String key) {
			if (getSource().containsKey(key)) {
				return new PropertySourceOrigin(this, key);
			}
			return null;
		}

	}

	static class TestConfigDataEnvironmentContributor extends ConfigDataEnvironmentContributor {

		private final boolean active;

		protected TestConfigDataEnvironmentContributor(PropertySource<?> propertySource, boolean active,
				ConversionService conversionService) {
			super(Kind.ROOT, null, null, false, propertySource, null, null, null, null, conversionService);
			this.active = active;
		}

		@Override
		boolean isActive(@Nullable ConfigDataActivationContext activationContext) {
			return this.active;
		}

	}

	private static final class CustomValue {

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free