Add @UnmappedPropertyValue support
Update @PropertyMapping support to allow single enum values to be marked as an @UnmappedPropertyValue. This change is primarily so that users can replace "default" values globally for across all tests. See gh-6455
This commit is contained in:
parent
ab9834a92e
commit
8355d8516b
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.boot.test.autoconfigure.properties;
|
package org.springframework.boot.test.autoconfigure.properties;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -113,9 +114,11 @@ public class AnnotationsPropertySource extends EnumerablePropertySource<Class<?>
|
||||||
String name = getName(typeMapping, attributeMapping, attribute);
|
String name = getName(typeMapping, attributeMapping, attribute);
|
||||||
ReflectionUtils.makeAccessible(attribute);
|
ReflectionUtils.makeAccessible(attribute);
|
||||||
Object value = ReflectionUtils.invokeMethod(attribute, annotation);
|
Object value = ReflectionUtils.invokeMethod(attribute, annotation);
|
||||||
|
if (isValueMapped(value)) {
|
||||||
putProperties(name, value, properties);
|
putProperties(name, value, properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isMapped(PropertyMapping typeMapping,
|
private boolean isMapped(PropertyMapping typeMapping,
|
||||||
PropertyMapping attributeMapping) {
|
PropertyMapping attributeMapping) {
|
||||||
|
@ -125,6 +128,17 @@ public class AnnotationsPropertySource extends EnumerablePropertySource<Class<?>
|
||||||
return (typeMapping != null && typeMapping.map());
|
return (typeMapping != null && typeMapping.map());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isValueMapped(Object value) {
|
||||||
|
if (value != null && value instanceof Enum) {
|
||||||
|
Field field = ReflectionUtils.findField(value.getClass(),
|
||||||
|
((Enum<?>) value).name());
|
||||||
|
if (AnnotatedElementUtils.isAnnotated(field, UnmappedPropertyValue.class)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private String getName(PropertyMapping typeMapping, PropertyMapping attributeMapping,
|
private String getName(PropertyMapping typeMapping, PropertyMapping attributeMapping,
|
||||||
Method attribute) {
|
Method attribute) {
|
||||||
String prefix = (typeMapping == null ? "" : typeMapping.value());
|
String prefix = (typeMapping == null ? "" : typeMapping.value());
|
||||||
|
|
|
@ -47,6 +47,7 @@ import org.springframework.test.context.TestPropertySource;
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @since 1.4.0
|
* @since 1.4.0
|
||||||
* @see AnnotationsPropertySource
|
* @see AnnotationsPropertySource
|
||||||
|
* @see UnmappedPropertyValue
|
||||||
* @see TestPropertySource
|
* @see TestPropertySource
|
||||||
*/
|
*/
|
||||||
@Documented
|
@Documented
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2016 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.test.autoconfigure.properties;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that a single value should not be mapped when referenced from a
|
||||||
|
* {@link PropertyMapping @PropertyMapping} annotation.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 1.4.0
|
||||||
|
* @see PropertyMapping
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ ElementType.FIELD })
|
||||||
|
public @interface UnmappedPropertyValue {
|
||||||
|
|
||||||
|
}
|
|
@ -185,6 +185,20 @@ public class AnnotationsPropertySourceTests {
|
||||||
assertThat(source.getProperty("aliasing.value")).isEqualTo("baz");
|
assertThat(source.getProperty("aliasing.value")).isEqualTo("baz");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enumValueMapped() throws Exception {
|
||||||
|
AnnotationsPropertySource source = new AnnotationsPropertySource(
|
||||||
|
EnumValueMapped.class);
|
||||||
|
assertThat(source.getProperty("testenum.value")).isEqualTo(EnumItem.TWO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enumValueNotMapped() throws Exception {
|
||||||
|
AnnotationsPropertySource source = new AnnotationsPropertySource(
|
||||||
|
EnumValueNotMapped.class);
|
||||||
|
assertThat(source.containsProperty("testenum.value")).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
static class NoAnnotation {
|
static class NoAnnotation {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -382,4 +396,32 @@ public class AnnotationsPropertySourceTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EnumAnnotation(EnumItem.TWO)
|
||||||
|
static class EnumValueMapped {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@EnumAnnotation(EnumItem.DEFAULT)
|
||||||
|
static class EnumValueNotMapped {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@PropertyMapping("testenum")
|
||||||
|
static @interface EnumAnnotation {
|
||||||
|
|
||||||
|
EnumItem value();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
enum EnumItem {
|
||||||
|
|
||||||
|
@UnmappedPropertyValue DEFAULT,
|
||||||
|
|
||||||
|
ONE,
|
||||||
|
|
||||||
|
TWO
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue