@@ -68,43 +68,43 @@ void setUp() {
68
68
69
69
@ Test // DATACMNS-68
70
70
void discoversComponentTypeCorrectly () {
71
- assertThat (getProperty (TestClassComplex .class , "testClassSet" ).getComponentType ()).isEqualTo (Object .class );
71
+ assertThat (createProperty (TestClassComplex .class , "testClassSet" ).getComponentType ()).isEqualTo (Object .class );
72
72
}
73
73
74
74
@ Test // DATACMNS-101
75
75
void returnsNestedEntityTypeCorrectly () {
76
- assertThat (getProperty (TestClassComplex .class , "testClassSet" ).getPersistentEntityTypes ()).isEmpty ();
76
+ assertThat (createProperty (TestClassComplex .class , "testClassSet" ).getPersistentEntityTypes ()).isEmpty ();
77
77
}
78
78
79
79
@ Test // DATACMNS-132
80
80
void isEntityWorksForUntypedMaps () {
81
- assertThat (getProperty (TestClassComplex .class , "map" ).isEntity ()).isFalse ();
81
+ assertThat (createProperty (TestClassComplex .class , "map" ).isEntity ()).isFalse ();
82
82
}
83
83
84
84
@ Test // DATACMNS-132
85
85
void isEntityWorksForUntypedCollection () {
86
- assertThat (getProperty (TestClassComplex .class , "collection" ).isEntity ()).isFalse ();
86
+ assertThat (createProperty (TestClassComplex .class , "collection" ).isEntity ()).isFalse ();
87
87
}
88
88
89
89
@ Test // DATACMNS-121
90
90
void considersPropertiesEqualIfFieldEquals () {
91
91
92
- SamplePersistentProperty firstProperty = getProperty (FirstConcrete .class , "genericField" );
93
- SamplePersistentProperty secondProperty = getProperty (SecondConcrete .class , "genericField" );
92
+ SamplePersistentProperty firstProperty = createProperty (FirstConcrete .class , "genericField" );
93
+ SamplePersistentProperty secondProperty = createProperty (SecondConcrete .class , "genericField" );
94
94
95
95
assertThat (firstProperty ).isEqualTo (secondProperty );
96
96
assertThat (firstProperty .hashCode ()).isEqualTo (secondProperty .hashCode ());
97
97
}
98
98
99
99
@ Test // DATACMNS-180
100
100
void doesNotConsiderJavaTransientFieldsTransient () {
101
- assertThat (getProperty (TestClassComplex .class , "transientField" ).isTransient ()).isFalse ();
101
+ assertThat (createProperty (TestClassComplex .class , "transientField" ).isTransient ()).isFalse ();
102
102
}
103
103
104
104
@ Test // DATACMNS-206
105
105
void findsSimpleGettersAndASetters () {
106
106
107
- SamplePersistentProperty property = getProperty (AccessorTestClass .class , "id" );
107
+ SamplePersistentProperty property = createProperty (AccessorTestClass .class , "id" );
108
108
109
109
assertThat (property .getGetter ()).isNotNull ();
110
110
assertThat (property .getSetter ()).isNotNull ();
@@ -113,7 +113,7 @@ void findsSimpleGettersAndASetters() {
113
113
@ Test // DATACMNS-206
114
114
void doesNotUseInvalidGettersAndASetters () {
115
115
116
- SamplePersistentProperty property = getProperty (AccessorTestClass .class , "anotherId" );
116
+ SamplePersistentProperty property = createProperty (AccessorTestClass .class , "anotherId" );
117
117
118
118
assertThat (property .getGetter ()).isNull ();
119
119
assertThat (property .getSetter ()).isNull ();
@@ -122,7 +122,7 @@ void doesNotUseInvalidGettersAndASetters() {
122
122
@ Test // DATACMNS-206
123
123
void usesCustomGetter () {
124
124
125
- SamplePersistentProperty property = getProperty (AccessorTestClass .class , "yetAnotherId" );
125
+ SamplePersistentProperty property = createProperty (AccessorTestClass .class , "yetAnotherId" );
126
126
127
127
assertThat (property .getGetter ()).isNotNull ();
128
128
assertThat (property .getSetter ()).isNull ();
@@ -131,7 +131,7 @@ void usesCustomGetter() {
131
131
@ Test // DATACMNS-206
132
132
void usesCustomSetter () {
133
133
134
- SamplePersistentProperty property = getProperty (AccessorTestClass .class , "yetYetAnotherId" );
134
+ SamplePersistentProperty property = createProperty (AccessorTestClass .class , "yetYetAnotherId" );
135
135
136
136
assertThat (property .getGetter ()).isNull ();
137
137
assertThat (property .getSetter ()).isNotNull ();
@@ -153,83 +153,100 @@ void doesNotDiscoverGetterAndSetterIfNoPropertyDescriptorGiven() {
153
153
@ Test // DATACMNS-337
154
154
void resolvesActualType () {
155
155
156
- SamplePersistentProperty property = getProperty (Sample .class , "person" );
156
+ SamplePersistentProperty property = createProperty (Sample .class , "person" );
157
157
assertThat (property .getActualType ()).isEqualTo (Person .class );
158
158
159
- property = getProperty (Sample .class , "persons" );
159
+ property = createProperty (Sample .class , "persons" );
160
160
assertThat (property .getActualType ()).isEqualTo (Person .class );
161
161
162
- property = getProperty (Sample .class , "personArray" );
162
+ property = createProperty (Sample .class , "personArray" );
163
163
assertThat (property .getActualType ()).isEqualTo (Person .class );
164
164
165
- property = getProperty (Sample .class , "personMap" );
165
+ property = createProperty (Sample .class , "personMap" );
166
166
assertThat (property .getActualType ()).isEqualTo (Person .class );
167
167
}
168
168
169
169
@ Test // DATACMNS-462
170
170
void considersCollectionPropertyEntitiesIfComponentTypeIsEntity () {
171
171
172
- SamplePersistentProperty property = getProperty (Sample .class , "persons" );
172
+ SamplePersistentProperty property = createProperty (Sample .class , "persons" );
173
173
assertThat (property .isEntity ()).isTrue ();
174
174
}
175
175
176
176
@ Test // DATACMNS-462
177
177
void considersMapPropertyEntitiesIfValueTypeIsEntity () {
178
178
179
- SamplePersistentProperty property = getProperty (Sample .class , "personMap" );
179
+ SamplePersistentProperty property = createProperty (Sample .class , "personMap" );
180
180
assertThat (property .isEntity ()).isTrue ();
181
181
}
182
182
183
183
@ Test // DATACMNS-462
184
184
void considersArrayPropertyEntitiesIfComponentTypeIsEntity () {
185
185
186
- SamplePersistentProperty property = getProperty (Sample .class , "personArray" );
186
+ SamplePersistentProperty property = createProperty (Sample .class , "personArray" );
187
187
assertThat (property .isEntity ()).isTrue ();
188
188
}
189
189
190
190
@ Test // DATACMNS-462
191
191
void considersCollectionPropertySimpleIfComponentTypeIsSimple () {
192
192
193
- SamplePersistentProperty property = getProperty (Sample .class , "strings" );
193
+ SamplePersistentProperty property = createProperty (Sample .class , "strings" );
194
194
assertThat (property .isEntity ()).isFalse ();
195
195
}
196
196
197
197
@ Test // DATACMNS-562
198
198
void doesNotConsiderPropertyWithTreeMapMapValueAnEntity () {
199
199
200
- SamplePersistentProperty property = getProperty (TreeMapWrapper .class , "map" );
200
+ SamplePersistentProperty property = createProperty (TreeMapWrapper .class , "map" );
201
201
assertThat (property .getPersistentEntityTypes ()).isEmpty ();
202
202
assertThat (property .isEntity ()).isFalse ();
203
203
}
204
204
205
205
@ Test // DATACMNS-867
206
206
void resolvesFieldNameWithUnderscoresCorrectly () {
207
207
208
- SamplePersistentProperty property = getProperty (TestClassComplex .class , "var_name_with_underscores" );
208
+ SamplePersistentProperty property = createProperty (TestClassComplex .class , "var_name_with_underscores" );
209
209
assertThat (property .getName ()).isEqualTo ("var_name_with_underscores" );
210
210
}
211
211
212
212
@ Test // DATACMNS-1139
213
213
void resolvesGenericsForRawType () {
214
214
215
- SamplePersistentProperty property = getProperty (FirstConcrete .class , "genericField" );
215
+ SamplePersistentProperty property = createProperty (FirstConcrete .class , "genericField" );
216
216
217
217
assertThat (property .getRawType ()).isEqualTo (String .class );
218
218
}
219
219
220
220
@ Test // DATACMNS-1180
221
221
void returnsAccessorsForGenericReturnType () {
222
222
223
- SamplePersistentProperty property = getProperty (ConcreteGetter .class , "genericField" );
223
+ SamplePersistentProperty property = createProperty (ConcreteGetter .class , "genericField" );
224
224
225
225
assertThat (property .getSetter ()).isNotNull ();
226
226
assertThat (property .getGetter ()).isNotNull ();
227
227
}
228
228
229
+ @ Test // GH-2315
230
+ void detectsImplicitTargetTypeForAssociations () {
231
+
232
+ SamplePersistentProperty property = new SamplePersistentProperty (getProperty (Sample .class , "person" ),
233
+ getEntity (Sample .class ), typeHolder ) {
234
+ @ Override
235
+ public boolean isAssociation () {
236
+ return true ;
237
+ }
238
+ };
239
+
240
+ assertThat (property .getAssociationTargetType ()).isEqualTo (Person .class );
241
+
242
+ property = createProperty (Sample .class , "person" );
243
+ assertThat (property .getAssociationTargetType ()).isNull ();
244
+ }
245
+
229
246
@ Test // GH-2315
230
247
void detectsJMoleculesAssociation () {
231
248
232
- SamplePersistentProperty property = getProperty (JMolecules .class , "association" );
249
+ SamplePersistentProperty property = createProperty (JMolecules .class , "association" );
233
250
234
251
assertThat (property .isAssociation ()).isTrue ();
235
252
assertThat (property .getAssociationTargetType ()).isEqualTo (JMoleculesAggregate .class );
@@ -239,19 +256,24 @@ private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T
239
256
return new BasicPersistentEntity <>(ClassTypeInformation .from (type ));
240
257
}
241
258
242
- private <T > SamplePersistentProperty getProperty (Class <T > type , String name ) {
259
+ private <T > SamplePersistentProperty createProperty (Class <T > type , String name ) {
260
+
261
+ Property property = getProperty (type , name );
262
+
263
+ return new SamplePersistentProperty (property , getEntity (type ), typeHolder );
264
+ }
265
+
266
+ private <T > Property getProperty (Class <T > type , String name ) {
243
267
244
268
TypeInformation <?> typeInformation = ClassTypeInformation .from (type );
245
269
Optional <Field > field = Optional .ofNullable (ReflectionUtils .findField (type , name ));
246
270
Optional <PropertyDescriptor > descriptor = getPropertyDescriptor (type , name );
247
271
248
- Property property = Optionals .firstNonEmpty ( //
272
+ return Optionals .firstNonEmpty ( //
249
273
() -> Optionals .mapIfAllPresent (field , descriptor , (left , right ) -> Property .of (typeInformation , left , right )), //
250
274
() -> field .map (it -> Property .of (typeInformation , it )), //
251
275
() -> descriptor .map (it -> Property .of (typeInformation , it ))) //
252
276
.orElseThrow (() -> new IllegalArgumentException (String .format ("Couldn't find property %s on %s!" , name , type )));
253
-
254
- return new SamplePersistentProperty (property , getEntity (type ), typeHolder );
255
277
}
256
278
257
279
private static Optional <PropertyDescriptor > getPropertyDescriptor (Class <?> type , String propertyName ) {
0 commit comments