Use deterministic order for configuration properties metadata

This commit updates the annotation processor to write metadata in a
consistent way. Groups, properties and hints are written and each item
is ordered alphabetically based on its name.

Also, deprecated items are written last.

Closes gh-14347
This commit is contained in:
Stephane Nicoll
2018-09-08 08:20:43 +02:00
parent d3ecd02987
commit 0493355241
6 changed files with 118 additions and 61 deletions

View File

@@ -22,6 +22,8 @@ import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Rule;
@@ -822,9 +824,26 @@ public class ConfigurationMetadataAnnotationProcessorTests {
ConfigurationMetadata metadata = compile(SimpleProperties.class,
SimpleConflictingProperties.class);
assertThat(metadata.getItems()).hasSize(6);
assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class)
.fromSource(SimpleProperties.class).withDescription("A simple flag.")
.withDeprecation(null, null).withDefaultValue(true));
List<ItemMetadata> items = metadata.getItems().stream()
.filter((item) -> item.getName().equals("simple.flag"))
.collect(Collectors.toList());
assertThat(items).hasSize(2);
ItemMetadata matchingProperty = items.stream()
.filter((item) -> item.getType().equals(Boolean.class.getName()))
.findFirst().orElse(null);
assertThat(matchingProperty).isNotNull();
assertThat(matchingProperty.getDefaultValue()).isEqualTo(true);
assertThat(matchingProperty.getSourceType())
.isEqualTo(SimpleProperties.class.getName());
assertThat(matchingProperty.getDescription()).isEqualTo("A simple flag.");
ItemMetadata nonMatchingProperty = items.stream()
.filter((item) -> item.getType().equals(String.class.getName()))
.findFirst().orElse(null);
assertThat(nonMatchingProperty).isNotNull();
assertThat(nonMatchingProperty.getDefaultValue()).isEqualTo("hello");
assertThat(nonMatchingProperty.getSourceType())
.isEqualTo(SimpleConflictingProperties.class.getName());
assertThat(nonMatchingProperty.getDescription()).isNull();
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -18,6 +18,7 @@ package org.springframework.boot.configurationprocessor.metadata;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
@@ -82,4 +83,51 @@ public class JsonMarshallerTests {
.withProvider("second"));
}
@Test
public void marshallOrderItems() throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemHint.newHint("fff"));
metadata.add(ItemHint.newHint("eee"));
metadata.add(ItemMetadata.newProperty("com.example.bravo", "bbb", null, null,
null, null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.bravo", "aaa", null, null,
null, null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.alpha", "ddd", null, null,
null, null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.alpha", "ccc", null, null,
null, null, null, null));
metadata.add(ItemMetadata.newGroup("com.acme.bravo",
"com.example.AnotherTestProperties", null, null));
metadata.add(ItemMetadata.newGroup("com.acme.alpha", "com.example.TestProperties",
null, null));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = new String(outputStream.toByteArray());
assertThat(json).containsSubsequence("\"groups\"", "\"com.acme.alpha\"",
"\"com.acme.bravo\"", "\"properties\"", "\"com.example.alpha.ccc\"",
"\"com.example.alpha.ddd\"", "\"com.example.bravo.aaa\"",
"\"com.example.bravo.bbb\"", "\"hints\"", "\"eee\"", "\"fff\"");
}
@Test
public void marshallPutDeprecatedItemsAtTheEnd() throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("com.example.bravo", "bbb", null, null,
null, null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.bravo", "aaa", null, null,
null, null, null, new ItemDeprecation(null, null, "warning")));
metadata.add(ItemMetadata.newProperty("com.example.alpha", "ddd", null, null,
null, null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.alpha", "ccc", null, null,
null, null, null, new ItemDeprecation(null, null, "warning")));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = new String(outputStream.toByteArray());
assertThat(json).containsSubsequence("\"properties\"",
"\"com.example.alpha.ddd\"", "\"com.example.bravo.bbb\"",
"\"com.example.alpha.ccc\"", "\"com.example.bravo.aaa\"");
}
}