Skip to content

Commit fa7af7d

Browse files
Added test for dynamic collection names
Closes #2764
1 parent e219fc9 commit fa7af7d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434

3535
import org.springframework.context.ApplicationContext;
3636
import org.springframework.core.annotation.AliasFor;
37+
import org.springframework.core.env.Environment;
3738
import org.springframework.core.env.StandardEnvironment;
3839
import org.springframework.data.mapping.MappingException;
3940
import org.springframework.data.mongodb.core.query.Collation;
4041
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
4142
import org.springframework.data.spel.spi.EvaluationContextExtension;
4243
import org.springframework.data.util.TypeInformation;
44+
import org.springframework.mock.env.MockEnvironment;
4345

4446
/**
4547
* Unit tests for {@link BasicMongoPersistentEntity}.
@@ -87,6 +89,19 @@ void collectionAllowsReferencingSpringBean() {
8789
assertThat(entity.getCollection()).isEqualTo("otherReference");
8890
}
8991

92+
@Test // GH-2764
93+
void collectionAllowsReferencingProperties() {
94+
95+
MockEnvironment environment = new MockEnvironment();
96+
environment.setProperty("collectionName", "reference");
97+
98+
BasicMongoPersistentEntity<DynamicallyMappedUsingPropertyPlaceholder> entity = new BasicMongoPersistentEntity<>(
99+
TypeInformation.of(DynamicallyMappedUsingPropertyPlaceholder.class));
100+
entity.setEnvironment(environment);
101+
102+
assertThat(entity.getCollection()).isEqualTo("reference_cat");
103+
}
104+
90105
@Test // DATAMONGO-937
91106
void shouldDetectLanguageCorrectly() {
92107

@@ -325,6 +340,9 @@ class Company {}
325340
@Document("#{@myBean.collectionName}")
326341
class DynamicallyMapped {}
327342

343+
@Document("${collectionName}_cat")
344+
class DynamicallyMappedUsingPropertyPlaceholder {}
345+
328346
class CollectionProvider {
329347
String collectionName;
330348

0 commit comments

Comments
 (0)