Home / Class/ ConfigurationPropertiesScanTests Class — spring-boot Architecture

ConfigurationPropertiesScanTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanTests.java lines 52–166

class ConfigurationPropertiesScanTests {

	private AnnotationConfigApplicationContext context;

	@BeforeEach
	void setup() {
		this.context = new AnnotationConfigApplicationContext();
	}

	@AfterEach
	void teardown() {
		if (this.context != null) {
			this.context.close();
		}
	}

	@Test
	void scanImportBeanRegistrarShouldBeEnvironmentAwareWithRequiredProfile() {
		this.context.getEnvironment().addActiveProfile("test");
		load(TestConfiguration.class);
		assertThat(this.context.containsBean(
				"profile-org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration$MyProfileProperties"))
			.isTrue();
	}

	@Test
	void scanImportBeanRegistrarShouldBeEnvironmentAwareWithoutRequiredProfile() {
		load(TestConfiguration.class);
		assertThat(this.context.containsBean(
				"profile-org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration$MyProfileProperties"))
			.isFalse();
	}

	@Test
	void scanImportBeanRegistrarShouldBeResourceLoaderAwareWithRequiredResource() {
		DefaultResourceLoader resourceLoader = mock(DefaultResourceLoader.class);
		this.context.setResourceLoader(resourceLoader);
		willCallRealMethod().given(resourceLoader).getClassLoader();
		given(resourceLoader.getResource("test")).willReturn(new ByteArrayResource("test".getBytes()));
		load(TestConfiguration.class);
		assertThat(this.context.containsBean(
				"resource-org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration$MyResourceProperties"))
			.isTrue();
	}

	@Test
	void scanImportBeanRegistrarShouldBeResourceLoaderAwareWithoutRequiredResource() {
		load(TestConfiguration.class);
		assertThat(this.context.containsBean(
				"resource-org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration$MyResourceProperties"))
			.isFalse();
	}

	@Test
	void scanImportBeanRegistrarShouldUsePackageName() {
		load(TestAnotherPackageConfiguration.class);
		assertThat(this.context.getBeanNamesForType(BProperties.class)).containsOnly(
				"b.first-org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration$BFirstProperties",
				"b.second-org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration$BSecondProperties");
	}

	@Test
	void scanImportBeanRegistrarShouldApplyTypeExcludeFilter() {
		this.context.getBeanFactory().registerSingleton("filter", new ConfigurationPropertiesTestTypeExcludeFilter());
		this.context.register(TestAnotherPackageConfiguration.class);
		this.context.refresh();
		assertThat(this.context.getBeanNamesForType(BProperties.class)).containsOnly(
				"b.first-org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration$BFirstProperties");
	}

	@Test
	void scanShouldBindConfigurationProperties() {
		load(TestAnotherPackageConfiguration.class, "b.first.name=constructor", "b.second.number=42");
		assertThat(this.context.getBean(BFirstProperties.class).getName()).isEqualTo("constructor");
		assertThat(this.context.getBean(BSecondProperties.class).getNumber()).isEqualTo(42);
	}

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

	@ConfigurationPropertiesScan(basePackageClasses = AScanConfiguration.class)
	static class TestConfiguration {

	}

	@ConfigurationPropertiesScan(basePackages = "org.springframework.boot.context.properties.scan.valid.b")
	static class TestAnotherPackageConfiguration {

	}

	static class ConfigurationPropertiesTestTypeExcludeFilter extends TypeExcludeFilter {

		@Override
		public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
				throws IOException {
			AssignableTypeFilter typeFilter = new AssignableTypeFilter(BFirstProperties.class);
			return !typeFilter.match(metadataReader, metadataReaderFactory);
		}

		@Override
		public boolean equals(@Nullable Object o) {
			return (this == o);
		}

		@Override
		public int hashCode() {
			return Objects.hash(42);
		}

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free