Restructure metadata generation

This commit restructures the annotation processor to off-load most of
its logic in a PropertyDescriptor abstraction that is consumed to
generate the relevant metadata.

This has the benefit to isolate the various way properties can be
identified (java bean and lombok for now).

Closes gh-16036
This commit is contained in:
Stephane Nicoll
2019-02-21 11:15:02 +01:00
parent 99c0b4561d
commit 00a18c32ab
24 changed files with 1836 additions and 388 deletions

View File

@@ -24,6 +24,8 @@ import org.springframework.boot.configurationsample.simple.ClassWithNestedProper
import org.springframework.boot.configurationsample.simple.DeprecatedSingleProperty;
import org.springframework.boot.configurationsample.simple.DescriptionProperties;
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesGrandparent;
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesParent;
import org.springframework.boot.configurationsample.simple.NotAnnotated;
import org.springframework.boot.configurationsample.simple.SimpleArrayProperties;
import org.springframework.boot.configurationsample.simple.SimpleCollectionProperties;
@@ -141,16 +143,18 @@ public class ConfigurationMetadataAnnotationProcessorTests
@Test
public void hierarchicalProperties() {
ConfigurationMetadata metadata = compile(HierarchicalProperties.class);
ConfigurationMetadata metadata = compile(HierarchicalProperties.class,
HierarchicalPropertiesParent.class,
HierarchicalPropertiesGrandparent.class);
assertThat(metadata).has(Metadata.withGroup("hierarchical")
.fromSource(HierarchicalProperties.class));
assertThat(metadata).has(Metadata.withProperty("hierarchical.first", String.class)
.withDefaultValue("one").fromSource(HierarchicalProperties.class));
assertThat(metadata).has(Metadata
.withProperty("hierarchical.second", String.class).withDefaultValue("two")
.fromSource(HierarchicalProperties.class));
assertThat(metadata)
.has(Metadata.withProperty("hierarchical.second", String.class)
.fromSource(HierarchicalProperties.class));
assertThat(metadata).has(Metadata.withProperty("hierarchical.third", String.class)
.fromSource(HierarchicalProperties.class));
.withDefaultValue("three").fromSource(HierarchicalProperties.class));
}
@Test

View File

