|
17 | 17 |
|
18 | 18 | import static org.hamcrest.Matchers.*; |
19 | 19 | import static org.junit.Assert.*; |
| 20 | +import static org.junit.Assume.*; |
20 | 21 | import static org.mockito.Mockito.*; |
21 | 22 |
|
22 | 23 | import java.io.Serializable; |
23 | 24 | import java.lang.reflect.Method; |
| 25 | +import java.util.Arrays; |
24 | 26 | import java.util.Collections; |
25 | 27 | import java.util.List; |
26 | 28 | import java.util.Set; |
| 29 | +import java.util.concurrent.CompletableFuture; |
27 | 30 | import java.util.concurrent.Future; |
28 | 31 |
|
29 | 32 | import org.junit.Assume; |
|
48 | 51 | import org.springframework.data.repository.core.NamedQueries; |
49 | 52 | import org.springframework.data.repository.core.RepositoryMetadata; |
50 | 53 | import org.springframework.data.repository.query.RepositoryQuery; |
| 54 | +import org.springframework.data.repository.sample.User; |
51 | 55 | import org.springframework.scheduling.annotation.Async; |
52 | 56 | import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor; |
53 | 57 | import org.springframework.test.util.ReflectionTestUtils; |
54 | 58 | import org.springframework.util.ClassUtils; |
| 59 | +import org.springframework.util.concurrent.ListenableFuture; |
55 | 60 |
|
56 | 61 | /** |
57 | 62 | * Unit tests for {@link RepositoryFactorySupport}. |
@@ -241,6 +246,89 @@ public void addsTransactionProxyInterfaceIfAvailable() throws Exception { |
241 | 246 | } |
242 | 247 | } |
243 | 248 |
|
| 249 | + /** |
| 250 | + * @see @see DATACMNS-714 |
| 251 | + */ |
| 252 | + @Test |
| 253 | + public void wrapsExecutionResultIntoCompletableFutureIfConfigured() throws Exception { |
| 254 | + |
| 255 | + assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); |
| 256 | + |
| 257 | + User reference = new User(); |
| 258 | + |
| 259 | + expect(prepareConvertingRepository(reference).findOneByFirstname("Foo"), reference); |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * @see @see DATACMNS-714 |
| 264 | + */ |
| 265 | + @Test |
| 266 | + public void wrapsExecutionResultIntoListenableFutureIfConfigured() throws Exception { |
| 267 | + |
| 268 | + assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); |
| 269 | + |
| 270 | + User reference = new User(); |
| 271 | + |
| 272 | + expect(prepareConvertingRepository(reference).findOneByLastname("Foo"), reference); |
| 273 | + } |
| 274 | + |
| 275 | + /** |
| 276 | + * @see @see DATACMNS-714 |
| 277 | + */ |
| 278 | + @Test |
| 279 | + public void wrapsExecutionResultIntoCompletableFutureWithEntityCollectionIfConfigured() throws Exception { |
| 280 | + |
| 281 | + assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); |
| 282 | + |
| 283 | + List<User> reference = Arrays.asList(new User()); |
| 284 | + |
| 285 | + expect(prepareConvertingRepository(reference).readAllByFirstname("Foo"), reference); |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * @see @see DATACMNS-714 |
| 290 | + */ |
| 291 | + @Test |
| 292 | + public void wrapsExecutionResultIntoListenableFutureWithEntityCollectionIfConfigured() throws Exception { |
| 293 | + |
| 294 | + assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); |
| 295 | + |
| 296 | + List<User> reference = Arrays.asList(new User()); |
| 297 | + |
| 298 | + expect(prepareConvertingRepository(reference).readAllByLastname("Foo"), reference); |
| 299 | + } |
| 300 | + |
| 301 | + private ConvertingRepository prepareConvertingRepository(final Object expectedValue) { |
| 302 | + |
| 303 | + when(factory.queryOne.execute(Mockito.any(Object[].class))).then(new Answer<Object>() { |
| 304 | + |
| 305 | + @Override |
| 306 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 307 | + Thread.sleep(200); |
| 308 | + return expectedValue; |
| 309 | + } |
| 310 | + }); |
| 311 | + |
| 312 | + AsyncAnnotationBeanPostProcessor processor = new AsyncAnnotationBeanPostProcessor(); |
| 313 | + processor.setBeanFactory(new DefaultListableBeanFactory()); |
| 314 | + |
| 315 | + return (ConvertingRepository) processor |
| 316 | + .postProcessAfterInitialization(factory.getRepository(ConvertingRepository.class), null); |
| 317 | + } |
| 318 | + |
| 319 | + private void expect(Future<?> future, Object value) throws Exception { |
| 320 | + |
| 321 | + assertThat(future.isDone(), is(false)); |
| 322 | + |
| 323 | + while (!future.isDone()) { |
| 324 | + Thread.sleep(50); |
| 325 | + } |
| 326 | + |
| 327 | + assertThat(future.get(), is(value)); |
| 328 | + |
| 329 | + verify(factory.queryOne, times(1)).execute(Mockito.any(Object[].class)); |
| 330 | + } |
| 331 | + |
244 | 332 | interface SimpleRepository extends Repository<Object, Serializable> {} |
245 | 333 |
|
246 | 334 | interface ObjectRepository extends Repository<Object, Serializable>, ObjectRepositoryCustom { |
@@ -301,5 +389,21 @@ interface ConvertingRepository extends Repository<Object, Long> { |
301 | 389 |
|
302 | 390 | @Async |
303 | 391 | Future<Object> findByFirstname(String firstname); |
| 392 | + |
| 393 | + // DATACMNS-714 |
| 394 | + @Async |
| 395 | + CompletableFuture<User> findOneByFirstname(String firstname); |
| 396 | + |
| 397 | + // DATACMNS-714 |
| 398 | + @Async |
| 399 | + CompletableFuture<List<User>> readAllByFirstname(String firstname); |
| 400 | + |
| 401 | + // DATACMNS-714 |
| 402 | + @Async |
| 403 | + ListenableFuture<User> findOneByLastname(String lastname); |
| 404 | + |
| 405 | + // DATACMNS-714 |
| 406 | + @Async |
| 407 | + ListenableFuture<List<User>> readAllByLastname(String lastname); |
304 | 408 | } |
305 | 409 | } |
0 commit comments