36
36
import org .springframework .data .mapping .PersistentProperty ;
37
37
import org .springframework .data .mapping .Person ;
38
38
import org .springframework .data .util .ClassTypeInformation ;
39
- import org .springframework .data .util .TypeInformation ;
40
39
import org .springframework .util .ReflectionUtils ;
41
40
42
41
/**
46
45
*/
47
46
public class AbstractPersistentPropertyUnitTests {
48
47
49
- TypeInformation <TestClassComplex > typeInfo ;
50
- PersistentEntity <TestClassComplex , SamplePersistentProperty > entity ;
51
48
SimpleTypeHolder typeHolder ;
52
49
53
50
@ Before
54
51
public void setUp () {
55
-
56
- typeInfo = ClassTypeInformation .from (TestClassComplex .class );
57
- entity = new BasicPersistentEntity <TestClassComplex , SamplePersistentProperty >(typeInfo );
58
52
typeHolder = new SimpleTypeHolder ();
59
53
}
60
54
@@ -64,9 +58,8 @@ public void setUp() {
64
58
@ Test
65
59
public void discoversComponentTypeCorrectly () throws Exception {
66
60
67
- Field field = ReflectionUtils . findField (TestClassComplex .class , "testClassSet" );
61
+ SamplePersistentProperty property = getProperty (TestClassComplex .class , "testClassSet" );
68
62
69
- SamplePersistentProperty property = new SamplePersistentProperty (field , null , entity , typeHolder );
70
63
property .getComponentType ();
71
64
}
72
65
@@ -76,9 +69,8 @@ public void discoversComponentTypeCorrectly() throws Exception {
76
69
@ Test
77
70
public void returnsNestedEntityTypeCorrectly () {
78
71
79
- Field field = ReflectionUtils . findField (TestClassComplex .class , "testClassSet" );
72
+ SamplePersistentProperty property = getProperty (TestClassComplex .class , "testClassSet" , null );
80
73
81
- SamplePersistentProperty property = new SamplePersistentProperty (field , null , entity , typeHolder );
82
74
assertThat (property .getPersistentEntityType ().iterator ().hasNext (), is (false ));
83
75
}
84
76
@@ -88,8 +80,8 @@ public void returnsNestedEntityTypeCorrectly() {
88
80
@ Test
89
81
public void isEntityWorksForUntypedMaps () throws Exception {
90
82
91
- Field field = ReflectionUtils . findField (TestClassComplex .class , "map" );
92
- SamplePersistentProperty property = new SamplePersistentProperty ( field , null , entity , typeHolder );
83
+ SamplePersistentProperty property = getProperty (TestClassComplex .class , "map" , null );
84
+
93
85
assertThat (property .isEntity (), is (false ));
94
86
}
95
87
@@ -99,8 +91,8 @@ public void isEntityWorksForUntypedMaps() throws Exception {
99
91
@ Test
100
92
public void isEntityWorksForUntypedCollection () throws Exception {
101
93
102
- Field field = ReflectionUtils . findField (TestClassComplex .class , "collection" );
103
- SamplePersistentProperty property = new SamplePersistentProperty ( field , null , entity , typeHolder );
94
+ SamplePersistentProperty property = getProperty (TestClassComplex .class , "collection" , null );
95
+
104
96
assertThat (property .isEntity (), is (false ));
105
97
}
106
98
@@ -110,11 +102,8 @@ public void isEntityWorksForUntypedCollection() throws Exception {
110
102
@ Test
111
103
public void considersPropertiesEqualIfFieldEquals () {
112
104
113
- Field first = ReflectionUtils .findField (FirstConcrete .class , "genericField" );
114
- Field second = ReflectionUtils .findField (SecondConcrete .class , "genericField" );
115
-
116
- SamplePersistentProperty firstProperty = new SamplePersistentProperty (first , null , entity , typeHolder );
117
- SamplePersistentProperty secondProperty = new SamplePersistentProperty (second , null , entity , typeHolder );
105
+ SamplePersistentProperty firstProperty = getProperty (FirstConcrete .class , "genericField" , null );
106
+ SamplePersistentProperty secondProperty = getProperty (SecondConcrete .class , "genericField" , null );
118
107
119
108
assertThat (firstProperty , is (secondProperty ));
120
109
assertThat (firstProperty .hashCode (), is (secondProperty .hashCode ()));
@@ -126,9 +115,8 @@ public void considersPropertiesEqualIfFieldEquals() {
126
115
@ Test
127
116
public void doesNotConsiderJavaTransientFieldsTransient () {
128
117
129
- Field transientField = ReflectionUtils . findField (TestClassComplex .class , "transientField" );
118
+ PersistentProperty <?> property = getProperty (TestClassComplex .class , "transientField" , null );
130
119
131
- PersistentProperty <?> property = new SamplePersistentProperty (transientField , null , entity , typeHolder );
132
120
assertThat (property .isTransient (), is (false ));
133
121
}
134
122
@@ -138,9 +126,7 @@ public void doesNotConsiderJavaTransientFieldsTransient() {
138
126
@ Test
139
127
public void findsSimpleGettersAndASetters () {
140
128
141
- Field field = ReflectionUtils .findField (AccessorTestClass .class , "id" );
142
- PersistentProperty <SamplePersistentProperty > property = new SamplePersistentProperty (field , getPropertyDescriptor (
143
- AccessorTestClass .class , "id" ), entity , typeHolder );
129
+ PersistentProperty <SamplePersistentProperty > property = getProperty (AccessorTestClass .class , "id" );
144
130
145
131
assertThat (property .getGetter (), is (notNullValue ()));
146
132
assertThat (property .getSetter (), is (notNullValue ()));
@@ -152,9 +138,7 @@ public void findsSimpleGettersAndASetters() {
152
138
@ Test
153
139
public void doesNotUseInvalidGettersAndASetters () {
154
140
155
- Field field = ReflectionUtils .findField (AccessorTestClass .class , "anotherId" );
156
- PersistentProperty <SamplePersistentProperty > property = new SamplePersistentProperty (field , getPropertyDescriptor (
157
- AccessorTestClass .class , "anotherId" ), entity , typeHolder );
141
+ PersistentProperty <SamplePersistentProperty > property = getProperty (AccessorTestClass .class , "anotherId" );
158
142
159
143
assertThat (property .getGetter (), is (nullValue ()));
160
144
assertThat (property .getSetter (), is (nullValue ()));
@@ -166,9 +150,7 @@ public void doesNotUseInvalidGettersAndASetters() {
166
150
@ Test
167
151
public void usesCustomGetter () {
168
152
169
- Field field = ReflectionUtils .findField (AccessorTestClass .class , "yetAnotherId" );
170
- PersistentProperty <SamplePersistentProperty > property = new SamplePersistentProperty (field , getPropertyDescriptor (
171
- AccessorTestClass .class , "yetAnotherId" ), entity , typeHolder );
153
+ PersistentProperty <SamplePersistentProperty > property = getProperty (AccessorTestClass .class , "yetAnotherId" );
172
154
173
155
assertThat (property .getGetter (), is (notNullValue ()));
174
156
assertThat (property .getSetter (), is (nullValue ()));
@@ -180,9 +162,8 @@ public void usesCustomGetter() {
180
162
@ Test
181
163
public void usesCustomSetter () {
182
164
183
- Field field = ReflectionUtils .findField (AccessorTestClass .class , "yetYetAnotherId" );
184
- PersistentProperty <SamplePersistentProperty > property = new SamplePersistentProperty (field , getPropertyDescriptor (
185
- AccessorTestClass .class , "yetYetAnotherId" ), entity , typeHolder );
165
+ PersistentProperty <SamplePersistentProperty > property = getProperty (AccessorTestClass .class , "yetAnotherId" ,
166
+ getPropertyDescriptor (AccessorTestClass .class , "yetYetAnotherId" ));
186
167
187
168
assertThat (property .getGetter (), is (nullValue ()));
188
169
assertThat (property .getSetter (), is (notNullValue ()));
@@ -194,9 +175,7 @@ public void usesCustomSetter() {
194
175
@ Test
195
176
public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven () {
196
177
197
- Field field = ReflectionUtils .findField (AccessorTestClass .class , "id" );
198
- PersistentProperty <SamplePersistentProperty > property = new SamplePersistentProperty (field , null , entity ,
199
- typeHolder );
178
+ PersistentProperty <SamplePersistentProperty > property = getProperty (AccessorTestClass .class , "id" , null );
200
179
201
180
assertThat (property .getGetter (), is (nullValue ()));
202
181
assertThat (property .getSetter (), is (nullValue ()));
@@ -272,13 +251,25 @@ public void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() {
272
251
assertThat (property .isEntity (), is (false ));
273
252
}
274
253
254
+ @ Test // DATACMNS-1139
255
+ public void resolvesGenericsForRawType () {
256
+
257
+ SamplePersistentProperty property = getProperty (FirstConcrete .class , "genericField" );
258
+
259
+ assertThat (property .getRawType (), is (typeCompatibleWith (String .class )));
260
+ }
261
+
275
262
private <T > SamplePersistentProperty getProperty (Class <T > type , String name ) {
263
+ return getProperty (type , name , getPropertyDescriptor (type , name ));
264
+ }
265
+
266
+ private <T > SamplePersistentProperty getProperty (Class <T > type , String name , PropertyDescriptor descriptor ) {
276
267
277
268
BasicPersistentEntity <T , SamplePersistentProperty > entity = new BasicPersistentEntity <T , SamplePersistentProperty >(
278
269
ClassTypeInformation .from (type ));
279
270
280
271
Field field = ReflectionUtils .findField (type , name );
281
- return new SamplePersistentProperty (field , null , entity , typeHolder );
272
+ return new SamplePersistentProperty (field , descriptor , entity , typeHolder );
282
273
}
283
274
284
275
private static PropertyDescriptor getPropertyDescriptor (Class <?> type , String propertyName ) {
0 commit comments