28
28
29
29
import org .junit .jupiter .api .Test ;
30
30
import org .junit .jupiter .api .extension .ExtendWith ;
31
+ import org .junitpioneer .jupiter .SetSystemProperty ;
31
32
import org .mockito .Mock ;
32
33
import org .mockito .junit .jupiter .MockitoExtension ;
33
34
34
35
import org .springframework .context .ApplicationContext ;
35
36
import org .springframework .core .annotation .AliasFor ;
37
+ import org .springframework .core .env .StandardEnvironment ;
36
38
import org .springframework .data .mapping .MappingException ;
37
39
import org .springframework .data .mongodb .core .query .Collation ;
38
40
import org .springframework .data .spel .ExtensionAwareEvaluationContextProvider ;
39
41
import org .springframework .data .spel .spi .EvaluationContextExtension ;
40
- import org .springframework .data .util .ClassTypeInformation ;
42
+ import org .springframework .data .util .TypeInformation ;
41
43
42
44
/**
43
45
* Unit tests for {@link BasicMongoPersistentEntity}.
@@ -56,14 +58,14 @@ public class BasicMongoPersistentEntityUnitTests {
56
58
void subclassInheritsAtDocumentAnnotation () {
57
59
58
60
BasicMongoPersistentEntity <Person > entity = new BasicMongoPersistentEntity <>(
59
- ClassTypeInformation . from (Person .class ));
61
+ TypeInformation . of (Person .class ));
60
62
assertThat (entity .getCollection ()).isEqualTo ("contacts" );
61
63
}
62
64
63
65
@ Test
64
66
void evaluatesSpELExpression () {
65
67
66
- MongoPersistentEntity <Company > entity = new BasicMongoPersistentEntity <>(ClassTypeInformation . from (Company .class ));
68
+ MongoPersistentEntity <Company > entity = new BasicMongoPersistentEntity <>(TypeInformation . of (Company .class ));
67
69
assertThat (entity .getCollection ()).isEqualTo ("35" );
68
70
}
69
71
@@ -76,7 +78,7 @@ void collectionAllowsReferencingSpringBean() {
76
78
when (context .getBean ("myBean" )).thenReturn (provider );
77
79
78
80
BasicMongoPersistentEntity <DynamicallyMapped > entity = new BasicMongoPersistentEntity <>(
79
- ClassTypeInformation . from (DynamicallyMapped .class ));
81
+ TypeInformation . of (DynamicallyMapped .class ));
80
82
entity .setEvaluationContextProvider (new ExtensionAwareEvaluationContextProvider (context ));
81
83
82
84
assertThat (entity .getCollection ()).isEqualTo ("reference" );
@@ -89,7 +91,7 @@ void collectionAllowsReferencingSpringBean() {
89
91
void shouldDetectLanguageCorrectly () {
90
92
91
93
BasicMongoPersistentEntity <DocumentWithLanguage > entity = new BasicMongoPersistentEntity <>(
92
- ClassTypeInformation . from (DocumentWithLanguage .class ));
94
+ TypeInformation . of (DocumentWithLanguage .class ));
93
95
94
96
assertThat (entity .getLanguage ()).isEqualTo ("spanish" );
95
97
}
@@ -101,7 +103,7 @@ void verifyShouldThrowExceptionForInvalidTypeOfExplicitLanguageProperty() {
101
103
doReturn (Number .class ).when (propertyMock ).getActualType ();
102
104
103
105
BasicMongoPersistentEntity <AnyDocument > entity = new BasicMongoPersistentEntity <>(
104
- ClassTypeInformation . from (AnyDocument .class ));
106
+ TypeInformation . of (AnyDocument .class ));
105
107
entity .addPersistentProperty (propertyMock );
106
108
107
109
assertThatExceptionOfType (MappingException .class ).isThrownBy (entity ::verify );
@@ -114,7 +116,7 @@ void verifyShouldPassForStringAsExplicitLanguageProperty() {
114
116
doReturn (String .class ).when (propertyMock ).getActualType ();
115
117
116
118
BasicMongoPersistentEntity <AnyDocument > entity = new BasicMongoPersistentEntity <>(
117
- ClassTypeInformation . from (AnyDocument .class ));
119
+ TypeInformation . of (AnyDocument .class ));
118
120
entity .addPersistentProperty (propertyMock );
119
121
120
122
entity .verify ();
@@ -127,7 +129,7 @@ void verifyShouldPassForStringAsExplicitLanguageProperty() {
127
129
void verifyShouldIgnoreNonExplicitLanguageProperty () {
128
130
129
131
BasicMongoPersistentEntity <AnyDocument > entity = new BasicMongoPersistentEntity <>(
130
- ClassTypeInformation . from (AnyDocument .class ));
132
+ TypeInformation . of (AnyDocument .class ));
131
133
when (propertyMock .isExplicitLanguageProperty ()).thenReturn (false );
132
134
entity .addPersistentProperty (propertyMock );
133
135
@@ -149,7 +151,7 @@ void verifyShouldThrowErrorForLazyDBRefOnFinalClass() {
149
151
doReturn (true ).when (dbRefMock ).lazy ();
150
152
151
153
BasicMongoPersistentEntity <AnyDocument > entity = new BasicMongoPersistentEntity <>(
152
- ClassTypeInformation . from (AnyDocument .class ));
154
+ TypeInformation . of (AnyDocument .class ));
153
155
entity .addPersistentProperty (propertyMock );
154
156
155
157
assertThatExceptionOfType (MappingException .class ).isThrownBy (entity ::verify );
@@ -167,7 +169,7 @@ void verifyShouldThrowErrorForLazyDBRefArray() {
167
169
doReturn (true ).when (dbRefMock ).lazy ();
168
170
169
171
BasicMongoPersistentEntity <AnyDocument > entity = new BasicMongoPersistentEntity <>(
170
- ClassTypeInformation . from (AnyDocument .class ));
172
+ TypeInformation . of (AnyDocument .class ));
171
173
entity .addPersistentProperty (propertyMock );
172
174
173
175
assertThatExceptionOfType (MappingException .class ).isThrownBy (entity ::verify );
@@ -185,7 +187,7 @@ void verifyShouldPassForLazyDBRefOnNonArrayNonFinalClass() {
185
187
doReturn (true ).when (dbRefMock ).lazy ();
186
188
187
189
BasicMongoPersistentEntity <AnyDocument > entity = new BasicMongoPersistentEntity <>(
188
- ClassTypeInformation . from (AnyDocument .class ));
190
+ TypeInformation . of (AnyDocument .class ));
189
191
entity .addPersistentProperty (propertyMock );
190
192
entity .verify ();
191
193
@@ -203,7 +205,7 @@ void verifyShouldPassForNonLazyDBRefOnFinalClass() {
203
205
doReturn (false ).when (dbRefMock ).lazy ();
204
206
205
207
BasicMongoPersistentEntity <AnyDocument > entity = new BasicMongoPersistentEntity <>(
206
- ClassTypeInformation . from (AnyDocument .class ));
208
+ TypeInformation . of (AnyDocument .class ));
207
209
entity .addPersistentProperty (propertyMock );
208
210
entity .verify ();
209
211
@@ -214,7 +216,7 @@ void verifyShouldPassForNonLazyDBRefOnFinalClass() {
214
216
void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation () {
215
217
216
218
BasicMongoPersistentEntity <DocumentWithCustomAnnotation > entity = new BasicMongoPersistentEntity <>(
217
- ClassTypeInformation . from (DocumentWithCustomAnnotation .class ));
219
+ TypeInformation . of (DocumentWithCustomAnnotation .class ));
218
220
219
221
assertThat (entity .getCollection ()).isEqualTo ("collection-1" );
220
222
}
@@ -223,7 +225,7 @@ void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation() {
223
225
void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation () {
224
226
225
227
BasicMongoPersistentEntity <DocumentWithComposedAnnotation > entity = new BasicMongoPersistentEntity <>(
226
- ClassTypeInformation . from (DocumentWithComposedAnnotation .class ));
228
+ TypeInformation . of (DocumentWithComposedAnnotation .class ));
227
229
228
230
assertThat (entity .getCollection ()).isEqualTo ("custom-collection" );
229
231
}
@@ -232,18 +234,29 @@ void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation() {
232
234
void usesEvaluationContextExtensionInDynamicDocumentName () {
233
235
234
236
BasicMongoPersistentEntity <MappedWithExtension > entity = new BasicMongoPersistentEntity <>(
235
- ClassTypeInformation . from (MappedWithExtension .class ));
237
+ TypeInformation . of (MappedWithExtension .class ));
236
238
entity .setEvaluationContextProvider (
237
239
new ExtensionAwareEvaluationContextProvider (Collections .singletonList (new SampleExtension ())));
238
240
239
241
assertThat (entity .getCollection ()).isEqualTo ("collectionName" );
240
242
}
241
243
244
+ @ Test // GH-4634
245
+ @ SetSystemProperty (key = "mongo.entity.collection" , value = "collectionName" )
246
+ void readsCollectionNameFromSystemProperty () {
247
+
248
+ BasicMongoPersistentEntity <MappedWithExtensionPropertyPlaceholderStyle > entity = new BasicMongoPersistentEntity <>(
249
+ TypeInformation .of (MappedWithExtensionPropertyPlaceholderStyle .class ));
250
+ entity .setEnvironment (new StandardEnvironment ());
251
+
252
+ assertThat (entity .getCollection ()).isEqualTo ("collectionName" );
253
+ }
254
+
242
255
@ Test // DATAMONGO-1854
243
256
void readsSimpleCollation () {
244
257
245
258
BasicMongoPersistentEntity <WithSimpleCollation > entity = new BasicMongoPersistentEntity <>(
246
- ClassTypeInformation . from (WithSimpleCollation .class ));
259
+ TypeInformation . of (WithSimpleCollation .class ));
247
260
248
261
assertThat (entity .getCollation ()).isEqualTo (org .springframework .data .mongodb .core .query .Collation .of ("en_US" ));
249
262
}
@@ -252,7 +265,7 @@ void readsSimpleCollation() {
252
265
void readsDocumentCollation () {
253
266
254
267
BasicMongoPersistentEntity <WithDocumentCollation > entity = new BasicMongoPersistentEntity <>(
255
- ClassTypeInformation . from (WithDocumentCollation .class ));
268
+ TypeInformation . of (WithDocumentCollation .class ));
256
269
257
270
assertThat (entity .getCollation ()).isEqualTo (org .springframework .data .mongodb .core .query .Collation .of ("en_US" ));
258
271
}
@@ -261,7 +274,7 @@ void readsDocumentCollation() {
261
274
void usesCorrectExpressionsForCollectionAndCollation () {
262
275
263
276
BasicMongoPersistentEntity <WithCollectionAndCollationFromSpEL > entity = new BasicMongoPersistentEntity <>(
264
- ClassTypeInformation . from (WithCollectionAndCollationFromSpEL .class ));
277
+ TypeInformation . of (WithCollectionAndCollationFromSpEL .class ));
265
278
entity .setEvaluationContextProvider (
266
279
new ExtensionAwareEvaluationContextProvider (Collections .singletonList (new SampleExtension ())));
267
280
@@ -298,7 +311,7 @@ void readsMultiShardKey() {
298
311
}
299
312
300
313
static <T > BasicMongoPersistentEntity <T > entityOf (Class <T > type ) {
301
- return new BasicMongoPersistentEntity <>(ClassTypeInformation . from (type ));
314
+ return new BasicMongoPersistentEntity <>(TypeInformation . of (type ));
302
315
}
303
316
304
317
@ Document ("contacts" )
@@ -350,6 +363,9 @@ private static class DocumentWithComposedAnnotation {}
350
363
@ Document ("#{myProperty}" )
351
364
class MappedWithExtension {}
352
365
366
+ @ Document ("${mongo.entity.collection}" )
367
+ class MappedWithExtensionPropertyPlaceholderStyle {}
368
+
353
369
@ Document ("${value.from.file}" )
354
370
class MappedWithValue {}
355
371
0 commit comments