23
23
import java .lang .annotation .Retention ;
24
24
import java .lang .annotation .RetentionPolicy ;
25
25
import java .lang .annotation .Target ;
26
+ import java .util .Map ;
27
+ import java .util .concurrent .ConcurrentMap ;
26
28
27
29
import org .junit .Before ;
28
30
import org .junit .Test ;
29
31
import org .springframework .data .annotation .Id ;
30
32
import org .springframework .data .mapping .context .SampleMappingContext ;
31
33
import org .springframework .data .mapping .context .SamplePersistentProperty ;
34
+ import org .springframework .data .util .ClassTypeInformation ;
35
+ import org .springframework .data .util .TypeInformation ;
36
+ import org .springframework .test .util .ReflectionTestUtils ;
32
37
33
38
/**
34
39
* @author Oliver Gierke
35
40
*/
36
41
public class AbstractAnnotationBasedPropertyUnitTests <P extends AnnotationBasedPersistentProperty <P >> {
37
42
38
43
BasicPersistentEntity <Object , SamplePersistentProperty > entity ;
44
+ SampleMappingContext context ;
39
45
40
46
@ Before
41
47
public void setUp () {
42
48
43
- SampleMappingContext context = new SampleMappingContext ();
49
+ context = new SampleMappingContext ();
44
50
entity = context .getPersistentEntity (Sample .class );
45
51
}
46
52
@@ -72,6 +78,47 @@ public void findsMetaAnnotation() {
72
78
assertAnnotationPresent (Id .class , entity .getPersistentProperty ("id" ));
73
79
}
74
80
81
+ /**
82
+ * @see DATACMNS-282
83
+ */
84
+ @ Test
85
+ public void populatesAnnotationCacheWithDirectAnnotationsOnCreation () {
86
+
87
+ SamplePersistentProperty property = entity .getPersistentProperty ("meta" );
88
+
89
+ // Assert direct annotations are cached on construction
90
+ Map <Class <? extends Annotation >, Annotation > cache = getAnnotationCache (property );
91
+ assertThat (cache .containsKey (MyAnnotationAsMeta .class ), is (true ));
92
+ assertThat (cache .containsKey (MyAnnotation .class ), is (false ));
93
+
94
+ // Assert meta annotation is found and cached
95
+ MyAnnotation annotation = property .findAnnotation (MyAnnotation .class );
96
+ assertThat (annotation , is (notNullValue ()));
97
+ assertThat (cache .containsKey (MyAnnotation .class ), is (true ));
98
+ }
99
+
100
+ /**
101
+ * @see DATACMNS-282
102
+ */
103
+ @ Test
104
+ @ SuppressWarnings ("unchecked" )
105
+ public void discoversAmbiguousMappingUsingDirectAnnotationsOnAccessors () {
106
+
107
+ try {
108
+ context .getPersistentEntity (InvalidSample .class );
109
+ fail ("Expected MappingException!" );
110
+ } catch (MappingException o_O ) {
111
+ ConcurrentMap <TypeInformation <?>, ?> entities = (ConcurrentMap <TypeInformation <?>, ?>) ReflectionTestUtils
112
+ .getField (context , "persistentEntities" );
113
+ assertThat (entities .containsKey (ClassTypeInformation .from (InvalidSample .class )), is (false ));
114
+ }
115
+ }
116
+
117
+ @ SuppressWarnings ("unchecked" )
118
+ private Map <Class <? extends Annotation >, Annotation > getAnnotationCache (SamplePersistentProperty property ) {
119
+ return (Map <Class <? extends Annotation >, Annotation >) ReflectionTestUtils .getField (property , "annotationCache" );
120
+ }
121
+
75
122
private <A extends Annotation > A assertAnnotationPresent (Class <A > annotationType ,
76
123
AnnotationBasedPersistentProperty <?> property ) {
77
124
@@ -90,6 +137,9 @@ static class Sample {
90
137
String getter ;
91
138
String setter ;
92
139
140
+ @ MyAnnotationAsMeta
141
+ String meta ;
142
+
93
143
@ MyAnnotation ("field" )
94
144
String override ;
95
145
@@ -109,12 +159,34 @@ public String getOverride() {
109
159
}
110
160
}
111
161
162
+ static class InvalidSample {
163
+
164
+ String meta ;
165
+
166
+ @ MyAnnotation
167
+ public String getMeta () {
168
+ return meta ;
169
+ }
170
+
171
+ @ MyAnnotation
172
+ public void setMeta (String meta ) {
173
+ this .meta = meta ;
174
+ }
175
+ }
176
+
112
177
@ Retention (RetentionPolicy .RUNTIME )
113
178
@ Target (value = { FIELD , METHOD , ANNOTATION_TYPE })
114
179
public static @interface MyAnnotation {
115
180
String value () default "" ;
116
181
}
117
182
183
+ @ Retention (RetentionPolicy .RUNTIME )
184
+ @ Target (value = { FIELD , METHOD })
185
+ @ MyAnnotation
186
+ public static @interface MyAnnotationAsMeta {
187
+
188
+ }
189
+
118
190
@ Retention (RetentionPolicy .RUNTIME )
119
191
@ Target (value = { FIELD , METHOD , ANNOTATION_TYPE })
120
192
@ Id
0 commit comments