|
|
|
|
@ -22,7 +22,7 @@ import java.lang.annotation.ElementType;
@@ -22,7 +22,7 @@ import java.lang.annotation.ElementType;
|
|
|
|
|
import java.lang.annotation.Retention; |
|
|
|
|
import java.lang.annotation.RetentionPolicy; |
|
|
|
|
import java.lang.annotation.Target; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
@ -33,6 +33,7 @@ import org.mockito.junit.MockitoJUnitRunner;
@@ -33,6 +33,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
|
import org.springframework.core.annotation.AliasFor; |
|
|
|
|
import org.springframework.data.mapping.MappingException; |
|
|
|
|
import org.springframework.data.mongodb.core.query.Collation; |
|
|
|
|
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider; |
|
|
|
|
import org.springframework.data.spel.spi.EvaluationContextExtension; |
|
|
|
|
import org.springframework.data.util.ClassTypeInformation; |
|
|
|
|
@ -42,6 +43,7 @@ import org.springframework.data.util.ClassTypeInformation;
@@ -42,6 +43,7 @@ import org.springframework.data.util.ClassTypeInformation;
|
|
|
|
|
* |
|
|
|
|
* @author Oliver Gierke |
|
|
|
|
* @author Christoph Strobl |
|
|
|
|
* @author Mark Paluch |
|
|
|
|
*/ |
|
|
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
|
|
public class BasicMongoPersistentEntityUnitTests { |
|
|
|
|
@ -52,7 +54,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -52,7 +54,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
@Test |
|
|
|
|
public void subclassInheritsAtDocumentAnnotation() { |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<Person> entity = new BasicMongoPersistentEntity<Person>( |
|
|
|
|
BasicMongoPersistentEntity<Person> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(Person.class)); |
|
|
|
|
assertThat(entity.getCollection()).isEqualTo("contacts"); |
|
|
|
|
} |
|
|
|
|
@ -60,7 +62,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -60,7 +62,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
@Test |
|
|
|
|
public void evaluatesSpELExpression() { |
|
|
|
|
|
|
|
|
|
MongoPersistentEntity<Company> entity = new BasicMongoPersistentEntity<Company>( |
|
|
|
|
MongoPersistentEntity<Company> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(Company.class)); |
|
|
|
|
assertThat(entity.getCollection()).isEqualTo("35"); |
|
|
|
|
} |
|
|
|
|
@ -73,7 +75,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -73,7 +75,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
|
|
|
|
|
when(context.getBean("myBean")).thenReturn(provider); |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<DynamicallyMapped> entity = new BasicMongoPersistentEntity<DynamicallyMapped>( |
|
|
|
|
BasicMongoPersistentEntity<DynamicallyMapped> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(DynamicallyMapped.class)); |
|
|
|
|
entity.setEvaluationContextProvider(new ExtensionAwareEvaluationContextProvider(context)); |
|
|
|
|
|
|
|
|
|
@ -86,7 +88,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -86,7 +88,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
@Test // DATAMONGO-937
|
|
|
|
|
public void shouldDetectLanguageCorrectly() { |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<DocumentWithLanguage> entity = new BasicMongoPersistentEntity<DocumentWithLanguage>( |
|
|
|
|
BasicMongoPersistentEntity<DocumentWithLanguage> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(DocumentWithLanguage.class)); |
|
|
|
|
|
|
|
|
|
assertThat(entity.getLanguage()).isEqualTo("spanish"); |
|
|
|
|
@ -98,11 +100,11 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -98,11 +100,11 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
doReturn(true).when(propertyMock).isExplicitLanguageProperty(); |
|
|
|
|
doReturn(Number.class).when(propertyMock).getActualType(); |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(AnyDocument.class)); |
|
|
|
|
entity.addPersistentProperty(propertyMock); |
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> entity.verify()); |
|
|
|
|
assertThatExceptionOfType(MappingException.class).isThrownBy(entity::verify); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-1053
|
|
|
|
|
@ -111,7 +113,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -111,7 +113,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
doReturn(true).when(propertyMock).isExplicitLanguageProperty(); |
|
|
|
|
doReturn(String.class).when(propertyMock).getActualType(); |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(AnyDocument.class)); |
|
|
|
|
entity.addPersistentProperty(propertyMock); |
|
|
|
|
|
|
|
|
|
@ -124,7 +126,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -124,7 +126,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
@Test // DATAMONGO-1053
|
|
|
|
|
public void verifyShouldIgnoreNonExplicitLanguageProperty() { |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(AnyDocument.class)); |
|
|
|
|
when(propertyMock.isExplicitLanguageProperty()).thenReturn(false); |
|
|
|
|
entity.addPersistentProperty(propertyMock); |
|
|
|
|
@ -146,11 +148,11 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -146,11 +148,11 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
doReturn(dbRefMock).when(propertyMock).getDBRef(); |
|
|
|
|
doReturn(true).when(dbRefMock).lazy(); |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(AnyDocument.class)); |
|
|
|
|
entity.addPersistentProperty(propertyMock); |
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> entity.verify()); |
|
|
|
|
assertThatExceptionOfType(MappingException.class).isThrownBy(entity::verify); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-1157
|
|
|
|
|
@ -164,11 +166,11 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -164,11 +166,11 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
doReturn(dbRefMock).when(propertyMock).getDBRef(); |
|
|
|
|
doReturn(true).when(dbRefMock).lazy(); |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(AnyDocument.class)); |
|
|
|
|
entity.addPersistentProperty(propertyMock); |
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> entity.verify()); |
|
|
|
|
assertThatExceptionOfType(MappingException.class).isThrownBy(entity::verify); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-1157
|
|
|
|
|
@ -182,7 +184,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -182,7 +184,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
doReturn(dbRefMock).when(propertyMock).getDBRef(); |
|
|
|
|
doReturn(true).when(dbRefMock).lazy(); |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(AnyDocument.class)); |
|
|
|
|
entity.addPersistentProperty(propertyMock); |
|
|
|
|
entity.verify(); |
|
|
|
|
@ -200,7 +202,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -200,7 +202,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
doReturn(dbRefMock).when(propertyMock).getDBRef(); |
|
|
|
|
doReturn(false).when(dbRefMock).lazy(); |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
|
|
|
|
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(AnyDocument.class)); |
|
|
|
|
entity.addPersistentProperty(propertyMock); |
|
|
|
|
entity.verify(); |
|
|
|
|
@ -211,7 +213,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -211,7 +213,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
@Test // DATAMONGO-1291
|
|
|
|
|
public void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation() { |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<DocumentWithCustomAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithCustomAnnotation>( |
|
|
|
|
BasicMongoPersistentEntity<DocumentWithCustomAnnotation> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(DocumentWithCustomAnnotation.class)); |
|
|
|
|
|
|
|
|
|
assertThat(entity.getCollection()).isEqualTo("collection-1"); |
|
|
|
|
@ -220,7 +222,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -220,7 +222,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
@Test // DATAMONGO-1373
|
|
|
|
|
public void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation() { |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<DocumentWithComposedAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithComposedAnnotation>( |
|
|
|
|
BasicMongoPersistentEntity<DocumentWithComposedAnnotation> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(DocumentWithComposedAnnotation.class)); |
|
|
|
|
|
|
|
|
|
assertThat(entity.getCollection()).isEqualTo("custom-collection"); |
|
|
|
|
@ -232,7 +234,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -232,7 +234,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
BasicMongoPersistentEntity<MappedWithExtension> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(MappedWithExtension.class)); |
|
|
|
|
entity.setEvaluationContextProvider( |
|
|
|
|
new ExtensionAwareEvaluationContextProvider(Arrays.asList(new SampleExtension()))); |
|
|
|
|
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension()))); |
|
|
|
|
|
|
|
|
|
assertThat(entity.getCollection()).isEqualTo("collectionName"); |
|
|
|
|
} |
|
|
|
|
@ -255,6 +257,18 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -255,6 +257,18 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
assertThat(entity.getCollation()).isEqualTo(org.springframework.data.mongodb.core.query.Collation.of("en_US")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // DATAMONGO-2565
|
|
|
|
|
public void usesCorrectExpressionsForCollectionAndCollation() { |
|
|
|
|
|
|
|
|
|
BasicMongoPersistentEntity<WithCollectionAndCollationFromSpEL> entity = new BasicMongoPersistentEntity<>( |
|
|
|
|
ClassTypeInformation.from(WithCollectionAndCollationFromSpEL.class)); |
|
|
|
|
entity.setEvaluationContextProvider( |
|
|
|
|
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension()))); |
|
|
|
|
|
|
|
|
|
assertThat(entity.getCollection()).isEqualTo("collectionName"); |
|
|
|
|
assertThat(entity.getCollation()).isEqualTo(Collation.of("en_US")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Document("contacts") |
|
|
|
|
class Contact {} |
|
|
|
|
|
|
|
|
|
@ -307,6 +321,9 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -307,6 +321,9 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
@Document(collation = "#{myCollation}") |
|
|
|
|
class WithCollationFromSpEL {} |
|
|
|
|
|
|
|
|
|
@Document(collection = "#{myProperty}", collation = "#{myCollation}") |
|
|
|
|
class WithCollectionAndCollationFromSpEL {} |
|
|
|
|
|
|
|
|
|
@Document(collation = "en_US") |
|
|
|
|
class WithSimpleCollation {} |
|
|
|
|
|
|
|
|
|
@ -315,7 +332,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -315,7 +332,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
|
|
|
|
|
static class SampleExtension implements EvaluationContextExtension { |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.spel.spi.EvaluationContextExtension#getExtensionId() |
|
|
|
|
*/ |
|
|
|
|
@ -324,7 +341,7 @@ public class BasicMongoPersistentEntityUnitTests {
@@ -324,7 +341,7 @@ public class BasicMongoPersistentEntityUnitTests {
|
|
|
|
|
return "sampleExtension"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.spel.spi.EvaluationContextExtension#getProperties() |
|
|
|
|
*/ |
|
|
|
|
|