From 03a34257670f482b0bd34faf631e7cfa06bf1df2 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 6 Nov 2024 11:30:50 +0000 Subject: [PATCH] Fix test for incremental build with type rename Closes gh-38119 --- ...crementalBuildMetadataGenerationTests.java | 6 ++--- .../configurationprocessor/TestProject.java | 26 ++++++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/IncrementalBuildMetadataGenerationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/IncrementalBuildMetadataGenerationTests.java index d77cd2d820e4..5bc7dd744e96 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/IncrementalBuildMetadataGenerationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/IncrementalBuildMetadataGenerationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.boot.configurationprocessor; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; @@ -74,7 +73,6 @@ void incrementalBuildAnnotationRemoved() { } @Test - @Disabled("gh-26271") void incrementalBuildTypeRenamed() { TestProject project = new TestProject(FooProperties.class, BarProperties.class); ConfigurationMetadata metadata = project.compile(); @@ -85,7 +83,7 @@ void incrementalBuildTypeRenamed() { assertThat(metadata).doesNotHave(Metadata.withProperty("bar.counter").fromSource(RenamedBarProperties.class)); project.delete(BarProperties.class); project.add(RenamedBarProperties.class); - metadata = project.compile(); + metadata = project.compile(metadata); assertThat(metadata) .has(Metadata.withProperty("foo.counter").fromSource(FooProperties.class).withDefaultValue(0)); assertThat(metadata) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java index cacb142bd56b..cd4933fed571 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,17 +16,22 @@ package org.springframework.boot.configurationprocessor; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.UncheckedIOException; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; +import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller; import org.springframework.boot.configurationprocessor.test.CompiledMetadataReader; import org.springframework.boot.configurationprocessor.test.TestConfigurationMetadataAnnotationProcessor; import org.springframework.boot.configurationsample.ConfigurationProperties; import org.springframework.boot.configurationsample.NestedConfigurationProperty; +import org.springframework.core.test.tools.ResourceFile; import org.springframework.core.test.tools.SourceFile; import org.springframework.core.test.tools.SourceFiles; import org.springframework.core.test.tools.TestCompiler; @@ -55,14 +60,33 @@ public TestProject(Class... classes) { } public ConfigurationMetadata compile() { + return compile(null); + } + + public ConfigurationMetadata compile(ConfigurationMetadata previousMetadata) { TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor(); TestCompiler compiler = TestCompiler.forSystem().withProcessors(processor); + if (previousMetadata != null) { + compiler = compiler.withResources( + ResourceFile.of("META-INF/spring-configuration-metadata.json", asBytes(previousMetadata))); + } AtomicReference configurationMetadata = new AtomicReference<>(); compiler.compile(this.sources, (compiled) -> configurationMetadata.set(CompiledMetadataReader.getMetadata(compiled))); return configurationMetadata.get(); } + private byte[] asBytes(ConfigurationMetadata previousMetadata) { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try { + new JsonMarshaller().write(previousMetadata, output); + } + catch (IOException ex) { + throw new UncheckedIOException(ex); + } + return output.toByteArray(); + } + /** * Add source code at the end of file, just before last '}' * @param target the target