DATACMNS-269 - Add support for mapping annotations on accessor methods.
Mapping annotations are now looked up in the following order: getter, setter, field. Made mapping annotations usable on methods as well.
This commit is contained in:
@@ -24,10 +24,9 @@ import java.util.Collections;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.TypeAlias;
|
||||
import org.springframework.data.mapping.MappingMetadataTests;
|
||||
import org.springframework.data.mapping.MappingMetadataTests.SampleMappingContext;
|
||||
import org.springframework.data.mapping.MappingMetadataTests.SampleProperty;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
@@ -43,7 +42,7 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mappingContext = new MappingMetadataTests.SampleMappingContext();
|
||||
mappingContext = new SampleMappingContext();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -65,7 +64,7 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
@Test
|
||||
public void extractsAliasForUnknownType() {
|
||||
|
||||
SampleMappingContext mappingContext = new MappingMetadataTests.SampleMappingContext();
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
mappingContext.initialize();
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
@@ -76,7 +75,7 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
@Test
|
||||
public void doesNotReturnTypeAliasForSimpleType() {
|
||||
|
||||
SampleMappingContext mappingContext = new MappingMetadataTests.SampleMappingContext();
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
mappingContext.initialize();
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
@@ -87,13 +86,13 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void detectsTypeForUnknownEntity() {
|
||||
|
||||
SampleMappingContext mappingContext = new MappingMetadataTests.SampleMappingContext();
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
mappingContext.initialize();
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
assertThat(mapper.resolveTypeFrom("foo"), is(nullValue()));
|
||||
|
||||
PersistentEntity<?, SampleProperty> entity = mappingContext.getPersistentEntity(Entity.class);
|
||||
PersistentEntity<?, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Entity.class);
|
||||
|
||||
assertThat(entity, is(notNullValue()));
|
||||
assertThat(mapper.resolveTypeFrom("foo"), is((TypeInformation) from(Entity.class)));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
* Copyright 2011-2013 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.
|
||||
@@ -17,18 +17,12 @@ package org.springframework.data.mapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.context.AbstractMappingContext;
|
||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.MutablePersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
|
||||
/**
|
||||
* Integration tests for Mapping metadata.
|
||||
@@ -51,7 +45,7 @@ public class MappingMetadataTests {
|
||||
ctx.setInitialEntitySet(Collections.singleton(PersonWithId.class));
|
||||
ctx.initialize();
|
||||
|
||||
PersistentEntity<?, SampleProperty> person = ctx.getPersistentEntity(PersonWithId.class);
|
||||
PersistentEntity<?, SamplePersistentProperty> person = ctx.getPersistentEntity(PersonWithId.class);
|
||||
assertNotNull(person.getIdProperty());
|
||||
assertEquals(String.class, person.getIdProperty().getType());
|
||||
}
|
||||
@@ -62,46 +56,11 @@ public class MappingMetadataTests {
|
||||
ctx.setInitialEntitySet(Collections.singleton(PersonWithChildren.class));
|
||||
ctx.initialize();
|
||||
|
||||
PersistentEntity<?, SampleProperty> person = ctx.getPersistentEntity(PersonWithChildren.class);
|
||||
person.doWithAssociations(new AssociationHandler<MappingMetadataTests.SampleProperty>() {
|
||||
public void doWithAssociation(Association<SampleProperty> association) {
|
||||
PersistentEntity<?, SamplePersistentProperty> person = ctx.getPersistentEntity(PersonWithChildren.class);
|
||||
person.doWithAssociations(new AssociationHandler<SamplePersistentProperty>() {
|
||||
public void doWithAssociation(Association<SamplePersistentProperty> association) {
|
||||
assertEquals(Child.class, association.getInverse().getComponentType());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public interface SampleProperty extends PersistentProperty<SampleProperty> {
|
||||
}
|
||||
|
||||
public static class SampleMappingContext extends
|
||||
AbstractMappingContext<MutablePersistentEntity<?, SampleProperty>, SampleProperty> {
|
||||
|
||||
@Override
|
||||
protected <T> MutablePersistentEntity<?, SampleProperty> createPersistentEntity(TypeInformation<T> typeInformation) {
|
||||
|
||||
return new BasicPersistentEntity<T, MappingMetadataTests.SampleProperty>(typeInformation);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SampleProperty createPersistentProperty(Field field, PropertyDescriptor descriptor,
|
||||
MutablePersistentEntity<?, SampleProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
return new SamplePropertyImpl(field, descriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
}
|
||||
|
||||
public static class SamplePropertyImpl extends AnnotationBasedPersistentProperty<SampleProperty> implements
|
||||
SampleProperty {
|
||||
|
||||
public SamplePropertyImpl(Field field, PropertyDescriptor propertyDescriptor,
|
||||
PersistentEntity<?, SampleProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Association<SampleProperty> createAssociation() {
|
||||
|
||||
return new Association<MappingMetadataTests.SampleProperty>(this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
class SampleMappingContext extends
|
||||
public class SampleMappingContext extends
|
||||
AbstractMappingContext<BasicPersistentEntity<Object, SamplePersistentProperty>, SamplePersistentProperty> {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
|
||||
class SamplePersistentProperty extends AnnotationBasedPersistentProperty<SamplePersistentProperty> {
|
||||
public class SamplePersistentProperty extends AnnotationBasedPersistentProperty<SamplePersistentProperty> {
|
||||
|
||||
public SamplePersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
|
||||
BasicPersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2013 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.data.mapping.model;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AbstractAnnotationBasedPropertyUnitTests<P extends AnnotationBasedPersistentProperty<P>> {
|
||||
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> entity;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
entity = context.getPersistentEntity(Sample.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversAnnotationOnField() {
|
||||
assertAnnotationPresent(MyAnnotation.class, entity.getPersistentProperty("field"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversAnnotationOnGetters() {
|
||||
assertAnnotationPresent(MyAnnotation.class, entity.getPersistentProperty("getter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversAnnotationOnSetters() {
|
||||
assertAnnotationPresent(MyAnnotation.class, entity.getPersistentProperty("setter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefersAnnotationOnMethodsToOverride() {
|
||||
MyAnnotation annotation = assertAnnotationPresent(MyAnnotation.class, entity.getPersistentProperty("override"));
|
||||
assertThat(annotation.value(), is("method"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsMetaAnnotation() {
|
||||
|
||||
assertAnnotationPresent(MyId.class, entity.getPersistentProperty("id"));
|
||||
assertAnnotationPresent(Id.class, entity.getPersistentProperty("id"));
|
||||
}
|
||||
|
||||
private <A extends Annotation> A assertAnnotationPresent(Class<A> annotationType,
|
||||
AnnotationBasedPersistentProperty<?> property) {
|
||||
|
||||
A annotation = property.findAnnotation(annotationType);
|
||||
assertThat(annotation, is(notNullValue()));
|
||||
return annotation;
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
@MyId
|
||||
String id;
|
||||
|
||||
@MyAnnotation
|
||||
String field;
|
||||
String getter;
|
||||
String setter;
|
||||
|
||||
@MyAnnotation("field")
|
||||
String override;
|
||||
|
||||
@MyAnnotation
|
||||
public String getGetter() {
|
||||
return getter;
|
||||
}
|
||||
|
||||
@MyAnnotation
|
||||
public void setSetter(String setter) {
|
||||
this.setter = setter;
|
||||
}
|
||||
|
||||
@MyAnnotation("method")
|
||||
public String getOverride() {
|
||||
return override;
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(value = { FIELD, METHOD, ANNOTATION_TYPE })
|
||||
public static @interface MyAnnotation {
|
||||
String value() default "";
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(value = { FIELD, METHOD, ANNOTATION_TYPE })
|
||||
@Id
|
||||
public static @interface MyId {
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-2013 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 @@ import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Version;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.data.mapping.MappingMetadataTests.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.model.MappingContextIsNewStrategyFactory.PropertyIsNullIsNewStrategy;
|
||||
import org.springframework.data.mapping.model.MappingContextIsNewStrategyFactory.PropertyIsNullOrZeroNumberIsNewStrategy;
|
||||
import org.springframework.data.support.IsNewStrategy;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except
|
||||
@@ -33,8 +33,8 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.mapping.MappingMetadataTests.SampleMappingContext;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
Reference in New Issue
Block a user