Browse Source

Polishing.

Reformat code.

See #3660.
Original pull request: #3662.
pull/860/head
Mark Paluch 5 years ago
parent
commit
2b715c54d3
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 35
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 28
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

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

@ -171,8 +171,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Assert.notNull(path, "ObjectPath must not be null"); Assert.notNull(path, "ObjectPath must not be null");
return new ConversionContext(conversions, path, this::readDocument, this::readCollectionOrArray, this::readMap, this::readDBRef, return new ConversionContext(conversions, path, this::readDocument, this::readCollectionOrArray, this::readMap,
this::getPotentiallyConvertedSimpleRead); this::readDBRef, this::getPotentiallyConvertedSimpleRead);
} }
/** /**
@ -527,7 +527,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return; return;
} }
if (property.isDocumentReference() || (!property.isDbReference() && property.findAnnotation(Reference.class) != null)) { if (property.isDocumentReference()
|| (!property.isDbReference() && property.findAnnotation(Reference.class) != null)) {
// quite unusual but sounds like worth having? // quite unusual but sounds like worth having?
@ -595,13 +596,13 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Assert.notNull(referringProperty, "Cannot create DocumentReference. The referringProperty must not be null!"); Assert.notNull(referringProperty, "Cannot create DocumentReference. The referringProperty must not be null!");
if (referringProperty.isDbReference()) { if (referringProperty.isDbReference()) {
return () -> toDBRef(source, referringProperty); return () -> toDBRef(source, referringProperty);
} }
if (referringProperty.isDocumentReference() || referringProperty.findAnnotation(Reference.class) != null) { if (referringProperty.isDocumentReference() || referringProperty.findAnnotation(Reference.class) != null) {
return createDocumentPointer(source, referringProperty); return createDocumentPointer(source, referringProperty);
} }
throw new IllegalArgumentException("The referringProperty is neither a DBRef nor a document reference"); throw new IllegalArgumentException("The referringProperty is neither a DBRef nor a document reference");
} }
@ -612,7 +613,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return () -> source; return () -> source;
} }
if(source instanceof DocumentPointer) { if (source instanceof DocumentPointer) {
return (DocumentPointer<?>) source; return (DocumentPointer<?>) source;
} }
@ -622,7 +623,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
} }
if (ClassUtils.isAssignableValue(referringProperty.getAssociationTargetType(), source)) { if (ClassUtils.isAssignableValue(referringProperty.getAssociationTargetType(), source)) {
return documentPointerFactory.computePointer(mappingContext, referringProperty, source, referringProperty.getActualType()); return documentPointerFactory.computePointer(mappingContext, referringProperty, source,
referringProperty.getActualType());
} }
return () -> source; return () -> source;
@ -1978,9 +1980,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
private final ContainerValueConverter<DBRef> dbRefConverter; private final ContainerValueConverter<DBRef> dbRefConverter;
private final ValueConverter<Object> elementConverter; private final ValueConverter<Object> elementConverter;
ConversionContext(org.springframework.data.convert.CustomConversions customConversions, ObjectPath path, ContainerValueConverter<Bson> documentConverter, ConversionContext(org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
ContainerValueConverter<Collection<?>> collectionConverter, ContainerValueConverter<Bson> mapConverter, ContainerValueConverter<Bson> documentConverter, ContainerValueConverter<Collection<?>> collectionConverter,
ContainerValueConverter<DBRef> dbRefConverter, ValueConverter<Object> elementConverter) { ContainerValueConverter<Bson> mapConverter, ContainerValueConverter<DBRef> dbRefConverter,
ValueConverter<Object> elementConverter) {
this.conversions = customConversions; this.conversions = customConversions;
this.path = path; this.path = path;
@ -2052,8 +2055,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Assert.notNull(currentPath, "ObjectPath must not be null"); Assert.notNull(currentPath, "ObjectPath must not be null");
return new ConversionContext(conversions, currentPath, documentConverter, collectionConverter, mapConverter, dbRefConverter, return new ConversionContext(conversions, currentPath, documentConverter, collectionConverter, mapConverter,
elementConverter); dbRefConverter, elementConverter);
} }
public ObjectPath getPath() { public ObjectPath getPath() {

28
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

@ -30,7 +30,6 @@ import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.*; import java.util.*;
import org.assertj.core.api.Assertions;
import org.bson.types.Code; import org.bson.types.Code;
import org.bson.types.Decimal128; import org.bson.types.Decimal128;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -42,6 +41,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.ConversionNotSupportedException; import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -529,7 +529,7 @@ class MappingMongoConverterUnitTests {
} }
@Test @Test
public void convertsObjectsIfNecessary() { void convertsObjectsIfNecessary() {
ObjectId id = new ObjectId(); ObjectId id = new ObjectId();
assertThat(converter.convertToMongoType(id)).isEqualTo(id); assertThat(converter.convertToMongoType(id)).isEqualTo(id);
@ -2113,21 +2113,21 @@ class MappingMongoConverterUnitTests {
} }
@Test // DATAMONGO-2479 @Test // DATAMONGO-2479
public void entityCallbacksAreNotSetByDefault() { void entityCallbacksAreNotSetByDefault() {
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNull(); assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNull();
} }
@Test // DATAMONGO-2479 @Test // DATAMONGO-2479
public void entityCallbacksShouldBeInitiatedOnSettingApplicationContext() { void entityCallbacksShouldBeInitiatedOnSettingApplicationContext() {
ApplicationContext ctx = new StaticApplicationContext(); ApplicationContext ctx = new StaticApplicationContext();
converter.setApplicationContext(ctx); converter.setApplicationContext(ctx);
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNotNull(); assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNotNull();
} }
@Test // DATAMONGO-2479 @Test // DATAMONGO-2479
public void setterForEntityCallbackOverridesContextInitializedOnes() { void setterForEntityCallbackOverridesContextInitializedOnes() {
ApplicationContext ctx = new StaticApplicationContext(); ApplicationContext ctx = new StaticApplicationContext();
converter.setApplicationContext(ctx); converter.setApplicationContext(ctx);
@ -2135,11 +2135,11 @@ class MappingMongoConverterUnitTests {
EntityCallbacks callbacks = EntityCallbacks.create(); EntityCallbacks callbacks = EntityCallbacks.create();
converter.setEntityCallbacks(callbacks); converter.setEntityCallbacks(callbacks);
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks); assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks);
} }
@Test // DATAMONGO-2479 @Test // DATAMONGO-2479
public void setterForApplicationContextShouldNotOverrideAlreadySetEntityCallbacks() { void setterForApplicationContextShouldNotOverrideAlreadySetEntityCallbacks() {
EntityCallbacks callbacks = EntityCallbacks.create(); EntityCallbacks callbacks = EntityCallbacks.create();
ApplicationContext ctx = new StaticApplicationContext(); ApplicationContext ctx = new StaticApplicationContext();
@ -2147,11 +2147,11 @@ class MappingMongoConverterUnitTests {
converter.setEntityCallbacks(callbacks); converter.setEntityCallbacks(callbacks);
converter.setApplicationContext(ctx); converter.setApplicationContext(ctx);
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks); assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks);
} }
@Test // DATAMONGO-2479 @Test // DATAMONGO-2479
public void resolveDBRefMapValueShouldInvokeCallbacks() { void resolveDBRefMapValueShouldInvokeCallbacks() {
AfterConvertCallback<Person> afterConvertCallback = spy(new ReturningAfterConvertCallback()); AfterConvertCallback<Person> afterConvertCallback = spy(new ReturningAfterConvertCallback());
converter.setEntityCallbacks(EntityCallbacks.create(afterConvertCallback)); converter.setEntityCallbacks(EntityCallbacks.create(afterConvertCallback));
@ -2168,7 +2168,7 @@ class MappingMongoConverterUnitTests {
} }
@Test // DATAMONGO-2300 @Test // DATAMONGO-2300
public void readAndConvertDBRefNestedByMapCorrectly() { void readAndConvertDBRefNestedByMapCorrectly() {
org.bson.Document cluster = new org.bson.Document("_id", 100L); org.bson.Document cluster = new org.bson.Document("_id", 100L);
DBRef dbRef = new DBRef("clusters", 100L); DBRef dbRef = new DBRef("clusters", 100L);
@ -2434,7 +2434,6 @@ class MappingMongoConverterUnitTests {
@Test // GH-3660 @Test // GH-3660
void usesCustomConverterForMapTypesOnWrite() { void usesCustomConverterForMapTypesOnWrite() {
converter = new MappingMongoConverter(resolver, mappingContext); converter = new MappingMongoConverter(resolver, mappingContext);
converter.setCustomConversions(MongoCustomConversions.create(it -> { converter.setCustomConversions(MongoCustomConversions.create(it -> {
it.registerConverter(new TypeImplementingMapToDocumentConverter()); it.registerConverter(new TypeImplementingMapToDocumentConverter());
@ -2521,7 +2520,6 @@ class MappingMongoConverterUnitTests {
assertThat(target.typeImplementingMap).isEqualTo(new TypeImplementingMap("one", 2)); assertThat(target.typeImplementingMap).isEqualTo(new TypeImplementingMap("one", 2));
} }
static class GenericType<T> { static class GenericType<T> {
T content; T content;
} }
@ -3098,7 +3096,7 @@ class MappingMongoConverterUnitTests {
String val1; String val1;
int val2; int val2;
public TypeImplementingMap(String val1, int val2) { TypeImplementingMap(String val1, int val2) {
this.val1 = val1; this.val1 = val1;
this.val2 = val2; this.val2 = val2;
} }

Loading…
Cancel
Save