@@ -0,0 +1,287 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.IOException;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import org.junit.Test;
import org.springframework.boot.configurationsample.simple.DeprecatedProperties;
import org.springframework.boot.configurationsample.simple.DeprecatedSingleProperty;
import org.springframework.boot.configurationsample.simple.SimpleCollectionProperties;
import org.springframework.boot.configurationsample.simple.SimpleProperties;
import org.springframework.boot.configurationsample.simple.SimpleTypeProperties;
import org.springframework.boot.configurationsample.specific.InnerClassProperties;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link JavaBeanPropertyDescriptor}.
*
* @author Stephane Nicoll
*/
public class JavaBeanPropertyDescriptorTests extends PropertyDescriptorTests {
@Test
public void javaBeanSimpleProperty() throws IOException {
process(SimpleTypeProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(SimpleTypeProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"myString");
assertThat(property.getName()).isEqualTo("myString");
assertThat(property.getSource()).isSameAs(property.getGetter());
assertThat(property.getGetter().getSimpleName()).hasToString("getMyString");
assertThat(property.getSetter().getSimpleName()).hasToString("setMyString");
assertThat(property.isProperty(metadataEnv)).isTrue();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void javaBeanCollectionProperty() throws IOException {
process(SimpleCollectionProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(SimpleCollectionProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"doubles");
assertThat(property.getName()).isEqualTo("doubles");
assertThat(property.getGetter().getSimpleName()).hasToString("getDoubles");
assertThat(property.getSetter()).isNull();
assertThat(property.isProperty(metadataEnv)).isTrue();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void javaBeanNestedPropertySameClass() throws IOException {
process(InnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(InnerClassProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"first");
assertThat(property.getName()).isEqualTo("first");
assertThat(property.getGetter().getSimpleName()).hasToString("getFirst");
assertThat(property.getSetter()).isNull();
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isTrue();
});
}
@Test
public void javaBeanNestedPropertyWithAnnotation() throws IOException {
process(InnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(InnerClassProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"third");
assertThat(property.getName()).isEqualTo("third");
assertThat(property.getGetter().getSimpleName()).hasToString("getThird");
assertThat(property.getSetter()).isNull();
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isTrue();
});
}
@Test
public void javaBeanSimplePropertyWithOnlyGetterShouldNotBeExposed()
throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
ExecutableElement getter = getMethod(ownerElement, "getSize");
VariableElement field = getField(ownerElement, "size");
JavaBeanPropertyDescriptor property = new JavaBeanPropertyDescriptor(
ownerElement, getter, getter, "size", field.asType(), field, null);
assertThat(property.getName()).isEqualTo("size");
assertThat(property.getSource()).isSameAs(property.getGetter());
assertThat(property.getGetter().getSimpleName()).hasToString("getSize");
assertThat(property.getSetter()).isNull();
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void javaBeanSimplePropertyWithOnlySetterShouldNotBeExposed()
throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
VariableElement field = getField(ownerElement, "counter");
JavaBeanPropertyDescriptor property = new JavaBeanPropertyDescriptor(
ownerElement, null, null, "counter", field.asType(), field,
getMethod(ownerElement, "setCounter"));
assertThat(property.getName()).isEqualTo("counter");
assertThat(property.getSource()).isSameAs(property.getGetter());
assertThat(property.getGetter()).isNull();
assertThat(property.getSetter().getSimpleName()).hasToString("setCounter");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void javaBeanMetadataSimpleProperty() throws IOException {
process(SimpleTypeProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(SimpleTypeProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"myString");
assertItemMetadata(metadataEnv, property).isProperty()
.hasName("test.my-string").hasType(String.class)
.hasSourceType(SimpleTypeProperties.class).hasNoDescription()
.isNotDeprecated();
});
}
@Test
public void javaBeanMetadataCollectionProperty() throws IOException {
process(SimpleCollectionProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(SimpleCollectionProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"doubles");
assertItemMetadata(metadataEnv, property).isProperty().hasName("test.doubles")
.hasType("java.util.List<java.lang.Double>")
.hasSourceType(SimpleCollectionProperties.class).hasNoDescription()
.isNotDeprecated();
});
}
@Test
public void javaBeanMetadataNestedGroup() throws IOException {
process(InnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(InnerClassProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"first");
assertItemMetadata(metadataEnv, property).isGroup().hasName("test.first")
.hasType(
"org.springframework.boot.configurationsample.specific.InnerClassProperties$Foo")
.hasSourceType(InnerClassProperties.class)
.hasSourceMethod("getFirst()").hasNoDescription().isNotDeprecated();
});
}
@Test
public void javaBeanMetadataNotACandidatePropertyShouldReturnNull()
throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
VariableElement field = getField(ownerElement, "counter");
JavaBeanPropertyDescriptor property = new JavaBeanPropertyDescriptor(
ownerElement, null, null, "counter", field.asType(), field,
getMethod(ownerElement, "setCounter"));
assertThat(property.resolveItemMetadata("test", metadataEnv)).isNull();
});
}
@Test
@SuppressWarnings("deprecation")
public void javaBeanDeprecatedPropertyOnClass() throws IOException {
process(DeprecatedProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(DeprecatedProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"name");
assertItemMetadata(metadataEnv, property).isProperty()
.isDeprecatedWithNoInformation();
});
}
@Test
public void javaBeanMetadataDeprecatedPropertyWithAnnotation() throws IOException {
process(DeprecatedSingleProperty.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(DeprecatedSingleProperty.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"name");
assertItemMetadata(metadataEnv, property).isProperty()
.isDeprecatedWithReason("renamed")
.isDeprecatedWithReplacement("singledeprecated.new-name");
});
}
@Test
public void javaBeanDeprecatedPropertyOnGetter() throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"flag", "isFlag", "setFlag");
assertItemMetadata(metadataEnv, property).isProperty()
.isDeprecatedWithNoInformation();
});
}
@Test
public void javaBeanDeprecatedPropertyOnSetter() throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"theName");
assertItemMetadata(metadataEnv, property).isProperty()
.isDeprecatedWithNoInformation();
});
}
@Test
public void javaBeanPropertyWithDescription() throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"theName");
assertItemMetadata(metadataEnv, property).isProperty()
.hasDescription("The name of this simple properties.");
});
}
@Test
public void javaBeanPropertyWithDefaultValue() throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
JavaBeanPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"theName");
assertItemMetadata(metadataEnv, property).isProperty()
.hasDefaultValue("boot");
});
}
protected JavaBeanPropertyDescriptor createPropertyDescriptor(
TypeElement ownerElement, String name) {
return createPropertyDescriptor(ownerElement, name,
createAccessorMethod("get", name), createAccessorMethod("set", name));
}
protected JavaBeanPropertyDescriptor createPropertyDescriptor(
TypeElement ownerElement, String name, String getterName, String setterName) {
ExecutableElement getter = getMethod(ownerElement, getterName);
ExecutableElement setter = getMethod(ownerElement, setterName);
VariableElement field = getField(ownerElement, name);
return new JavaBeanPropertyDescriptor(ownerElement, null, getter, name,
getter.getReturnType(), field, setter);
}
private String createAccessorMethod(String prefix, String name) {
char[] chars = name.toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
return prefix + new String(chars, 0, chars.length);
}
}

