|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Method; |
20 | 20 | import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
21 | 22 | import java.util.List; |
| 23 | +import java.util.Map; |
22 | 24 |
|
23 | 25 | import org.junit.Rule; |
24 | 26 | import org.junit.Test; |
25 | 27 | import org.junit.rules.ExpectedException; |
26 | 28 |
|
27 | 29 | import org.springframework.retry.ExhaustedRetryException; |
| 30 | +import org.springframework.util.CollectionUtils; |
28 | 31 | import org.springframework.util.ReflectionUtils; |
29 | 32 |
|
30 | | -import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.*; |
31 | 34 |
|
32 | 35 | /** |
33 | 36 | * @author Dave Syer |
@@ -107,6 +110,49 @@ public void parentReturnTypeRecoverMethod() { |
107 | 110 |
|
108 | 111 | } |
109 | 112 |
|
| 113 | + @Test |
| 114 | + public void genericReturnStringValueTypeParentThrowableRecoverMethod() { |
| 115 | + |
| 116 | + RecoverAnnotationRecoveryHandler<?> handler = new RecoverAnnotationRecoveryHandler<List<String>>( |
| 117 | + new GenericReturnTypeRecover(), |
| 118 | + ReflectionUtils.findMethod(GenericReturnTypeRecover.class, "foo", String.class)); |
| 119 | + |
| 120 | + @SuppressWarnings("unchecked") |
| 121 | + Map<String, String> recoverResponseMap = (Map<String, String>) handler.recover(new Object[] { "Aldo" }, |
| 122 | + new RuntimeException("Planned")); |
| 123 | + assertFalse(CollectionUtils.isEmpty(recoverResponseMap)); |
| 124 | + assertEquals("fooRecoverValue1", recoverResponseMap.get("foo")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void genericReturnStringValueTypeChildThrowableRecoverMethod() { |
| 129 | + |
| 130 | + RecoverAnnotationRecoveryHandler<?> handler = new RecoverAnnotationRecoveryHandler<List<String>>( |
| 131 | + new GenericReturnTypeRecover(), |
| 132 | + ReflectionUtils.findMethod(GenericReturnTypeRecover.class, "foo", String.class)); |
| 133 | + |
| 134 | + @SuppressWarnings("unchecked") |
| 135 | + Map<String, String> recoverResponseMap = (Map<String, String>) handler.recover(new Object[] { "Aldo" }, |
| 136 | + new IllegalStateException("Planned")); |
| 137 | + assertFalse(CollectionUtils.isEmpty(recoverResponseMap)); |
| 138 | + assertEquals("fooRecoverValue2", recoverResponseMap.get("foo")); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void genericReturnOneValueTypeRecoverMethod() { |
| 143 | + |
| 144 | + RecoverAnnotationRecoveryHandler<?> handler = new RecoverAnnotationRecoveryHandler<List<String>>( |
| 145 | + new GenericReturnTypeRecover(), |
| 146 | + ReflectionUtils.findMethod(GenericReturnTypeRecover.class, "bar", String.class)); |
| 147 | + |
| 148 | + @SuppressWarnings("unchecked") |
| 149 | + Map<String, GenericReturnTypeRecover.One> recoverResponseMap = (Map<String, GenericReturnTypeRecover.One>) handler |
| 150 | + .recover(new Object[] { "Aldo" }, new RuntimeException("Planned")); |
| 151 | + assertFalse(CollectionUtils.isEmpty(recoverResponseMap)); |
| 152 | + assertNotNull(recoverResponseMap.get("bar")); |
| 153 | + assertEquals("barRecoverValue", recoverResponseMap.get("bar").name); |
| 154 | + } |
| 155 | + |
110 | 156 | @Test |
111 | 157 | public void multipleQualifyingRecoverMethods() { |
112 | 158 | Method foo = ReflectionUtils.findMethod(MultipleQualifyingRecovers.class, "foo", String.class); |
@@ -293,6 +339,45 @@ public Number quux(RuntimeException re, String name) { |
293 | 339 |
|
294 | 340 | } |
295 | 341 |
|
| 342 | + protected static class GenericReturnTypeRecover { |
| 343 | + |
| 344 | + private static class One { |
| 345 | + |
| 346 | + String name; |
| 347 | + |
| 348 | + public One(String name) { |
| 349 | + this.name = name; |
| 350 | + } |
| 351 | + |
| 352 | + } |
| 353 | + |
| 354 | + @Retryable |
| 355 | + public Map<String, String> foo(String name) { |
| 356 | + return Collections.singletonMap("foo", "fooValue"); |
| 357 | + } |
| 358 | + |
| 359 | + @Retryable |
| 360 | + public Map<String, One> bar(String name) { |
| 361 | + return Collections.singletonMap("bar", new One("barValue")); |
| 362 | + } |
| 363 | + |
| 364 | + @Recover |
| 365 | + public Map<String, String> fooRecoverRe(RuntimeException re, String name) { |
| 366 | + return Collections.singletonMap("foo", "fooRecoverValue1"); |
| 367 | + } |
| 368 | + |
| 369 | + @Recover |
| 370 | + public Map<String, String> fooRecoverIe(IllegalStateException re, String name) { |
| 371 | + return Collections.singletonMap("foo", "fooRecoverValue2"); |
| 372 | + } |
| 373 | + |
| 374 | + @Recover |
| 375 | + public Map<String, One> barRecover(RuntimeException re, String name) { |
| 376 | + return Collections.singletonMap("bar", new One("barRecoverValue")); |
| 377 | + } |
| 378 | + |
| 379 | + } |
| 380 | + |
296 | 381 | protected static class MultipleQualifyingRecoversNoThrowable { |
297 | 382 |
|
298 | 383 | @Retryable |
|
0 commit comments