Browse Source
Mapping annotations are now looked up in the following order: getter, setter, field. Made mapping annotations usable on methods as well.pull/20/merge
16 changed files with 224 additions and 111 deletions
@ -0,0 +1,123 @@
@@ -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 { |
||||
} |
||||
} |
||||
Loading…
Reference in new issue