Browse Source

DATAMONGO-1874 - Polishing.

Cleanups in test case. Moved assertions to AssertJ.
pull/563/head
Oliver Gierke 8 years ago
parent
commit
43d821aab0
  1. 106
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java

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

@ -15,9 +15,7 @@
*/ */
package org.springframework.data.mongodb.core.mapping; package org.springframework.data.mongodb.core.mapping;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
@ -56,7 +54,7 @@ public class BasicMongoPersistentEntityUnitTests {
BasicMongoPersistentEntity<Person> entity = new BasicMongoPersistentEntity<Person>( BasicMongoPersistentEntity<Person> entity = new BasicMongoPersistentEntity<Person>(
ClassTypeInformation.from(Person.class)); ClassTypeInformation.from(Person.class));
assertThat(entity.getCollection(), is("contacts")); assertThat(entity.getCollection()).isEqualTo("contacts");
} }
@Test @Test
@ -64,7 +62,7 @@ public class BasicMongoPersistentEntityUnitTests {
MongoPersistentEntity<Company> entity = new BasicMongoPersistentEntity<Company>( MongoPersistentEntity<Company> entity = new BasicMongoPersistentEntity<Company>(
ClassTypeInformation.from(Company.class)); ClassTypeInformation.from(Company.class));
assertThat(entity.getCollection(), is("35")); assertThat(entity.getCollection()).isEqualTo("35");
} }
@Test // DATAMONGO-65, DATAMONGO-1108 @Test // DATAMONGO-65, DATAMONGO-1108
@ -79,10 +77,10 @@ public class BasicMongoPersistentEntityUnitTests {
ClassTypeInformation.from(DynamicallyMapped.class)); ClassTypeInformation.from(DynamicallyMapped.class));
entity.setEvaluationContextProvider(new ExtensionAwareEvaluationContextProvider(context)); entity.setEvaluationContextProvider(new ExtensionAwareEvaluationContextProvider(context));
assertThat(entity.getCollection(), is("reference")); assertThat(entity.getCollection()).isEqualTo("reference");
provider.collectionName = "otherReference"; provider.collectionName = "otherReference";
assertThat(entity.getCollection(), is("otherReference")); assertThat(entity.getCollection()).isEqualTo("otherReference");
} }
@Test // DATAMONGO-937 @Test // DATAMONGO-937
@ -90,31 +88,31 @@ public class BasicMongoPersistentEntityUnitTests {
BasicMongoPersistentEntity<DocumentWithLanguage> entity = new BasicMongoPersistentEntity<DocumentWithLanguage>( BasicMongoPersistentEntity<DocumentWithLanguage> entity = new BasicMongoPersistentEntity<DocumentWithLanguage>(
ClassTypeInformation.from(DocumentWithLanguage.class)); ClassTypeInformation.from(DocumentWithLanguage.class));
assertThat(entity.getLanguage(), is("spanish"));
assertThat(entity.getLanguage()).isEqualTo("spanish");
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test // DATAMONGO-1053
@Test(expected = MappingException.class) // DATAMONGO-1053
public void verifyShouldThrowExceptionForInvalidTypeOfExplicitLanguageProperty() { public void verifyShouldThrowExceptionForInvalidTypeOfExplicitLanguageProperty() {
doReturn(true).when(propertyMock).isExplicitLanguageProperty();
doReturn(Number.class).when(propertyMock).getActualType();
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class)); ClassTypeInformation.from(AnyDocument.class));
when(propertyMock.isExplicitLanguageProperty()).thenReturn(true);
when(propertyMock.getActualType()).thenReturn((Class) Number.class);
entity.addPersistentProperty(propertyMock); entity.addPersistentProperty(propertyMock);
entity.verify();
assertThatExceptionOfType(MappingException.class).isThrownBy(() -> entity.verify());
} }
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test // DATAMONGO-1053 @Test // DATAMONGO-1053
public void verifyShouldPassForStringAsExplicitLanguageProperty() { public void verifyShouldPassForStringAsExplicitLanguageProperty() {
doReturn(true).when(propertyMock).isExplicitLanguageProperty();
doReturn(String.class).when(propertyMock).getActualType();
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class)); ClassTypeInformation.from(AnyDocument.class));
when(propertyMock.isExplicitLanguageProperty()).thenReturn(true);
when(propertyMock.getActualType()).thenReturn((Class) String.class);
entity.addPersistentProperty(propertyMock); entity.addPersistentProperty(propertyMock);
entity.verify(); entity.verify();
@ -123,7 +121,6 @@ public class BasicMongoPersistentEntityUnitTests {
verify(propertyMock, times(1)).getActualType(); verify(propertyMock, times(1)).getActualType();
} }
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test // DATAMONGO-1053 @Test // DATAMONGO-1053
public void verifyShouldIgnoreNonExplicitLanguageProperty() { public void verifyShouldIgnoreNonExplicitLanguageProperty() {
@ -138,71 +135,74 @@ public class BasicMongoPersistentEntityUnitTests {
verify(propertyMock, never()).getActualType(); verify(propertyMock, never()).getActualType();
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test // DATAMONGO-1157
@Test(expected = MappingException.class) // DATAMONGO-1157
public void verifyShouldThrowErrorForLazyDBRefOnFinalClass() { public void verifyShouldThrowErrorForLazyDBRefOnFinalClass() {
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock( org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock(
org.springframework.data.mongodb.core.mapping.DBRef.class); org.springframework.data.mongodb.core.mapping.DBRef.class);
when(propertyMock.isDbReference()).thenReturn(true);
when(propertyMock.getDBRef()).thenReturn(dbRefMock); doReturn(Class.class).when(propertyMock).getActualType();
when(dbRefMock.lazy()).thenReturn(true); doReturn(true).when(propertyMock).isDbReference();
when(propertyMock.getActualType()).thenReturn((Class) Class.class); doReturn(dbRefMock).when(propertyMock).getDBRef();
doReturn(true).when(dbRefMock).lazy();
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
entity.addPersistentProperty(propertyMock); entity.addPersistentProperty(propertyMock);
entity.verify(); assertThatExceptionOfType(MappingException.class).isThrownBy(() -> entity.verify());
} }
@Test(expected = MappingException.class) // DATAMONGO-1157 @Test // DATAMONGO-1157
public void verifyShouldThrowErrorForLazyDBRefArray() { public void verifyShouldThrowErrorForLazyDBRefArray() {
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock( org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock(
org.springframework.data.mongodb.core.mapping.DBRef.class); org.springframework.data.mongodb.core.mapping.DBRef.class);
when(propertyMock.isDbReference()).thenReturn(true);
when(propertyMock.getDBRef()).thenReturn(dbRefMock); doReturn(true).when(propertyMock).isDbReference();
when(dbRefMock.lazy()).thenReturn(true); doReturn(true).when(propertyMock).isArray();
when(propertyMock.isArray()).thenReturn(true); doReturn(dbRefMock).when(propertyMock).getDBRef();
doReturn(true).when(dbRefMock).lazy();
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
entity.addPersistentProperty(propertyMock); entity.addPersistentProperty(propertyMock);
entity.verify(); assertThatExceptionOfType(MappingException.class).isThrownBy(() -> entity.verify());
} }
@Test // DATAMONGO-1157 @Test // DATAMONGO-1157
@SuppressWarnings({ "unchecked", "rawtypes" })
public void verifyShouldPassForLazyDBRefOnNonArrayNonFinalClass() { public void verifyShouldPassForLazyDBRefOnNonArrayNonFinalClass() {
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock( org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock(
org.springframework.data.mongodb.core.mapping.DBRef.class); org.springframework.data.mongodb.core.mapping.DBRef.class);
when(propertyMock.isDbReference()).thenReturn(true);
when(propertyMock.getDBRef()).thenReturn(dbRefMock);
when(dbRefMock.lazy()).thenReturn(true);
when(propertyMock.getActualType()).thenReturn((Class) Object.class);
entity.addPersistentProperty(propertyMock);
doReturn(true).when(propertyMock).isDbReference();
doReturn(Object.class).when(propertyMock).getActualType();
doReturn(dbRefMock).when(propertyMock).getDBRef();
doReturn(true).when(dbRefMock).lazy();
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
entity.addPersistentProperty(propertyMock);
entity.verify(); entity.verify();
verify(propertyMock, times(1)).isDbReference(); verify(propertyMock, times(1)).isDbReference();
} }
@Test // DATAMONGO-1157 @Test // DATAMONGO-1157
@SuppressWarnings({ "unchecked", "rawtypes" })
public void verifyShouldPassForNonLazyDBRefOnFinalClass() { public void verifyShouldPassForNonLazyDBRefOnFinalClass() {
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock( org.springframework.data.mongodb.core.mapping.DBRef dbRefMock = mock(
org.springframework.data.mongodb.core.mapping.DBRef.class); org.springframework.data.mongodb.core.mapping.DBRef.class);
when(propertyMock.isDbReference()).thenReturn(true);
when(propertyMock.getDBRef()).thenReturn(dbRefMock);
when(dbRefMock.lazy()).thenReturn(false);
entity.addPersistentProperty(propertyMock);
doReturn(true).when(propertyMock).isDbReference();
doReturn(dbRefMock).when(propertyMock).getDBRef();
doReturn(false).when(dbRefMock).lazy();
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
ClassTypeInformation.from(AnyDocument.class));
entity.addPersistentProperty(propertyMock);
entity.verify(); entity.verify();
verify(dbRefMock, times(1)).lazy(); verify(dbRefMock, times(1)).lazy();
@ -214,7 +214,7 @@ public class BasicMongoPersistentEntityUnitTests {
BasicMongoPersistentEntity<DocumentWithCustomAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithCustomAnnotation>( BasicMongoPersistentEntity<DocumentWithCustomAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithCustomAnnotation>(
ClassTypeInformation.from(DocumentWithCustomAnnotation.class)); ClassTypeInformation.from(DocumentWithCustomAnnotation.class));
assertThat(entity.getCollection(), is("collection-1")); assertThat(entity.getCollection()).isEqualTo("collection-1");
} }
@Test // DATAMONGO-1373 @Test // DATAMONGO-1373
@ -223,7 +223,7 @@ public class BasicMongoPersistentEntityUnitTests {
BasicMongoPersistentEntity<DocumentWithComposedAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithComposedAnnotation>( BasicMongoPersistentEntity<DocumentWithComposedAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithComposedAnnotation>(
ClassTypeInformation.from(DocumentWithComposedAnnotation.class)); ClassTypeInformation.from(DocumentWithComposedAnnotation.class));
assertThat(entity.getCollection(), is("custom-collection")); assertThat(entity.getCollection()).isEqualTo("custom-collection");
} }
@Test // DATAMONGO-1874 @Test // DATAMONGO-1874

Loading…
Cancel
Save