|
19 | 19 | import static org.mockito.ArgumentMatchers.anyString;
|
20 | 20 | import static org.mockito.BDDMockito.*;
|
21 | 21 |
|
22 |
| -import java.net.UnknownHostException; |
23 | 22 |
|
24 | 23 | import org.junit.After;
|
25 | 24 | import org.junit.Rule;
|
26 | 25 | import org.junit.Test;
|
27 | 26 | import org.junit.rules.ExpectedException;
|
28 |
| -import org.springframework.beans.factory.UnsatisfiedDependencyException; |
| 27 | +import org.springframework.beans.factory.BeanCreationException; |
29 | 28 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
30 | 29 | import org.springframework.context.annotation.Bean;
|
31 | 30 | import org.springframework.context.annotation.Configuration;
|
32 | 31 | import org.springframework.context.annotation.Import;
|
| 32 | +import org.springframework.context.annotation.Primary; |
33 | 33 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
34 | 34 | import org.springframework.data.mongodb.core.MongoOperations;
|
35 | 35 | import org.springframework.data.mongodb.core.index.IndexOperations;
|
@@ -65,10 +65,9 @@ public void after() {
|
65 | 65 | @Test
|
66 | 66 | public void noMongoOperationsConfiguration() {
|
67 | 67 |
|
68 |
| - this.thrown.expect(UnsatisfiedDependencyException.class); |
69 |
| - this.thrown.expectMessage("mongoSessionRepository"); |
70 |
| - |
71 |
| - registerAndRefresh(EmptyConfiguration.class); |
| 68 | + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( |
| 69 | + () -> registerAndRefresh(NoMongoOperationsConfiguration.class)).withMessageContaining( |
| 70 | + "expected at least 1 bean which qualifies as autowire candidate"); |
72 | 71 | }
|
73 | 72 |
|
74 | 73 | @Test
|
@@ -155,16 +154,82 @@ private void registerAndRefresh(Class<?>... annotatedClasses) {
|
155 | 154 | this.context.refresh();
|
156 | 155 | }
|
157 | 156 |
|
| 157 | + @Test |
| 158 | + public void multipleDataSourceConfiguration() { |
| 159 | + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( |
| 160 | + () -> registerAndRefresh(MongoOperationConfiguration.class, |
| 161 | + MultipleMongoOperationsConfiguration.class)).withMessageContaining( |
| 162 | + "expected single matching bean but found 2"); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void primaryMongoOperationConfiguration() { |
| 167 | + |
| 168 | + registerAndRefresh(MongoOperationConfiguration.class, PrimaryMongoOperationsConfiguration.class); |
| 169 | + |
| 170 | + MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class); |
| 171 | + MongoOperations mongoOperations = this.context.getBean("primaryMongoOperations", MongoOperations.class); |
| 172 | + assertThat(repository).isNotNull(); |
| 173 | + assertThat(mongoOperations).isNotNull(); |
| 174 | + MongoOperations mongoOperationsReflection = (MongoOperations) ReflectionTestUtils.getField(repository, |
| 175 | + "mongoOperations"); |
| 176 | + assertThat(mongoOperationsReflection).isNotNull(); |
| 177 | + assertThat((mongoOperationsReflection)).isEqualTo(mongoOperations); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void qualifiedDataSourceConfiguration() { |
| 182 | + registerAndRefresh(MongoOperationConfiguration.class, QualifiedMongoOperationsConfiguration.class); |
| 183 | + |
| 184 | + MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class); |
| 185 | + MongoOperations mongoOperations = this.context.getBean("qualifiedMongoOperations", MongoOperations.class); |
| 186 | + assertThat(repository).isNotNull(); |
| 187 | + assertThat(mongoOperations).isNotNull(); |
| 188 | + MongoOperations mongoOperationsReflection = (MongoOperations) ReflectionTestUtils.getField(repository, |
| 189 | + "mongoOperations"); |
| 190 | + assertThat(mongoOperationsReflection).isNotNull(); |
| 191 | + assertThat(mongoOperationsReflection).isEqualTo(mongoOperations); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void qualifiedAndPrimaryDataSourceConfiguration() { |
| 196 | + registerAndRefresh(MongoOperationConfiguration.class, QualifiedAndPrimaryMongoConfiguration.class); |
| 197 | + |
| 198 | + MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class); |
| 199 | + MongoOperations mongoOperations = this.context.getBean("qualifiedMongoOperations", MongoOperations.class); |
| 200 | + assertThat(repository).isNotNull(); |
| 201 | + assertThat(mongoOperations).isNotNull(); |
| 202 | + MongoOperations mongoOperationsReflection = (MongoOperations) ReflectionTestUtils.getField(repository, |
| 203 | + "mongoOperations"); |
| 204 | + assertThat(mongoOperations).isNotNull(); |
| 205 | + assertThat(mongoOperationsReflection).isEqualTo(mongoOperations); |
| 206 | + } |
| 207 | + |
158 | 208 | @Configuration
|
159 | 209 | @EnableMongoHttpSession
|
160 | 210 | static class EmptyConfiguration {
|
161 | 211 |
|
162 | 212 | }
|
163 | 213 |
|
| 214 | + @Configuration |
| 215 | + static class MongoOperationConfiguration { |
| 216 | + |
| 217 | + @Bean |
| 218 | + public MongoOperations defaultMongoOperations() { |
| 219 | + MongoOperations mongoOperations = mock(MongoOperations.class); |
| 220 | + IndexOperations indexOperations = mock(IndexOperations.class); |
| 221 | + |
| 222 | + given(mongoOperations.indexOps(anyString())).willReturn(indexOperations); |
| 223 | + |
| 224 | + return mongoOperations; |
| 225 | + } |
| 226 | + |
| 227 | + } |
| 228 | + |
164 | 229 | static class BaseConfiguration {
|
165 | 230 |
|
166 | 231 | @Bean
|
167 |
| - public MongoOperations mongoOperations() throws UnknownHostException { |
| 232 | + public MongoOperations mongoOperations() { |
168 | 233 |
|
169 | 234 | MongoOperations mongoOperations = mock(MongoOperations.class);
|
170 | 235 | IndexOperations indexOperations = mock(IndexOperations.class);
|
@@ -241,4 +306,78 @@ public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer
|
241 | 306 |
|
242 | 307 | }
|
243 | 308 |
|
| 309 | + @EnableMongoHttpSession |
| 310 | + static class NoMongoOperationsConfiguration { |
| 311 | + |
| 312 | + } |
| 313 | + |
| 314 | + @EnableMongoHttpSession |
| 315 | + static class MultipleMongoOperationsConfiguration { |
| 316 | + |
| 317 | + @Bean |
| 318 | + public MongoOperations secondaryDataSource() { |
| 319 | + return mock(MongoOperations.class); |
| 320 | + } |
| 321 | + |
| 322 | + } |
| 323 | + |
| 324 | + @EnableMongoHttpSession |
| 325 | + static class PrimaryMongoOperationsConfiguration { |
| 326 | + |
| 327 | + @Bean |
| 328 | + @Primary |
| 329 | + public MongoOperations primaryMongoOperations() { |
| 330 | + MongoOperations mongoOperations = mock(MongoOperations.class); |
| 331 | + IndexOperations indexOperations = mock(IndexOperations.class); |
| 332 | + |
| 333 | + given(mongoOperations.indexOps(anyString())).willReturn(indexOperations); |
| 334 | + |
| 335 | + return mongoOperations; |
| 336 | + } |
| 337 | + |
| 338 | + } |
| 339 | + |
| 340 | + @EnableMongoHttpSession |
| 341 | + static class QualifiedMongoOperationsConfiguration { |
| 342 | + |
| 343 | + @Bean |
| 344 | + @SpringSessionMongoOperations |
| 345 | + public MongoOperations qualifiedMongoOperations() { |
| 346 | + MongoOperations mongoOperations = mock(MongoOperations.class); |
| 347 | + IndexOperations indexOperations = mock(IndexOperations.class); |
| 348 | + |
| 349 | + given(mongoOperations.indexOps(anyString())).willReturn(indexOperations); |
| 350 | + |
| 351 | + return mongoOperations; |
| 352 | + } |
| 353 | + |
| 354 | + } |
| 355 | + |
| 356 | + @EnableMongoHttpSession |
| 357 | + static class QualifiedAndPrimaryMongoConfiguration { |
| 358 | + |
| 359 | + @Bean |
| 360 | + @SpringSessionMongoOperations |
| 361 | + public MongoOperations qualifiedMongoOperations() { |
| 362 | + MongoOperations mongoOperations = mock(MongoOperations.class); |
| 363 | + IndexOperations indexOperations = mock(IndexOperations.class); |
| 364 | + |
| 365 | + given(mongoOperations.indexOps(anyString())).willReturn(indexOperations); |
| 366 | + |
| 367 | + return mongoOperations; |
| 368 | + } |
| 369 | + |
| 370 | + @Bean |
| 371 | + @Primary |
| 372 | + public MongoOperations primaryMongoOperations() { |
| 373 | + MongoOperations mongoOperations = mock(MongoOperations.class); |
| 374 | + IndexOperations indexOperations = mock(IndexOperations.class); |
| 375 | + |
| 376 | + given(mongoOperations.indexOps(anyString())).willReturn(indexOperations); |
| 377 | + |
| 378 | + return mongoOperations; |
| 379 | + } |
| 380 | + |
| 381 | + } |
| 382 | + |
244 | 383 | }
|
0 commit comments