View File

@@ -0,0 +1,308 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.IOException;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import org.junit.Test;
import org.springframework.boot.configurationsample.lombok.LombokDefaultValueProperties;
import org.springframework.boot.configurationsample.lombok.LombokDeprecatedProperties;
import org.springframework.boot.configurationsample.lombok.LombokDeprecatedSingleProperty;
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
import org.springframework.boot.configurationsample.lombok.LombokInnerClassProperties;
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
import org.springframework.boot.configurationsample.simple.SimpleProperties;
import org.springframework.boot.configurationsample.specific.InnerClassProperties;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link LombokPropertyDescriptor}.
*
* @author Stephane Nicoll
*/
public class LombokPropertyDescriptorTests extends PropertyDescriptorTests {
@Test
public void lombokSimpleProperty() throws IOException {
process(LombokSimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"name");
assertThat(property.getName()).isEqualTo("name");
assertThat(property.getSource()).isSameAs(property.getField());
assertThat(property.getField().getSimpleName()).hasToString("name");
assertThat(property.isProperty(metadataEnv)).isTrue();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void lombokCollectionProperty() throws IOException {
process(LombokSimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"items");
assertThat(property.getName()).isEqualTo("items");
assertThat(property.getSource()).isSameAs(property.getField());
assertThat(property.getField().getSimpleName()).hasToString("items");
assertThat(property.isProperty(metadataEnv)).isTrue();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void lombokNestedPropertySameClass() throws IOException {
process(LombokInnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokInnerClassProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"first");
assertThat(property.getName()).isEqualTo("first");
assertThat(property.getSource()).isSameAs(property.getField());
assertThat(property.getField().getSimpleName()).hasToString("first");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isTrue();
});
}
@Test
public void lombokNestedPropertyWithAnnotation() throws IOException {
process(LombokInnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokInnerClassProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"third");
assertThat(property.getName()).isEqualTo("third");
assertThat(property.getSource()).isSameAs(property.getField());
assertThat(property.getField().getSimpleName()).hasToString("third");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isTrue();
});
}
@Test
public void lombokSimplePropertyWithOnlyGetterOnClassShouldNotBeExposed()
throws IOException {
process(LombokSimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"ignored");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void lombokSimplePropertyWithOnlyGetterOnDataClassShouldNotBeExposed()
throws IOException {
process(LombokSimpleDataProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleDataProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"ignored");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void lombokSimplePropertyWithOnlyGetterOnFieldShouldNotBeExposed()
throws IOException {
process(LombokExplicitProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokExplicitProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"ignoredOnlyGetter");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void lombokSimplePropertyWithOnlySetterOnFieldShouldNotBeExposed()
throws IOException {
process(LombokExplicitProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokExplicitProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"ignoredOnlySetter");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void lombokMetadataSimpleProperty() throws IOException {
process(LombokSimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"description");
assertItemMetadata(metadataEnv, property).isProperty()
.hasName("test.description").hasType(String.class)
.hasSourceType(LombokSimpleProperties.class).hasNoDescription()
.isNotDeprecated();
});
}
@Test
public void lombokMetadataCollectionProperty() throws IOException {
process(LombokSimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"items");
assertItemMetadata(metadataEnv, property).isProperty().hasName("test.items")
.hasType("java.util.List<java.lang.String>")
.hasSourceType(LombokSimpleProperties.class).hasNoDescription()
.isNotDeprecated();
});
}
@Test
public void lombokMetadataNestedGroup() throws IOException {
process(LombokInnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokInnerClassProperties.class);
VariableElement field = getField(ownerElement, "third");
ExecutableElement getter = getMethod(ownerElement, "getThird");
LombokPropertyDescriptor property = new LombokPropertyDescriptor(ownerElement,
null, field, "third", field.asType(), getter);
assertItemMetadata(metadataEnv, property).isGroup().hasName("test.third")
.hasType(
"org.springframework.boot.configurationsample.lombok.SimpleLombokPojo")
.hasSourceType(LombokInnerClassProperties.class)
.hasSourceMethod("getThird()").hasNoDescription().isNotDeprecated();
});
}
@Test
public void lombokMetadataNestedGroupNoGetter() throws IOException {
process(LombokInnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokInnerClassProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"first");
assertItemMetadata(metadataEnv, property).isGroup().hasName("test.first")
.hasType(
"org.springframework.boot.configurationsample.lombok.LombokInnerClassProperties$Foo")
.hasSourceType(LombokInnerClassProperties.class).hasSourceMethod(null)
.hasNoDescription().isNotDeprecated();
});
}
@Test
public void lombokMetadataNotACandidatePropertyShouldReturnNull() throws IOException {
process(LombokSimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"ignored");
assertThat(property.resolveItemMetadata("test", metadataEnv)).isNull();
});
}
@Test
@SuppressWarnings("deprecation")
public void lombokDeprecatedPropertyOnClass() throws IOException {
process(LombokDeprecatedProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokDeprecatedProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"name");
assertItemMetadata(metadataEnv, property).isProperty()
.isDeprecatedWithNoInformation();
});
}
@Test
public void lombokDeprecatedPropertyOnField() throws IOException {
process(LombokDeprecatedSingleProperty.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokDeprecatedSingleProperty.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"name");
assertItemMetadata(metadataEnv, property).isProperty()
.isDeprecatedWithNoInformation();
});
}
@Test
public void lombokPropertyWithDescription() throws IOException {
process(LombokSimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokSimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"name");
assertItemMetadata(metadataEnv, property).isProperty()
.hasDescription("Name description.");
});
}
@Test
public void lombokPropertyWithDefaultValue() throws IOException {
process(LombokDefaultValueProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(LombokDefaultValueProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"description");
assertItemMetadata(metadataEnv, property).isProperty()
.hasDefaultValue("my description");
});
}
@Test
public void lombokPropertyNotCandidate() throws IOException {
process(SimpleProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv.getRootElement(SimpleProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"theName");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
@Test
public void lombokNestedPropertyNotCandidate() throws IOException {
process(InnerClassProperties.class, (roundEnv, metadataEnv) -> {
TypeElement ownerElement = roundEnv
.getRootElement(InnerClassProperties.class);
LombokPropertyDescriptor property = createPropertyDescriptor(ownerElement,
"first");
assertThat(property.isProperty(metadataEnv)).isFalse();
assertThat(property.isNested(metadataEnv)).isFalse();
});
}
protected LombokPropertyDescriptor createPropertyDescriptor(TypeElement ownerElement,
String name) {
VariableElement field = getField(ownerElement, name);
return new LombokPropertyDescriptor(ownerElement, null, field, name,
field.asType(), null);
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.function.Function;
import javax.annotation.processing.ProcessingEnvironment;
import org.springframework.boot.configurationprocessor.test.TestConfigurationMetadataAnnotationProcessor;
/**
* A factory for {@link MetadataGenerationEnvironment} against test annotations.
*
* @author Stephane Nicoll
*/
class MetadataGenerationEnvironmentFactory
implements Function<ProcessingEnvironment, MetadataGenerationEnvironment> {
@Override
public MetadataGenerationEnvironment apply(ProcessingEnvironment environment) {
return new MetadataGenerationEnvironment(environment,
TestConfigurationMetadataAnnotationProcessor.CONFIGURATION_PROPERTIES_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.NESTED_CONFIGURATION_PROPERTY_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.READ_OPERATION_ANNOTATION);
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.lang.model.element.TypeElement;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester;
import org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor;
import org.springframework.boot.configurationsample.lombok.LombokExplicitProperties;
import org.springframework.boot.configurationsample.lombok.LombokSimpleDataProperties;
import org.springframework.boot.configurationsample.lombok.LombokSimpleProperties;
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesGrandparent;
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesParent;
import org.springframework.boot.configurationsample.simple.SimpleProperties;
import org.springframework.boot.testsupport.compiler.TestCompiler;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link PropertyDescriptorResolver}.
*
* @author Stephane Nicoll
*/
public class PropertyDescriptorResolverTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void propertiesWithJavaBeanProperties() throws IOException {
process(SimpleProperties.class, propertyNames((stream) -> assertThat(stream)
.containsExactly("theName", "flag", "comparator")));
}
@Test
public void propertiesWithJavaBeanHierarchicalProperties() throws IOException {
process(HierarchicalProperties.class,
Arrays.asList(HierarchicalPropertiesParent.class,
HierarchicalPropertiesGrandparent.class),
(type, metadataEnv) -> {
PropertyDescriptorResolver resolver = new PropertyDescriptorResolver(
metadataEnv);
assertThat(
resolver.resolve(type, null).map(PropertyDescriptor::getName))
.containsExactly("third", "second", "first");
assertThat(resolver.resolve(type, null)
.map((descriptor) -> descriptor.resolveItemMetadata("test",
metadataEnv))
.map(ItemMetadata::getDefaultValue)).containsExactly("three",
"two", "one");
});
}
@Test
public void propertiesWithLombokGetterSetterAtClassLevel() throws IOException {
process(LombokSimpleProperties.class, propertyNames((stream) -> assertThat(stream)
.containsExactly("name", "description", "counter", "number", "items")));
}
@Test
public void propertiesWithLombokGetterSetterAtFieldLevel() throws IOException {
process(LombokExplicitProperties.class,
propertyNames((stream) -> assertThat(stream).containsExactly("name",
"description", "counter", "number", "items")));
}
@Test
public void propertiesWithLombokDataClass() throws IOException {
process(LombokSimpleDataProperties.class,
propertyNames((stream) -> assertThat(stream).containsExactly("name",
"description", "counter", "number", "items")));
}
private BiConsumer<TypeElement, MetadataGenerationEnvironment> properties(
Consumer<Stream<PropertyDescriptor<?>>> stream) {
return (element, metadataEnv) -> {
PropertyDescriptorResolver resolver = new PropertyDescriptorResolver(
metadataEnv);
stream.accept(resolver.resolve(element, null));
};
}
private BiConsumer<TypeElement, MetadataGenerationEnvironment> propertyNames(
Consumer<Stream<String>> stream) {
return properties(
(result) -> stream.accept(result.map(PropertyDescriptor::getName)));
}
private void process(Class<?> target,
BiConsumer<TypeElement, MetadataGenerationEnvironment> consumer)
throws IOException {
process(target, Collections.emptyList(), consumer);
}
private void process(Class<?> target, Collection<Class<?>> additionalClasses,
BiConsumer<TypeElement, MetadataGenerationEnvironment> consumer)
throws IOException {
BiConsumer<RoundEnvironmentTester, MetadataGenerationEnvironment> internalConsumer = (
roundEnv, metadataEnv) -> {
TypeElement element = roundEnv.getRootElement(target);
consumer.accept(element, metadataEnv);
};
TestableAnnotationProcessor<MetadataGenerationEnvironment> processor = new TestableAnnotationProcessor<>(
internalConsumer, new MetadataGenerationEnvironmentFactory());
TestCompiler compiler = new TestCompiler(this.temporaryFolder);
ArrayList<Class<?>> allClasses = new ArrayList<>();
allClasses.add(target);
allClasses.addAll(additionalClasses);
compiler.getTask(allClasses.toArray(new Class<?>[0])).call(processor);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.IOException;
import java.util.function.BiConsumer;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementFilter;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.configurationprocessor.test.ItemMetadataAssert;
import org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester;
import org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor;
import org.springframework.boot.testsupport.compiler.TestCompiler;
/**
* Base test infrastructure to test {@link PropertyDescriptor} implementations.
*
* @author Stephane Nicoll
*/
public abstract class PropertyDescriptorTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
protected ExecutableElement getMethod(TypeElement element, String name) {
return ElementFilter.methodsIn(element.getEnclosedElements()).stream().filter(
(method) -> ((Element) method).getSimpleName().toString().equals(name))
.findFirst().orElse(null);
}
protected VariableElement getField(TypeElement element, String name) {
return ElementFilter.fieldsIn(element.getEnclosedElements()).stream().filter(
(method) -> ((Element) method).getSimpleName().toString().equals(name))
.findFirst().orElse(null);
}
protected ItemMetadataAssert assertItemMetadata(
MetadataGenerationEnvironment metadataEnv, PropertyDescriptor<?> property) {
return new ItemMetadataAssert(property.resolveItemMetadata("test", metadataEnv));
}
protected void process(Class<?> target,
BiConsumer<RoundEnvironmentTester, MetadataGenerationEnvironment> consumer)
throws IOException {
TestableAnnotationProcessor<MetadataGenerationEnvironment> processor = new TestableAnnotationProcessor<>(
consumer, new MetadataGenerationEnvironmentFactory());
TestCompiler compiler = new TestCompiler(this.temporaryFolder);
compiler.getTask(target).call(processor);
}
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.test;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AssertProvider;
import org.assertj.core.internal.Objects;
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
/**
* AssertJ assert for {@link ItemMetadata}.
*
* @author Stephane Nicoll
*/
public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemMetadata>
implements AssertProvider<ItemMetadataAssert> {
private static final Objects objects = Objects.instance();
public ItemMetadataAssert(ItemMetadata itemMetadata) {
super(itemMetadata, ItemMetadataAssert.class);
objects.assertNotNull(this.info, itemMetadata);
}
public ItemMetadataAssert isProperty() {
objects.assertEqual(this.info, this.actual.isOfItemType(ItemType.PROPERTY), true);
return this;
}
public ItemMetadataAssert isGroup() {
objects.assertEqual(this.info, this.actual.isOfItemType(ItemType.GROUP), true);
return this;
}
public ItemMetadataAssert hasName(String name) {
objects.assertEqual(this.info, this.actual.getName(), name);
return this;
}
public ItemMetadataAssert hasType(String type) {
objects.assertEqual(this.info, this.actual.getType(), type);
return this;
}
public ItemMetadataAssert hasType(Class<?> type) {
return hasType(type.getName());
}
public ItemMetadataAssert hasDescription(String description) {
objects.assertEqual(this.info, this.actual.getDescription(), description);
return this;
}
public ItemMetadataAssert hasNoDescription() {
return hasDescription(null);
}
public ItemMetadataAssert hasSourceType(String type) {
objects.assertEqual(this.info, this.actual.getSourceType(), type);
return this;
}
public ItemMetadataAssert hasSourceType(Class<?> type) {
return hasSourceType(type.getName());
}
public ItemMetadataAssert hasSourceMethod(String type) {
objects.assertEqual(this.info, this.actual.getSourceMethod(), type);
return this;
}
public ItemMetadataAssert hasDefaultValue(Object defaultValue) {
objects.assertEqual(this.info, this.actual.getDefaultValue(), defaultValue);
return this;
}
public ItemMetadataAssert isDeprecatedWithNoInformation() {
assertItemDeprecation();
return this;
}
public ItemMetadataAssert isDeprecatedWithReason(String reason) {
ItemDeprecation deprecation = assertItemDeprecation();
objects.assertEqual(this.info, deprecation.getReason(), reason);
return this;
}
public ItemMetadataAssert isDeprecatedWithReplacement(String replacement) {
ItemDeprecation deprecation = assertItemDeprecation();
objects.assertEqual(this.info, deprecation.getReplacement(), replacement);
return this;
}
public ItemMetadataAssert isNotDeprecated() {
objects.assertNull(this.info, this.actual.getDeprecation());
return this;
}
private ItemDeprecation assertItemDeprecation() {
ItemDeprecation deprecation = this.actual.getDeprecation();
objects.assertNotNull(this.info, deprecation);
objects.assertNull(this.info, deprecation.getLevel());
return deprecation;
}
@Override
public ItemMetadataAssert assertThat() {
return this;
}
}

View File

@@ -41,15 +41,15 @@ import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
public class TestConfigurationMetadataAnnotationProcessor
extends ConfigurationMetadataAnnotationProcessor {
static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot.configurationsample.ConfigurationProperties";
public static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot.configurationsample.ConfigurationProperties";
static final String NESTED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.NestedConfigurationProperty";
public static final String NESTED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.NestedConfigurationProperty";
static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.DeprecatedConfigurationProperty";
public static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.DeprecatedConfigurationProperty";
static final String ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.Endpoint";
public static final String ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.Endpoint";
static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.configurationsample.ReadOperation";
public static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.configurationsample.ReadOperation";
private ConfigurationMetadata metadata;

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import lombok.Data;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Configuration properties with default values.
*
* @author Stephane Nicoll
*/
@Data
@ConfigurationProperties("default")
public class LombokDefaultValueProperties {
private String description = "my description";
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Deprecated configuration properties.
*
* @author Stephane Nicoll
*/
@Getter
@Setter
@ConfigurationProperties(prefix = "deprecated")
@Deprecated
public class LombokDeprecatedProperties {
private String name;
private String description;
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import lombok.Data;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Configuration properties with a single deprecated element.
*
* @author Stephane Nicoll
*/
@Data
@ConfigurationProperties("singledeprecated")
public class LombokDeprecatedSingleProperty {
@Deprecated
private String name;
private String description;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -40,6 +40,11 @@ public class LombokInnerClassProperties {
private Fourth fourth;
// Only there to record the source method
public SimpleLombokPojo getThird() {
return this.third;
}
@Data
public static class Foo {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -26,7 +26,7 @@ import org.springframework.boot.configurationsample.ConfigurationProperties;
@ConfigurationProperties(prefix = "hierarchical")
public class HierarchicalProperties extends HierarchicalPropertiesParent {
private String third;
private String third = "three";
public String getThird() {
return this.third;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -23,7 +23,7 @@ package org.springframework.boot.configurationsample.simple;
*/
public abstract class HierarchicalPropertiesGrandparent {
private String first;
private String first = "one";
public String getFirst() {
return this.first;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -24,7 +24,7 @@ package org.springframework.boot.configurationsample.simple;
public abstract class HierarchicalPropertiesParent
extends HierarchicalPropertiesGrandparent {
private String second;
private String second = "two";
public String getSecond() {
return this.second;