Commit 1b1c61a2 authored by Phillip Webb's avatar Phillip Webb

Make processor output fully reproducible

Update `AutoConfigureAnnotationProcessor` to ensure that the generated
properties file is fully repeatable. Properties are now sorted and
written out directly to ensure that the timestamp comment is not
present.

Closes gh-19370
parent 695de2c6
...@@ -17,7 +17,9 @@ ...@@ -17,7 +17,9 @@
package org.springframework.boot.autoconfigureprocessor; package org.springframework.boot.autoconfigureprocessor;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
...@@ -26,11 +28,12 @@ import java.util.HashSet; ...@@ -26,11 +28,12 @@ import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
...@@ -66,7 +69,7 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor { ...@@ -66,7 +69,7 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
private final Map<String, ValueExtractor> valueExtractors; private final Map<String, ValueExtractor> valueExtractors;
private final Properties properties = new Properties(); private final Map<String, String> properties = new TreeMap<>();
public AutoConfigureAnnotationProcessor() { public AutoConfigureAnnotationProcessor() {
Map<String, String> annotations = new LinkedHashMap<>(); Map<String, String> annotations = new LinkedHashMap<>();
...@@ -177,10 +180,15 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor { ...@@ -177,10 +180,15 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
private void writeProperties() throws IOException { private void writeProperties() throws IOException {
if (!this.properties.isEmpty()) { if (!this.properties.isEmpty()) {
FileObject file = this.processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", Filer filer = this.processingEnv.getFiler();
PROPERTIES_PATH); FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", PROPERTIES_PATH);
try (OutputStream outputStream = file.openOutputStream()) { try (Writer writer = new OutputStreamWriter(file.openOutputStream(), StandardCharsets.UTF_8)) {
this.properties.store(outputStream, null); for (Map.Entry<String, String> entry : this.properties.entrySet()) {
writer.append(entry.getKey());
writer.append("=");
writer.append(entry.getValue());
writer.append(System.lineSeparator());
}
} }
} }
} }
......
...@@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test; ...@@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.testsupport.compiler.TestCompiler; import org.springframework.boot.testsupport.compiler.TestCompiler;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -96,11 +97,24 @@ class AutoConfigureAnnotationProcessorTests { ...@@ -96,11 +97,24 @@ class AutoConfigureAnnotationProcessorTests {
"123"); "123");
} }
@Test // gh-19370
void propertiesAreFullRepeatable() throws Exception {
String first = new String(
FileCopyUtils.copyToByteArray(process(TestOrderedClassConfiguration.class).getWrittenFile()));
String second = new String(
FileCopyUtils.copyToByteArray(process(TestOrderedClassConfiguration.class).getWrittenFile()));
assertThat(first).isEqualTo(second).doesNotContain("#");
}
private Properties compile(Class<?>... types) throws IOException { private Properties compile(Class<?>... types) throws IOException {
return process(types).getWrittenProperties();
}
private TestAutoConfigureAnnotationProcessor process(Class<?>... types) {
TestAutoConfigureAnnotationProcessor processor = new TestAutoConfigureAnnotationProcessor( TestAutoConfigureAnnotationProcessor processor = new TestAutoConfigureAnnotationProcessor(
this.compiler.getOutputLocation()); this.compiler.getOutputLocation());
this.compiler.getTask(types).call(processor); this.compiler.getTask(types).call(processor);
return processor.getWrittenProperties(); return processor;
} }
} }
...@@ -60,7 +60,7 @@ public class TestAutoConfigureAnnotationProcessor extends AutoConfigureAnnotatio ...@@ -60,7 +60,7 @@ public class TestAutoConfigureAnnotationProcessor extends AutoConfigureAnnotatio
} }
public Properties getWrittenProperties() throws IOException { public Properties getWrittenProperties() throws IOException {
File file = new File(this.outputLocation, PROPERTIES_PATH); File file = getWrittenFile();
if (!file.exists()) { if (!file.exists()) {
return null; return null;
} }
...@@ -71,4 +71,8 @@ public class TestAutoConfigureAnnotationProcessor extends AutoConfigureAnnotatio ...@@ -71,4 +71,8 @@ public class TestAutoConfigureAnnotationProcessor extends AutoConfigureAnnotatio
} }
} }
public File getWrittenFile() {
return new File(this.outputLocation, PROPERTIES_PATH);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment