Home / Class/ ConfigurationPropertiesBindHandlerAdvisorTests Class — spring-boot Architecture

ConfigurationPropertiesBindHandlerAdvisorTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindHandlerAdvisorTests.java lines 45–192

class ConfigurationPropertiesBindHandlerAdvisorTests {

	private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

	@AfterEach
	void cleanup() {
		this.context.close();
	}

	@Test
	void loadWithoutConfigurationPropertiesBindHandlerAdvisor() {
		load(WithoutConfigurationPropertiesBindHandlerAdvisor.class, "foo.bar.default.content-type=text/plain",
				"foo.bar.bindings.input.destination=d1", "foo.bar.bindings.input.content-type=text/xml",
				"foo.bar.bindings.output.destination=d2");
		BindingServiceProperties properties = this.context.getBean(BindingServiceProperties.class);
		BindingProperties input = properties.getBindings().get("input");
		assertThat(input).isNotNull();
		assertThat(input.getDestination()).isEqualTo("d1");
		assertThat(input.getContentType()).isEqualTo("text/xml");
		BindingProperties output = properties.getBindings().get("output");
		assertThat(output).isNotNull();
		assertThat(output.getDestination()).isEqualTo("d2");
		assertThat(output.getContentType()).isEqualTo("application/json");
	}

	@Test
	void loadWithConfigurationPropertiesBindHandlerAdvisor() {
		load(WithConfigurationPropertiesBindHandlerAdvisor.class, "foo.bar.default.content-type=text/plain",
				"foo.bar.bindings.input.destination=d1", "foo.bar.bindings.input.content-type=text/xml",
				"foo.bar.bindings.output.destination=d2");
		BindingServiceProperties properties = this.context.getBean(BindingServiceProperties.class);
		BindingProperties input = properties.getBindings().get("input");
		assertThat(input).isNotNull();
		assertThat(input.getDestination()).isEqualTo("d1");
		assertThat(input.getContentType()).isEqualTo("text/xml");
		BindingProperties output = properties.getBindings().get("output");
		assertThat(output).isNotNull();
		assertThat(output.getDestination()).isEqualTo("d2");
		assertThat(output.getContentType()).isEqualTo("text/plain");
	}

	private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
		return load(new Class<?>[] { configuration }, inlinedProperties);
	}

	private AnnotationConfigApplicationContext load(Class<?>[] configuration, String... inlinedProperties) {
		this.context.register(configuration);
		TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, inlinedProperties);
		this.context.refresh();
		return this.context;
	}

	@Configuration(proxyBeanMethods = false)
	@EnableConfigurationProperties(BindingServiceProperties.class)
	static class WithoutConfigurationPropertiesBindHandlerAdvisor {

	}

	@Configuration(proxyBeanMethods = false)
	@EnableConfigurationProperties(BindingServiceProperties.class)
	@Import(DefaultValuesConfigurationPropertiesBindHandlerAdvisor.class)
	static class WithConfigurationPropertiesBindHandlerAdvisor {

	}

	static class DefaultValuesConfigurationPropertiesBindHandlerAdvisor
			implements ConfigurationPropertiesBindHandlerAdvisor {

		@Override
		public BindHandler apply(BindHandler bindHandler) {
			return new DefaultValuesBindHandler(bindHandler);
		}

	}

	static class DefaultValuesBindHandler extends AbstractBindHandler {

		private final Map<ConfigurationPropertyName, ConfigurationPropertyName> mappings;

		DefaultValuesBindHandler(BindHandler bindHandler) {
			super(bindHandler);
			this.mappings = new LinkedHashMap<>();
			this.mappings.put(ConfigurationPropertyName.of("foo.bar.bindings"),
					ConfigurationPropertyName.of("foo.bar.default"));
		}

		@Override
		public <T> @Nullable Bindable<T> onStart(ConfigurationPropertyName name, Bindable<T> target,
				BindContext context) {
			ConfigurationPropertyName defaultName = getDefaultName(name);
			if (defaultName != null) {
				BindResult<T> result = context.getBinder().bind(defaultName, target);
				if (result.isBound()) {
					return target.withExistingValue(result.get());
				}
			}
			return super.onStart(name, target, context);
		}

		private @Nullable ConfigurationPropertyName getDefaultName(ConfigurationPropertyName name) {
			for (Map.Entry<ConfigurationPropertyName, ConfigurationPropertyName> mapping : this.mappings.entrySet()) {
				ConfigurationPropertyName from = mapping.getKey();
				ConfigurationPropertyName to = mapping.getValue();
				if (name.getNumberOfElements() == from.getNumberOfElements() + 1 && from.isParentOf(name)) {
					return to;
				}
			}
			return null;
		}

	}

	@ConfigurationProperties("foo.bar")
	static class BindingServiceProperties {

		private final Map<String, BindingProperties> bindings = new TreeMap<>();

		Map<String, BindingProperties> getBindings() {
			return this.bindings;
		}

	}

	static class BindingProperties {

		private @Nullable String destination;

		private String contentType = "application/json";

		@Nullable String getDestination() {
			return this.destination;
		}

		void setDestination(@Nullable String destination) {
			this.destination = destination;
		}

		String getContentType() {
			return this.contentType;
		}

		void setContentType(String contentType) {
			this.contentType = contentType;
		}

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free