|
|
|
@ -16,13 +16,8 @@ |
|
|
|
package org.springframework.data.mongodb.core.convert; |
|
|
|
package org.springframework.data.mongodb.core.convert; |
|
|
|
|
|
|
|
|
|
|
|
import static java.time.ZoneId.*; |
|
|
|
import static java.time.ZoneId.*; |
|
|
|
import static org.assertj.core.api.Assertions.*; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.*; |
|
|
|
import static org.hamcrest.Matchers.*; |
|
|
|
import static org.hamcrest.Matchers.not; |
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.assertThat; |
|
|
|
|
|
|
|
import static org.junit.Assert.fail; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*; |
|
|
|
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*; |
|
|
|
|
|
|
|
|
|
|
|
@ -1144,8 +1139,8 @@ public class MappingMongoConverterUnitTests { |
|
|
|
@Test // DATAMONGO-592
|
|
|
|
@Test // DATAMONGO-592
|
|
|
|
public void recursivelyConvertsSpELReadValue() { |
|
|
|
public void recursivelyConvertsSpELReadValue() { |
|
|
|
|
|
|
|
|
|
|
|
DBObject input = (DBObject) JSON |
|
|
|
DBObject input = (DBObject) JSON.parse( |
|
|
|
.parse("{ \"_id\" : { \"$oid\" : \"50ca271c4566a2b08f2d667a\" }, \"_class\" : \"com.recorder.TestRecorder2$ObjectContainer\", \"property\" : { \"property\" : 100 } }"); |
|
|
|
"{ \"_id\" : { \"$oid\" : \"50ca271c4566a2b08f2d667a\" }, \"_class\" : \"com.recorder.TestRecorder2$ObjectContainer\", \"property\" : { \"property\" : 100 } }"); |
|
|
|
|
|
|
|
|
|
|
|
converter.read(ObjectContainer.class, input); |
|
|
|
converter.read(ObjectContainer.class, input); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -1434,11 +1429,10 @@ public class MappingMongoConverterUnitTests { |
|
|
|
|
|
|
|
|
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo.get("circle"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat(dbo.get("circle"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat( |
|
|
|
assertThat(dbo.get("circle"), |
|
|
|
dbo.get("circle"), |
|
|
|
is((Object) new BasicDBObject("center", |
|
|
|
is((Object) new BasicDBObject("center", new BasicDBObject("x", circle.getCenter().getX()).append("y", circle |
|
|
|
new BasicDBObject("x", circle.getCenter().getX()).append("y", circle.getCenter().getY())) |
|
|
|
.getCenter().getY())).append("radius", radius.getNormalizedValue()).append("metric", |
|
|
|
.append("radius", radius.getNormalizedValue()).append("metric", radius.getMetric().toString()))); |
|
|
|
radius.getMetric().toString()))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@ -1469,11 +1463,10 @@ public class MappingMongoConverterUnitTests { |
|
|
|
|
|
|
|
|
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo.get("sphere"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat(dbo.get("sphere"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat( |
|
|
|
assertThat(dbo.get("sphere"), |
|
|
|
dbo.get("sphere"), |
|
|
|
is((Object) new BasicDBObject("center", |
|
|
|
is((Object) new BasicDBObject("center", new BasicDBObject("x", sphere.getCenter().getX()).append("y", sphere |
|
|
|
new BasicDBObject("x", sphere.getCenter().getX()).append("y", sphere.getCenter().getY())) |
|
|
|
.getCenter().getY())).append("radius", radius.getNormalizedValue()).append("metric", |
|
|
|
.append("radius", radius.getNormalizedValue()).append("metric", radius.getMetric().toString()))); |
|
|
|
radius.getMetric().toString()))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@ -1489,11 +1482,10 @@ public class MappingMongoConverterUnitTests { |
|
|
|
|
|
|
|
|
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo.get("sphere"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat(dbo.get("sphere"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat( |
|
|
|
assertThat(dbo.get("sphere"), |
|
|
|
dbo.get("sphere"), |
|
|
|
is((Object) new BasicDBObject("center", |
|
|
|
is((Object) new BasicDBObject("center", new BasicDBObject("x", sphere.getCenter().getX()).append("y", sphere |
|
|
|
new BasicDBObject("x", sphere.getCenter().getX()).append("y", sphere.getCenter().getY())) |
|
|
|
.getCenter().getY())).append("radius", radius.getNormalizedValue()).append("metric", |
|
|
|
.append("radius", radius.getNormalizedValue()).append("metric", radius.getMetric().toString()))); |
|
|
|
radius.getMetric().toString()))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@ -1524,11 +1516,10 @@ public class MappingMongoConverterUnitTests { |
|
|
|
|
|
|
|
|
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo, is(notNullValue())); |
|
|
|
assertThat(dbo.get("shape"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat(dbo.get("shape"), is(instanceOf(DBObject.class))); |
|
|
|
assertThat( |
|
|
|
assertThat(dbo.get("shape"), |
|
|
|
dbo.get("shape"), |
|
|
|
is((Object) new BasicDBObject("center", |
|
|
|
is((Object) new BasicDBObject("center", new BasicDBObject("x", sphere.getCenter().getX()).append("y", sphere |
|
|
|
new BasicDBObject("x", sphere.getCenter().getX()).append("y", sphere.getCenter().getY())) |
|
|
|
.getCenter().getY())).append("radius", radius.getNormalizedValue()).append("metric", |
|
|
|
.append("radius", radius.getNormalizedValue()).append("metric", radius.getMetric().toString()))); |
|
|
|
radius.getMetric().toString()))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@Test // DATAMONGO-858
|
|
|
|
@ -1563,8 +1554,8 @@ public class MappingMongoConverterUnitTests { |
|
|
|
@Test // DATAMONGO-976
|
|
|
|
@Test // DATAMONGO-976
|
|
|
|
public void shouldIncludeTextScorePropertyWhenReading() { |
|
|
|
public void shouldIncludeTextScorePropertyWhenReading() { |
|
|
|
|
|
|
|
|
|
|
|
ClassWithTextScoreProperty entity = converter |
|
|
|
ClassWithTextScoreProperty entity = converter.read(ClassWithTextScoreProperty.class, |
|
|
|
.read(ClassWithTextScoreProperty.class, new BasicDBObject("score", 5F)); |
|
|
|
new BasicDBObject("score", 5F)); |
|
|
|
assertThat(entity.score, equalTo(5F)); |
|
|
|
assertThat(entity.score, equalTo(5F)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -1643,8 +1634,8 @@ public class MappingMongoConverterUnitTests { |
|
|
|
@Test // DATAMONGO-1050
|
|
|
|
@Test // DATAMONGO-1050
|
|
|
|
public void readShouldUseExplicitFieldnameForIdPropertyWhenAnnotated() { |
|
|
|
public void readShouldUseExplicitFieldnameForIdPropertyWhenAnnotated() { |
|
|
|
|
|
|
|
|
|
|
|
DBObject source = new BasicDBObjectBuilder().add("_id", "rootId") |
|
|
|
DBObject source = new BasicDBObjectBuilder().add("_id", "rootId").add("nested", new BasicDBObject("id", "nestedId")) |
|
|
|
.add("nested", new BasicDBObject("id", "nestedId")).get(); |
|
|
|
.get(); |
|
|
|
|
|
|
|
|
|
|
|
RootForClassWithExplicitlyRenamedIdField sink = converter.read(RootForClassWithExplicitlyRenamedIdField.class, |
|
|
|
RootForClassWithExplicitlyRenamedIdField sink = converter.read(RootForClassWithExplicitlyRenamedIdField.class, |
|
|
|
source); |
|
|
|
source); |
|
|
|
@ -1734,8 +1725,8 @@ public class MappingMongoConverterUnitTests { |
|
|
|
public void convertsMapKeyUsingCustomConverterForAndBackwards() { |
|
|
|
public void convertsMapKeyUsingCustomConverterForAndBackwards() { |
|
|
|
|
|
|
|
|
|
|
|
MappingMongoConverter converter = new MappingMongoConverter(resolver, mappingContext); |
|
|
|
MappingMongoConverter converter = new MappingMongoConverter(resolver, mappingContext); |
|
|
|
converter.setCustomConversions(new CustomConversions(Arrays.asList(new FooBarEnumToStringConverter(), |
|
|
|
converter.setCustomConversions( |
|
|
|
new StringToFooNumConverter()))); |
|
|
|
new CustomConversions(Arrays.asList(new FooBarEnumToStringConverter(), new StringToFooNumConverter()))); |
|
|
|
converter.afterPropertiesSet(); |
|
|
|
converter.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
ClassWithMapUsingEnumAsKey source = new ClassWithMapUsingEnumAsKey(); |
|
|
|
ClassWithMapUsingEnumAsKey source = new ClassWithMapUsingEnumAsKey(); |
|
|
|
@ -1824,32 +1815,33 @@ public class MappingMongoConverterUnitTests { |
|
|
|
@Test // DATAMONGO-1898
|
|
|
|
@Test // DATAMONGO-1898
|
|
|
|
public void writesInterfaceBackedEnumsToSimpleNameByDefault() { |
|
|
|
public void writesInterfaceBackedEnumsToSimpleNameByDefault() { |
|
|
|
|
|
|
|
|
|
|
|
org.bson.Document document = new org.bson.Document(); |
|
|
|
DBObject document = new BasicDBObject(); |
|
|
|
|
|
|
|
|
|
|
|
DocWithInterfacedEnum source = new DocWithInterfacedEnum(); |
|
|
|
DocWithInterfacedEnum source = new DocWithInterfacedEnum(); |
|
|
|
source.property = InterfacedEnum.INSTANCE; |
|
|
|
source.property = InterfacedEnum.INSTANCE; |
|
|
|
|
|
|
|
|
|
|
|
converter.write(source, document); |
|
|
|
converter.write(source, document); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(document) //
|
|
|
|
assertThat(document.keySet(), hasSize(2)); |
|
|
|
.hasSize(2) //
|
|
|
|
assertThat(document.get("_class"), is(notNullValue())); |
|
|
|
.hasEntrySatisfying("_class", __ -> {}) //
|
|
|
|
assertThat(document.get("property"), is((Object) InterfacedEnum.INSTANCE.name())); |
|
|
|
.hasEntrySatisfying("property", value -> InterfacedEnum.INSTANCE.name().equals(value)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-1898
|
|
|
|
@Test // DATAMONGO-1898
|
|
|
|
public void rejectsConversionFromStringToEnumBackedInterface() { |
|
|
|
public void rejectsConversionFromStringToEnumBackedInterface() { |
|
|
|
|
|
|
|
|
|
|
|
org.bson.Document document = new org.bson.Document("property", InterfacedEnum.INSTANCE.name()); |
|
|
|
DBObject document = new BasicDBObject("property", InterfacedEnum.INSTANCE.name()); |
|
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(ConverterNotFoundException.class) //
|
|
|
|
exception.expect(ConverterNotFoundException.class); |
|
|
|
.isThrownBy(() -> converter.read(DocWithInterfacedEnum.class, document)); |
|
|
|
|
|
|
|
|
|
|
|
converter.read(DocWithInterfacedEnum.class, document); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-1898
|
|
|
|
@Test // DATAMONGO-1898
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public void readsInterfacedEnumIfConverterIsRegistered() { |
|
|
|
public void readsInterfacedEnumIfConverterIsRegistered() { |
|
|
|
|
|
|
|
|
|
|
|
org.bson.Document document = new org.bson.Document("property", InterfacedEnum.INSTANCE.name()); |
|
|
|
DBObject document = new BasicDBObject("property", InterfacedEnum.INSTANCE.name()); |
|
|
|
|
|
|
|
|
|
|
|
Converter<String, SomeInterface> enumConverter = new Converter<String, SomeInterface>() { |
|
|
|
Converter<String, SomeInterface> enumConverter = new Converter<String, SomeInterface>() { |
|
|
|
|
|
|
|
|
|
|
|
@ -1859,12 +1851,12 @@ public class MappingMongoConverterUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
converter.setCustomConversions(new MongoCustomConversions(Arrays.asList(enumConverter))); |
|
|
|
converter.setCustomConversions(new CustomConversions(Arrays.asList(enumConverter))); |
|
|
|
converter.afterPropertiesSet(); |
|
|
|
converter.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
DocWithInterfacedEnum result = converter.read(DocWithInterfacedEnum.class, document); |
|
|
|
DocWithInterfacedEnum result = converter.read(DocWithInterfacedEnum.class, document); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(result.property).isEqualTo(InterfacedEnum.INSTANCE); |
|
|
|
assertThat(result.property, is((Object) InterfacedEnum.INSTANCE)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static class GenericType<T> { |
|
|
|
static class GenericType<T> { |
|
|
|
@ -1888,7 +1880,7 @@ public class MappingMongoConverterUnitTests { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void method() { |
|
|
|
void method() { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
abstract void method(); |
|
|
|
abstract void method(); |
|
|
|
@ -2119,8 +2111,8 @@ public class MappingMongoConverterUnitTests { |
|
|
|
|
|
|
|
|
|
|
|
class ClassWithExplicitlyNamedDBRefProperty { |
|
|
|
class ClassWithExplicitlyNamedDBRefProperty { |
|
|
|
|
|
|
|
|
|
|
|
@Field("explict-name-for-db-ref")//
|
|
|
|
@Field("explict-name-for-db-ref") //
|
|
|
|
@org.springframework.data.mongodb.core.mapping.DBRef//
|
|
|
|
@org.springframework.data.mongodb.core.mapping.DBRef //
|
|
|
|
ClassWithIntId dbRefProperty; |
|
|
|
ClassWithIntId dbRefProperty; |
|
|
|
|
|
|
|
|
|
|
|
public ClassWithIntId getDbRefProperty() { |
|
|
|
public ClassWithIntId getDbRefProperty() { |
|
|
|
|