From 8355d8516b28ef211cf6eb6cf447615b7441fb23 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 25 Jul 2016 12:23:17 -0700 Subject: [PATCH] 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 --- .../properties/AnnotationsPropertySource.java | 16 ++++++- .../properties/PropertyMapping.java | 1 + .../properties/UnmappedPropertyValue.java | 36 ++++++++++++++++ .../AnnotationsPropertySourceTests.java | 42 +++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/UnmappedPropertyValue.java diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java index 8e907ca57e..1dea0bde5d 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java @@ -17,6 +17,7 @@ package org.springframework.boot.test.autoconfigure.properties; import java.lang.annotation.Annotation; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; @@ -113,7 +114,9 @@ public class AnnotationsPropertySource extends EnumerablePropertySource String name = getName(typeMapping, attributeMapping, attribute); ReflectionUtils.makeAccessible(attribute); Object value = ReflectionUtils.invokeMethod(attribute, annotation); - putProperties(name, value, properties); + if (isValueMapped(value)) { + putProperties(name, value, properties); + } } } @@ -125,6 +128,17 @@ public class AnnotationsPropertySource extends EnumerablePropertySource 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, Method attribute) { String prefix = (typeMapping == null ? "" : typeMapping.value()); diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/PropertyMapping.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/PropertyMapping.java index e2b374c42e..817749d2bc 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/PropertyMapping.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/PropertyMapping.java @@ -47,6 +47,7 @@ import org.springframework.test.context.TestPropertySource; * @author Phillip Webb * @since 1.4.0 * @see AnnotationsPropertySource + * @see UnmappedPropertyValue * @see TestPropertySource */ @Documented diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/UnmappedPropertyValue.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/UnmappedPropertyValue.java new file mode 100644 index 0000000000..59125660ad --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/UnmappedPropertyValue.java @@ -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 { + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java index 1b1cc96c84..183d8589b3 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java @@ -185,6 +185,20 @@ public class AnnotationsPropertySourceTests { 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 { } @@ -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 + + } + }