|
| 1 | +package org.springframework.aop.framework; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | +import static org.mockito.Mockito.doAnswer; |
| 5 | +import static org.mockito.Mockito.doThrow; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | + |
| 8 | +import java.lang.reflect.Proxy; |
| 9 | +import java.lang.reflect.UndeclaredThrowableException; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +import org.aopalliance.aop.Advice; |
| 14 | +import org.aopalliance.intercept.MethodInterceptor; |
| 15 | +import org.assertj.core.api.WithAssertions; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Nested; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.mockito.Mockito; |
| 20 | +import org.mockito.stubbing.Answer; |
| 21 | +import org.springframework.cglib.proxy.Enhancer; |
| 22 | +import org.springframework.lang.Nullable; |
| 23 | + |
| 24 | +abstract class ProxyExceptionHandlingTests implements WithAssertions { |
| 25 | + |
| 26 | + private static final RuntimeException uncheckedException = new RuntimeException(); |
| 27 | + private static final DeclaredCheckedException declaredCheckedException = new DeclaredCheckedException(); |
| 28 | + private static final UndeclaredCheckedException undeclaredCheckedException = new UndeclaredCheckedException(); |
| 29 | + |
| 30 | + protected final MyClass target = mock(MyClass.class); |
| 31 | + protected final ProxyFactory proxyFactory = new ProxyFactory(target); |
| 32 | + |
| 33 | + @Nullable |
| 34 | + protected MyInterface proxy; |
| 35 | + @Nullable |
| 36 | + private Throwable throwableSeenByCaller; |
| 37 | + |
| 38 | + static class ObjenesisCglibAopProxyTest extends ProxyExceptionHandlingTests { |
| 39 | + @BeforeEach |
| 40 | + void beforeEach() { |
| 41 | + proxyFactory.setProxyTargetClass(true); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void assertProxyType(Object proxy) { |
| 46 | + assertThat(Enhancer.isEnhanced(proxy.getClass())).isTrue(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + static class JdkAopProxyTest extends ProxyExceptionHandlingTests { |
| 51 | + @Override |
| 52 | + protected void assertProxyType(Object proxy) { |
| 53 | + assertThat(Proxy.isProxyClass(proxy.getClass())).isTrue(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + protected void assertProxyType(Object proxy) {}; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void beforeEach() { |
| 61 | + Mockito.clearInvocations(target); |
| 62 | + } |
| 63 | + |
| 64 | + @Nested |
| 65 | + class WhenThereIsOneInterceptor { |
| 66 | + |
| 67 | + @Nullable |
| 68 | + private Throwable throwableSeenByInterceptor; |
| 69 | + |
| 70 | + @BeforeEach |
| 71 | + void beforeEach() { |
| 72 | + proxyFactory.addAdvice(captureThrowable()); |
| 73 | + |
| 74 | + proxy = (MyInterface) proxyFactory.getProxy(); |
| 75 | + |
| 76 | + assertProxyType(proxy); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException { |
| 81 | + doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething(); |
| 82 | + |
| 83 | + invokeProxy(); |
| 84 | + |
| 85 | + assertThat(throwableSeenByInterceptor).isSameAs(undeclaredCheckedException); |
| 86 | + assertThat(throwableSeenByCaller) |
| 87 | + .isInstanceOf(UndeclaredThrowableException.class) |
| 88 | + .hasCauseReference(undeclaredCheckedException); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException { |
| 93 | + doThrow(declaredCheckedException).when(target).doSomething(); |
| 94 | + |
| 95 | + invokeProxy(); |
| 96 | + |
| 97 | + assertThat(throwableSeenByInterceptor).isSameAs(declaredCheckedException); |
| 98 | + assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void targetThrowsUncheckedException() throws DeclaredCheckedException { |
| 103 | + doThrow(uncheckedException).when(target).doSomething(); |
| 104 | + |
| 105 | + invokeProxy(); |
| 106 | + |
| 107 | + assertThat(throwableSeenByInterceptor).isSameAs(uncheckedException); |
| 108 | + assertThat(throwableSeenByCaller).isSameAs(uncheckedException); |
| 109 | + } |
| 110 | + |
| 111 | + private MethodInterceptor captureThrowable() { |
| 112 | + return invocation -> { |
| 113 | + try { |
| 114 | + return invocation.proceed(); |
| 115 | + } catch (Exception e) { |
| 116 | + throwableSeenByInterceptor = e; |
| 117 | + throw e; |
| 118 | + } |
| 119 | + }; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Nested |
| 124 | + class WhenThereAreNoInterceptors { |
| 125 | + |
| 126 | + @BeforeEach |
| 127 | + void beforeEach() { |
| 128 | + proxy = (MyInterface) proxyFactory.getProxy(); |
| 129 | + |
| 130 | + assertProxyType(proxy); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException { |
| 135 | + doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething(); |
| 136 | + |
| 137 | + invokeProxy(); |
| 138 | + |
| 139 | + assertThat(throwableSeenByCaller) |
| 140 | + .isInstanceOf(UndeclaredThrowableException.class) |
| 141 | + .hasCauseReference(undeclaredCheckedException); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException { |
| 146 | + doThrow(declaredCheckedException).when(target).doSomething(); |
| 147 | + |
| 148 | + invokeProxy(); |
| 149 | + |
| 150 | + assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void targetThrowsUncheckedException() throws DeclaredCheckedException { |
| 155 | + doThrow(uncheckedException).when(target).doSomething(); |
| 156 | + |
| 157 | + invokeProxy(); |
| 158 | + |
| 159 | + assertThat(throwableSeenByCaller).isSameAs(uncheckedException); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private void invokeProxy() { |
| 164 | + throwableSeenByCaller = catchThrowable(() -> requireNonNull(proxy).doSomething()); |
| 165 | + } |
| 166 | + |
| 167 | + private Answer<?> sneakyThrow(@SuppressWarnings("SameParameterValue") Throwable throwable) { |
| 168 | + return invocation -> { |
| 169 | + throw throwable; |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + static class MyClass implements MyInterface { |
| 174 | + @Override |
| 175 | + public void doSomething() throws DeclaredCheckedException { |
| 176 | + throw declaredCheckedException; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + protected interface MyInterface { |
| 181 | + void doSomething() throws DeclaredCheckedException; |
| 182 | + } |
| 183 | + |
| 184 | + protected static class UndeclaredCheckedException extends Exception { |
| 185 | + } |
| 186 | + |
| 187 | + protected static class DeclaredCheckedException extends Exception { |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments