Browse Source

DATAMONGO-2565 - Polishing.

Add unit test to verify behavior. Cleanup code.

Original pull request: #869.
2.2.x
Mark Paluch 6 years ago
parent
commit
1e9ab02e3f
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 57
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java

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

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

Loading…
Cancel
Save