|
24 | 24 | import org.mockito.Mock;
|
25 | 25 | import org.mockito.runners.MockitoJUnitRunner;
|
26 | 26 | import org.springframework.context.ApplicationContext;
|
| 27 | +import org.springframework.data.mapping.model.MappingException; |
27 | 28 | import org.springframework.data.util.ClassTypeInformation;
|
28 | 29 |
|
29 | 30 | /**
|
|
36 | 37 | public class BasicMongoPersistentEntityUnitTests {
|
37 | 38 |
|
38 | 39 | @Mock ApplicationContext context;
|
| 40 | + @Mock MongoPersistentProperty propertyMock; |
39 | 41 |
|
40 | 42 | @Test
|
41 | 43 | public void subclassInheritsAtDocumentAnnotation() {
|
@@ -80,6 +82,61 @@ public void shouldDetectLanguageCorrectly() {
|
80 | 82 | assertThat(entity.getLanguage(), is("spanish"));
|
81 | 83 | }
|
82 | 84 |
|
| 85 | + /** |
| 86 | + * @see DATAMONGO-1053 |
| 87 | + */ |
| 88 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 89 | + @Test(expected = MappingException.class) |
| 90 | + public void verifyShouldThrowExceptionForInvalidTypeOfExplicitLanguageProperty() { |
| 91 | + |
| 92 | + BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
| 93 | + ClassTypeInformation.from(AnyDocument.class)); |
| 94 | + |
| 95 | + when(propertyMock.isExplicitLanguageProperty()).thenReturn(true); |
| 96 | + when(propertyMock.getActualType()).thenReturn((Class) Number.class); |
| 97 | + |
| 98 | + entity.addPersistentProperty(propertyMock); |
| 99 | + entity.verify(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @see DATAMONGO-1053 |
| 104 | + */ |
| 105 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 106 | + @Test |
| 107 | + public void verifyShouldPassForStringAsExplicitLanguageProperty() { |
| 108 | + |
| 109 | + BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
| 110 | + ClassTypeInformation.from(AnyDocument.class)); |
| 111 | + when(propertyMock.isExplicitLanguageProperty()).thenReturn(true); |
| 112 | + when(propertyMock.getActualType()).thenReturn((Class) String.class); |
| 113 | + entity.addPersistentProperty(propertyMock); |
| 114 | + |
| 115 | + entity.verify(); |
| 116 | + |
| 117 | + verify(propertyMock, times(1)).isExplicitLanguageProperty(); |
| 118 | + verify(propertyMock, times(1)).getActualType(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @see DATAMONGO-1053 |
| 123 | + */ |
| 124 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 125 | + @Test |
| 126 | + public void verifyShouldIgnoreNonExplicitLanguageProperty() { |
| 127 | + |
| 128 | + BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>( |
| 129 | + ClassTypeInformation.from(AnyDocument.class)); |
| 130 | + when(propertyMock.isExplicitLanguageProperty()).thenReturn(false); |
| 131 | + when(propertyMock.getActualType()).thenReturn((Class) Number.class); |
| 132 | + entity.addPersistentProperty(propertyMock); |
| 133 | + |
| 134 | + entity.verify(); |
| 135 | + |
| 136 | + verify(propertyMock, times(1)).isExplicitLanguageProperty(); |
| 137 | + verify(propertyMock, never()).getActualType(); |
| 138 | + } |
| 139 | + |
83 | 140 | @Document(collection = "contacts")
|
84 | 141 | class Contact {
|
85 | 142 |
|
@@ -111,4 +168,8 @@ public String getCollectionName() {
|
111 | 168 | static class DocumentWithLanguage {
|
112 | 169 |
|
113 | 170 | }
|
| 171 | + |
| 172 | + static class AnyDocument { |
| 173 | + |
| 174 | + } |
114 | 175 | }
|
0 commit comments