Skip to content

Commit fe7c5f1

Browse files
Polishing.
Reduce method visibility, Remove unused variable. Add test and move off deprecated code in test. Original Pull Request: #4635
1 parent 8670d2d commit fe7c5f1

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefProxyHandler.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.springframework.data.mapping.PersistentPropertyAccessor;
2323
import org.springframework.data.mapping.context.MappingContext;
24-
import org.springframework.data.mapping.model.SpELContext;
2524
import org.springframework.data.mapping.model.ValueExpressionEvaluator;
2625
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2726
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
@@ -36,21 +35,17 @@
3635
*/
3736
class DefaultDbRefProxyHandler implements DbRefProxyHandler {
3837

39-
private final SpELContext spELContext;
4038
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
4139
private final ValueResolver resolver;
4240
private final Function<Object, ValueExpressionEvaluator> evaluatorFactory;
4341

4442
/**
45-
* @param spELContext must not be {@literal null}.
4643
* @param mappingContext must not be {@literal null}.
4744
* @param resolver must not be {@literal null}.
4845
*/
49-
public DefaultDbRefProxyHandler(SpELContext spELContext,
50-
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext,
46+
public DefaultDbRefProxyHandler(MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext,
5147
ValueResolver resolver, Function<Object, ValueExpressionEvaluator> evaluatorFactory) {
5248

53-
this.spELContext = spELContext;
5449
this.mappingContext = mappingContext;
5550
this.resolver = resolver;
5651
this.evaluatorFactory = evaluatorFactory;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolverCallback.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DefaultDbRefResolverCallback implements DbRefResolverCallback {
4343
* @param evaluator must not be {@literal null}.
4444
* @param resolver must not be {@literal null}.
4545
*/
46-
public DefaultDbRefResolverCallback(Bson surroundingObject, ObjectPath path, ValueExpressionEvaluator evaluator,
46+
DefaultDbRefResolverCallback(Bson surroundingObject, ObjectPath path, ValueExpressionEvaluator evaluator,
4747
ValueResolver resolver) {
4848

4949
this.surroundingObject = surroundingObject;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public MappingMongoConverter(DbRefResolver dbRefResolver,
176176
this.idMapper = new QueryMapper(this);
177177

178178
this.spELContext = new SpELContext(DocumentPropertyAccessor.INSTANCE);
179-
this.dbRefProxyHandler = new DefaultDbRefProxyHandler(spELContext, mappingContext,
179+
this.dbRefProxyHandler = new DefaultDbRefProxyHandler(mappingContext,
180180
(prop, bson, evaluator, path) -> {
181181

182182
ConversionContext context = getConversionContext(path);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/json/EvaluationContextExpressionEvaluator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class EvaluationContextExpressionEvaluator implements ValueExpressionEvaluator {
3434
final ValueProvider valueProvider;
3535
final ExpressionParser expressionParser;
3636

37-
public EvaluationContextExpressionEvaluator(ValueProvider valueProvider, ExpressionParser expressionParser) {
37+
EvaluationContextExpressionEvaluator(ValueProvider valueProvider, ExpressionParser expressionParser) {
3838

3939
this.valueProvider = valueProvider;
4040
this.expressionParser = expressionParser;
@@ -46,16 +46,16 @@ public <T> T evaluate(String expression) {
4646
return evaluateExpression(expression, Collections.emptyMap());
4747
}
4848

49-
public EvaluationContext getEvaluationContext(String expressionString) {
49+
EvaluationContext getEvaluationContext(String expressionString) {
5050
return new StandardEvaluationContext();
5151
}
5252

53-
public Expression getParsedExpression(String expressionString) {
53+
Expression getParsedExpression(String expressionString) {
5454
return expressionParser.parseExpression(expressionString);
5555
}
5656

5757
@SuppressWarnings("unchecked")
58-
public <T> T evaluateExpression(String expressionString, Map<String, Object> variables) {
58+
<T> T evaluateExpression(String expressionString, Map<String, Object> variables) {
5959

6060
Expression expression = getParsedExpression(expressionString);
6161
EvaluationContext ctx = getEvaluationContext(expressionString);

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

+35-19
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@
2828

2929
import org.junit.jupiter.api.Test;
3030
import org.junit.jupiter.api.extension.ExtendWith;
31+
import org.junitpioneer.jupiter.SetSystemProperty;
3132
import org.mockito.Mock;
3233
import org.mockito.junit.jupiter.MockitoExtension;
3334

3435
import org.springframework.context.ApplicationContext;
3536
import org.springframework.core.annotation.AliasFor;
37+
import org.springframework.core.env.StandardEnvironment;
3638
import org.springframework.data.mapping.MappingException;
3739
import org.springframework.data.mongodb.core.query.Collation;
3840
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
3941
import org.springframework.data.spel.spi.EvaluationContextExtension;
40-
import org.springframework.data.util.ClassTypeInformation;
42+
import org.springframework.data.util.TypeInformation;
4143

4244
/**
4345
* Unit tests for {@link BasicMongoPersistentEntity}.
@@ -56,14 +58,14 @@ public class BasicMongoPersistentEntityUnitTests {
5658
void subclassInheritsAtDocumentAnnotation() {
5759

5860
BasicMongoPersistentEntity<Person> entity = new BasicMongoPersistentEntity<>(
59-
ClassTypeInformation.from(Person.class));
61+
TypeInformation.of(Person.class));
6062
assertThat(entity.getCollection()).isEqualTo("contacts");
6163
}
6264

6365
@Test
6466
void evaluatesSpELExpression() {
6567

66-
MongoPersistentEntity<Company> entity = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(Company.class));
68+
MongoPersistentEntity<Company> entity = new BasicMongoPersistentEntity<>(TypeInformation.of(Company.class));
6769
assertThat(entity.getCollection()).isEqualTo("35");
6870
}
6971

@@ -76,7 +78,7 @@ void collectionAllowsReferencingSpringBean() {
7678
when(context.getBean("myBean")).thenReturn(provider);
7779

7880
BasicMongoPersistentEntity<DynamicallyMapped> entity = new BasicMongoPersistentEntity<>(
79-
ClassTypeInformation.from(DynamicallyMapped.class));
81+
TypeInformation.of(DynamicallyMapped.class));
8082
entity.setEvaluationContextProvider(new ExtensionAwareEvaluationContextProvider(context));
8183

8284
assertThat(entity.getCollection()).isEqualTo("reference");
@@ -89,7 +91,7 @@ void collectionAllowsReferencingSpringBean() {
8991
void shouldDetectLanguageCorrectly() {
9092

9193
BasicMongoPersistentEntity<DocumentWithLanguage> entity = new BasicMongoPersistentEntity<>(
92-
ClassTypeInformation.from(DocumentWithLanguage.class));
94+
TypeInformation.of(DocumentWithLanguage.class));
9395

9496
assertThat(entity.getLanguage()).isEqualTo("spanish");
9597
}
@@ -101,7 +103,7 @@ void verifyShouldThrowExceptionForInvalidTypeOfExplicitLanguageProperty() {
101103
doReturn(Number.class).when(propertyMock).getActualType();
102104

103105
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>(
104-
ClassTypeInformation.from(AnyDocument.class));
106+
TypeInformation.of(AnyDocument.class));
105107
entity.addPersistentProperty(propertyMock);
106108

107109
assertThatExceptionOfType(MappingException.class).isThrownBy(entity::verify);
@@ -114,7 +116,7 @@ void verifyShouldPassForStringAsExplicitLanguageProperty() {
114116
doReturn(String.class).when(propertyMock).getActualType();
115117

116118
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>(
117-
ClassTypeInformation.from(AnyDocument.class));
119+
TypeInformation.of(AnyDocument.class));
118120
entity.addPersistentProperty(propertyMock);
119121

120122
entity.verify();
@@ -127,7 +129,7 @@ void verifyShouldPassForStringAsExplicitLanguageProperty() {
127129
void verifyShouldIgnoreNonExplicitLanguageProperty() {
128130

129131
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>(
130-
ClassTypeInformation.from(AnyDocument.class));
132+
TypeInformation.of(AnyDocument.class));
131133
when(propertyMock.isExplicitLanguageProperty()).thenReturn(false);
132134
entity.addPersistentProperty(propertyMock);
133135

@@ -149,7 +151,7 @@ void verifyShouldThrowErrorForLazyDBRefOnFinalClass() {
149151
doReturn(true).when(dbRefMock).lazy();
150152

151153
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>(
152-
ClassTypeInformation.from(AnyDocument.class));
154+
TypeInformation.of(AnyDocument.class));
153155
entity.addPersistentProperty(propertyMock);
154156

155157
assertThatExceptionOfType(MappingException.class).isThrownBy(entity::verify);
@@ -167,7 +169,7 @@ void verifyShouldThrowErrorForLazyDBRefArray() {
167169
doReturn(true).when(dbRefMock).lazy();
168170

169171
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>(
170-
ClassTypeInformation.from(AnyDocument.class));
172+
TypeInformation.of(AnyDocument.class));
171173
entity.addPersistentProperty(propertyMock);
172174

173175
assertThatExceptionOfType(MappingException.class).isThrownBy(entity::verify);
@@ -185,7 +187,7 @@ void verifyShouldPassForLazyDBRefOnNonArrayNonFinalClass() {
185187
doReturn(true).when(dbRefMock).lazy();
186188

187189
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>(
188-
ClassTypeInformation.from(AnyDocument.class));
190+
TypeInformation.of(AnyDocument.class));
189191
entity.addPersistentProperty(propertyMock);
190192
entity.verify();
191193

@@ -203,7 +205,7 @@ void verifyShouldPassForNonLazyDBRefOnFinalClass() {
203205
doReturn(false).when(dbRefMock).lazy();
204206

205207
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>(
206-
ClassTypeInformation.from(AnyDocument.class));
208+
TypeInformation.of(AnyDocument.class));
207209
entity.addPersistentProperty(propertyMock);
208210
entity.verify();
209211

@@ -214,7 +216,7 @@ void verifyShouldPassForNonLazyDBRefOnFinalClass() {
214216
void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation() {
215217

216218
BasicMongoPersistentEntity<DocumentWithCustomAnnotation> entity = new BasicMongoPersistentEntity<>(
217-
ClassTypeInformation.from(DocumentWithCustomAnnotation.class));
219+
TypeInformation.of(DocumentWithCustomAnnotation.class));
218220

219221
assertThat(entity.getCollection()).isEqualTo("collection-1");
220222
}
@@ -223,7 +225,7 @@ void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation() {
223225
void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation() {
224226

225227
BasicMongoPersistentEntity<DocumentWithComposedAnnotation> entity = new BasicMongoPersistentEntity<>(
226-
ClassTypeInformation.from(DocumentWithComposedAnnotation.class));
228+
TypeInformation.of(DocumentWithComposedAnnotation.class));
227229

228230
assertThat(entity.getCollection()).isEqualTo("custom-collection");
229231
}
@@ -232,18 +234,29 @@ void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation() {
232234
void usesEvaluationContextExtensionInDynamicDocumentName() {
233235

234236
BasicMongoPersistentEntity<MappedWithExtension> entity = new BasicMongoPersistentEntity<>(
235-
ClassTypeInformation.from(MappedWithExtension.class));
237+
TypeInformation.of(MappedWithExtension.class));
236238
entity.setEvaluationContextProvider(
237239
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
238240

239241
assertThat(entity.getCollection()).isEqualTo("collectionName");
240242
}
241243

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+
242255
@Test // DATAMONGO-1854
243256
void readsSimpleCollation() {
244257

245258
BasicMongoPersistentEntity<WithSimpleCollation> entity = new BasicMongoPersistentEntity<>(
246-
ClassTypeInformation.from(WithSimpleCollation.class));
259+
TypeInformation.of(WithSimpleCollation.class));
247260

248261
assertThat(entity.getCollation()).isEqualTo(org.springframework.data.mongodb.core.query.Collation.of("en_US"));
249262
}
@@ -252,7 +265,7 @@ void readsSimpleCollation() {
252265
void readsDocumentCollation() {
253266

254267
BasicMongoPersistentEntity<WithDocumentCollation> entity = new BasicMongoPersistentEntity<>(
255-
ClassTypeInformation.from(WithDocumentCollation.class));
268+
TypeInformation.of(WithDocumentCollation.class));
256269

257270
assertThat(entity.getCollation()).isEqualTo(org.springframework.data.mongodb.core.query.Collation.of("en_US"));
258271
}
@@ -261,7 +274,7 @@ void readsDocumentCollation() {
261274
void usesCorrectExpressionsForCollectionAndCollation() {
262275

263276
BasicMongoPersistentEntity<WithCollectionAndCollationFromSpEL> entity = new BasicMongoPersistentEntity<>(
264-
ClassTypeInformation.from(WithCollectionAndCollationFromSpEL.class));
277+
TypeInformation.of(WithCollectionAndCollationFromSpEL.class));
265278
entity.setEvaluationContextProvider(
266279
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
267280

@@ -298,7 +311,7 @@ void readsMultiShardKey() {
298311
}
299312

300313
static <T> BasicMongoPersistentEntity<T> entityOf(Class<T> type) {
301-
return new BasicMongoPersistentEntity<>(ClassTypeInformation.from(type));
314+
return new BasicMongoPersistentEntity<>(TypeInformation.of(type));
302315
}
303316

304317
@Document("contacts")
@@ -350,6 +363,9 @@ private static class DocumentWithComposedAnnotation {}
350363
@Document("#{myProperty}")
351364
class MappedWithExtension {}
352365

366+
@Document("${mongo.entity.collection}")
367+
class MappedWithExtensionPropertyPlaceholderStyle {}
368+
353369
@Document("${value.from.file}")
354370
class MappedWithValue {}
355371

0 commit comments

Comments
 (0)