|
1 | 1 | /*
|
2 |
| - * Copyright 2006-2023 the original author or authors. |
| 2 | + * Copyright 2006-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
36 | 36 | import org.springframework.retry.RetryCallback;
|
37 | 37 | import org.springframework.retry.RetryContext;
|
38 | 38 | import org.springframework.retry.RetryListener;
|
| 39 | +import org.springframework.retry.context.RetryContextSupport; |
39 | 40 | import org.springframework.retry.listener.MethodInvocationRetryListenerSupport;
|
40 | 41 | import org.springframework.retry.policy.NeverRetryPolicy;
|
41 | 42 | import org.springframework.retry.policy.SimpleRetryPolicy;
|
| 43 | +import org.springframework.retry.support.RetrySynchronizationManager; |
42 | 44 | import org.springframework.retry.support.RetryTemplate;
|
43 | 45 | import org.springframework.transaction.support.TransactionSynchronization;
|
44 | 46 | import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
47 | 49 | import static org.assertj.core.api.Assertions.assertThat;
|
48 | 50 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
49 | 51 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
| 52 | +import static org.mockito.Mockito.mock; |
| 53 | +import static org.mockito.Mockito.when; |
50 | 54 |
|
51 | 55 | /**
|
52 | 56 | * @author Dave Syer
|
|
55 | 59 | * @author Stéphane Nicoll
|
56 | 60 | * @author Henning Pöttker
|
57 | 61 | * @author Artem Bilan
|
| 62 | + * @author Kim Jun Hyeong |
58 | 63 | */
|
59 | 64 | public class RetryOperationsInterceptorTests {
|
60 | 65 |
|
@@ -257,6 +262,48 @@ public Object proceed() {
|
257 | 262 | })).withMessageContaining("MethodInvocation");
|
258 | 263 | }
|
259 | 264 |
|
| 265 | + @Test |
| 266 | + public void testProxyAttributeCleanupWithCorrectKey() { |
| 267 | + RetryContext directContext = new RetryContextSupport(null); |
| 268 | + Object mockProxy = new Object(); |
| 269 | + RetrySynchronizationManager.register(directContext); |
| 270 | + directContext.setAttribute("___proxy___", mockProxy); |
| 271 | + RetryContext retrievedContext = RetrySynchronizationManager.getContext(); |
| 272 | + retrievedContext.removeAttribute("___proxy___"); |
| 273 | + assertThat(RetrySynchronizationManager.getContext().getAttribute("___proxy___")).isNull(); |
| 274 | + RetrySynchronizationManager.clear(); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + public void testProxyAttributeRemainWithWrongKey() { |
| 279 | + RetryContext directContext = new RetryContextSupport(null); |
| 280 | + Object mockProxy = new Object(); |
| 281 | + RetrySynchronizationManager.register(directContext); |
| 282 | + directContext.setAttribute("___proxy___", mockProxy); |
| 283 | + RetryContext retrievedContext = RetrySynchronizationManager.getContext(); |
| 284 | + retrievedContext.removeAttribute("__proxy__"); |
| 285 | + Object remainingProxy = RetrySynchronizationManager.getContext().getAttribute("___proxy___"); |
| 286 | + assertThat(remainingProxy).isNotNull(); |
| 287 | + assertThat(remainingProxy).isSameAs(mockProxy); |
| 288 | + RetrySynchronizationManager.clear(); |
| 289 | + } |
| 290 | + |
| 291 | + @Test |
| 292 | + public void testProxyAttributeCleanupEvenWhenIllegalStateExceptionThrown() throws NoSuchMethodException { |
| 293 | + RetryContext context = new RetryContextSupport(null); |
| 294 | + Object mockProxy = new Object(); |
| 295 | + RetrySynchronizationManager.register(context); |
| 296 | + context.setAttribute("___proxy___", mockProxy); |
| 297 | + assertThat(context.getAttribute("___proxy___")).isNotNull(); |
| 298 | + MethodInvocation mockInvocation = mock(MethodInvocation.class); |
| 299 | + Method testMethod = this.getClass().getMethod("testProxyAttributeCleanupEvenWhenIllegalStateExceptionThrown"); |
| 300 | + when(mockInvocation.getMethod()).thenReturn(testMethod); |
| 301 | + assertThatIllegalStateException().isThrownBy(() -> this.interceptor.invoke(mockInvocation)) |
| 302 | + .withMessageContaining("MethodInvocation"); |
| 303 | + assertThat(context.getAttribute("___proxy___")).isNull(); |
| 304 | + RetrySynchronizationManager.clear(); |
| 305 | + } |
| 306 | + |
260 | 307 | public static interface Service {
|
261 | 308 |
|
262 | 309 | void service() throws Exception;
|
|
0 commit comments