Home / Class/ SourceGenerationTests Class — spring-boot Architecture

SourceGenerationTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

configuration-metadata/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java lines 766–1020

	@Nested
	class SourceGenerationTests {

		@Test
		void simplePropertiesSource() {
			compile(withTestClasses(SimplePropertiesSource.class), (compiled) -> {
				ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(SimplePropertiesSource.class));
				assertThat(metadata).isNotNull()
					.has(Metadata.withProperty("name")
						.ofType(String.class)
						.withDefaultValue("boot")
						.withDescription("Description of this simple property."))
					.has(Metadata.withProperty("enabled")
						.ofType(Boolean.class)
						.withDefaultValue(false)
						.withDescription("Whether it is enabled."));
				assertThat(metadata.getItems()).hasSize(2);
				assertThat(metadata.getHints()).isEmpty();
			});
		}

		@Test
		void simplePropertiesSourceWithAdditionalMetadataIsMerged() {
			String additionalMetadata = """
					{
					   "properties": [
						 {
						   "name": "custom",
						   "type": "java.lang.Integer",
						   "description": "Custom property description."
						 }
					   ]
					 }""";
			compile(withTestClasses(SimplePropertiesSource.class)
				.andThen(withAdditionalMetadata(SimplePropertiesSource.class, additionalMetadata)), (compiled) -> {
					ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
							getSourceMetadataLocation(SimplePropertiesSource.class));
					assertThat(metadata).isNotNull()
						.has(Metadata.withProperty("name")
							.ofType(String.class)
							.withDefaultValue("boot")
							.withDescription("Description of this simple property."))
						.has(Metadata.withProperty("enabled")
							.ofType(Boolean.class)
							.withDefaultValue(false)
							.withDescription("Whether it is enabled."))
						.has(Metadata.withProperty("custom")
							.ofType(Integer.class)
							.withDescription("Custom property description."));
					assertThat(metadata.getItems()).hasSize(3);
				});
		}

		@Test
		void simplePropertiesSourceWithAdditionalMetadataHintIsMerged() {
			String additionalMetadata = """
					{
					 "hints": [
					   {
						 "name": "name",
						 "values": [
						   { "value": "boot", "description": "Spring Boot." },
						   { "value": "framework", "description": "Spring Framework." }
						 ]
					   }
					 ]
					}""";
			compile(withTestClasses(SimplePropertiesSource.class)
				.andThen(withAdditionalMetadata(SimplePropertiesSource.class, additionalMetadata)), (compiled) -> {
					ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
							getSourceMetadataLocation(SimplePropertiesSource.class));
					assertThat(metadata).isNotNull()
						.has(Metadata.withProperty("name")
							.ofType(String.class)
							.withDefaultValue("boot")
							.withDescription("Description of this simple property."))
						.has(Metadata.withProperty("enabled")
							.ofType(Boolean.class)
							.withDefaultValue(false)
							.withDescription("Whether it is enabled."))
						.has(Metadata.withHint("name")
							.withValue(0, "boot", "Spring Boot.")
							.withValue(1, "framework", "Spring Framework."));
					assertThat(metadata.getItems()).hasSize(2);
					assertThat(metadata.getHints()).hasSize(1);
				});
		}

		@Test
		void simplePropertiesSourceWithAdditionalMetadataCanBeOverridden() {
			String additionalMetadata = """
					{
					   "properties": [
						 {
						   "name": "name",
						   "description": "Custom description."
						 }
					   ]
					 }""";
			compile(withTestClasses(SimplePropertiesSource.class)
				.andThen(withAdditionalMetadata(SimplePropertiesSource.class, additionalMetadata)), (compiled) -> {
					ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
							getSourceMetadataLocation(SimplePropertiesSource.class));
					assertThat(metadata).isNotNull()
						.has(Metadata.withProperty("name")
							.ofType(String.class)
							.withDefaultValue("boot")
							.withDescription("Custom description."))
						.has(Metadata.withProperty("enabled")
							.ofType(Boolean.class)
							.withDefaultValue(false)
							.withDescription("Whether it is enabled."));
					assertThat(metadata.getItems()).hasSize(2);
				});
		}

		@Test
		void lombokPropertiesSource() {
			compile(withTestClasses(LombokPropertiesSource.class), (compiled) -> {
				ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(LombokPropertiesSource.class));
				assertThat(metadata).isNotNull()
					.has(Metadata.withProperty("name")
						.ofType(String.class)
						.withDefaultValue("boot")
						.withDescription("Description of this simple property."))
					.has(Metadata.withProperty("enabled")
						.ofType(Boolean.class)
						.withDefaultValue(false)
						.withDescription("Whether it is enabled."));
				assertThat(metadata.getItems()).hasSize(2);
				assertThat(metadata.getHints()).isEmpty();
			});
		}

		@Test
		void immutablePropertiesSource() {
			compile(withTestClasses(ImmutablePropertiesSource.class), (compiled) -> {
				ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(ImmutablePropertiesSource.class));
				assertThat(metadata).isNotNull()
					.has(Metadata.withProperty("name")
						.ofType(String.class)
						.withDefaultValue("boot")
						.withDescription("Description of this simple property."))
					.has(Metadata.withProperty("enabled")
						.ofType(Boolean.class)
						.withDefaultValue(false)
						.withDescription("Whether it is enabled."));
				assertThat(metadata.getItems()).hasSize(2);
				assertThat(metadata.getHints()).isEmpty();
			});
		}

		@Test
		void recordPropertiesSource() {
			compile(withTestClasses(RecordPropertiesSources.class), (compiled) -> {
				ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(RecordPropertiesSources.class));
				assertThat(metadata).isNotNull()
					.has(Metadata.withProperty("name")
						.ofType(String.class)
						.withDefaultValue("boot")
						.withDescription("Description of this simple property."))
					.has(Metadata.withProperty("enabled")
						.ofType(Boolean.class)
						.withDefaultValue(false)
						.withDescription("Whether it is enabled."));
				assertThat(metadata.getItems()).hasSize(2);
				assertThat(metadata.getHints()).isEmpty();
			});
		}

		@Test
		void abstractPropertiesSource() {
			compile(withTestClasses(AbstractPropertiesSource.class), (compiled) -> {
				ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(AbstractPropertiesSource.class));
				assertThat(metadata).isNotNull()
					.has(Metadata.withProperty("name")
						.ofType(String.class)
						.withDefaultValue("boot")
						.withDescription("Description of this simple property."))
					.has(Metadata.withProperty("enabled")
						.ofType(Boolean.class)
						.withDefaultValue(false)
						.withDescription("Whether it is enabled."));
				assertThat(metadata.getItems()).hasSize(2);
				assertThat(metadata.getHints()).isEmpty();
			});
		}

		@Test
		void nonRootConfigurationPropertiesSources() {
			compile(withTestClasses(ConfigurationPropertySourcesContainer.class), (compiled) -> {
				assertThat(CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(ConfigurationPropertySourcesContainer.class)))
					.isNull();
				ConfigurationMetadata firstMetadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(First.class));
				assertThat(firstMetadata).isNotNull()
					.has(Metadata.withProperty("name").ofType(String.class).withDescription("A name."));
				assertThat(firstMetadata.getItems()).hasSize(1);
				assertThat(firstMetadata.getHints()).isEmpty();
				ConfigurationMetadata secondMetadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(Second.class));
				assertThat(secondMetadata).isNotNull()
					.has(Metadata.withProperty("visible")
						.ofType(Boolean.class)
						.withDefaultValue(true)
						.withDescription("Whether this is visible."));
				assertThat(secondMetadata.getItems()).hasSize(1);
				assertThat(secondMetadata.getHints()).isEmpty();
				assertThat(CompiledMetadataReader.getMetadata(compiled, getSourceMetadataLocation(Third.class)))
					.isNull();

			});
		}

		@Test
		void nestedPropertiesSource() {
			compile(withTestClasses(NestedPropertiesSource.class), (compiled) -> {
				ConfigurationMetadata metadata = CompiledMetadataReader.getMetadata(compiled,
						getSourceMetadataLocation(NestedPropertiesSource.class));
				assertThat(metadata).isNotNull()
					.has(Metadata.withProperty("name").ofType(String.class).withDescription("A name."))
					.has(Metadata.withGroup("nested").ofType(NestedPropertiesSource.Nested.class))
					.has(Metadata.withProperty("nested.name").ofType(String.class).withDescription("Another name."));
				assertThat(metadata.getItems()).hasSize(3);
			});
		}

		private String getSourceMetadataLocation(Class<?> type) {
			return "META-INF/spring/configuration-metadata/%s.json".formatted(type.getName());
		}

		private void compile(Function<TestCompiler, TestCompiler> configuration, Consumer<Compiled> compiled) {
			TestCompiler testCompiler = TestCompiler.forSystem();
			configuration.apply(testCompiler)
				.withProcessors(new TestConfigurationMetadataAnnotationProcessor())
				.compile(compiled);
		}

		private Function<TestCompiler, TestCompiler> withTestClasses(Class<?>... testClasses) {
			return (compiler) -> compiler
				.withSources(Arrays.stream(testClasses).map(SourceFile::forTestClass).toList());
		}

		private Function<TestCompiler, TestCompiler> withAdditionalMetadata(Class<?> type, String content) {
			String location = "META-INF/spring/configuration-metadata/additional/%s.json".formatted(type.getName());
			return (compiler) -> compiler.withResources(ResourceFile.of(location, content));
		}

	}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free