|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.aop.framework; |
| 18 | + |
| 19 | +import java.io.Serial; |
| 20 | +import java.lang.reflect.Proxy; |
| 21 | +import java.lang.reflect.UndeclaredThrowableException; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +import org.aopalliance.intercept.MethodInterceptor; |
| 25 | +import org.assertj.core.api.WithAssertions; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Nested; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.mockito.stubbing.Answer; |
| 31 | + |
| 32 | +import org.springframework.cglib.proxy.Enhancer; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | + |
| 35 | +import static org.mockito.BDDMockito.doAnswer; |
| 36 | +import static org.mockito.BDDMockito.doThrow; |
| 37 | +import static org.mockito.BDDMockito.mock; |
| 38 | + |
| 39 | +abstract class ProxyExceptionHandlingTests implements WithAssertions { |
| 40 | + |
| 41 | + private static final RuntimeException uncheckedException = new RuntimeException(); |
| 42 | + private static final DeclaredCheckedException declaredCheckedException = new DeclaredCheckedException(); |
| 43 | + private static final UndeclaredCheckedException undeclaredCheckedException = new UndeclaredCheckedException(); |
| 44 | + |
| 45 | + protected final MyClass target = mock(MyClass.class); |
| 46 | + protected final ProxyFactory proxyFactory = new ProxyFactory(target); |
| 47 | + |
| 48 | + @Nullable |
| 49 | + protected MyInterface proxy; |
| 50 | + @Nullable |
| 51 | + private Throwable throwableSeenByCaller; |
| 52 | + |
| 53 | + static class ObjenesisCglibAopProxyTests extends ProxyExceptionHandlingTests { |
| 54 | + @BeforeEach |
| 55 | + void beforeEach() { |
| 56 | + proxyFactory.setProxyTargetClass(true); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void assertProxyType(Object proxy) { |
| 61 | + assertThat(Enhancer.isEnhanced(proxy.getClass())).isTrue(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static class JdkAopProxyTests extends ProxyExceptionHandlingTests { |
| 66 | + @Override |
| 67 | + protected void assertProxyType(Object proxy) { |
| 68 | + assertThat(Proxy.isProxyClass(proxy.getClass())).isTrue(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + protected void assertProxyType(Object proxy) { |
| 73 | + } |
| 74 | + |
| 75 | + @BeforeEach |
| 76 | + void beforeEach() { |
| 77 | + Mockito.clearInvocations(target); |
| 78 | + } |
| 79 | + |
| 80 | + @Nested |
| 81 | + class WhenThereIsOneInterceptor { |
| 82 | + |
| 83 | + @Nullable |
| 84 | + private Throwable throwableSeenByInterceptor; |
| 85 | + |
| 86 | + @BeforeEach |
| 87 | + void beforeEach() { |
| 88 | + proxyFactory.addAdvice(captureThrowable()); |
| 89 | + |
| 90 | + proxy = (MyInterface) proxyFactory.getProxy(); |
| 91 | + |
| 92 | + assertProxyType(proxy); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException { |
| 97 | + doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething(); |
| 98 | + |
| 99 | + invokeProxy(); |
| 100 | + |
| 101 | + assertThat(throwableSeenByInterceptor).isSameAs(undeclaredCheckedException); |
| 102 | + assertThat(throwableSeenByCaller) |
| 103 | + .isInstanceOf(UndeclaredThrowableException.class) |
| 104 | + .hasCauseReference(undeclaredCheckedException); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException { |
| 109 | + doThrow(declaredCheckedException).when(target).doSomething(); |
| 110 | + |
| 111 | + invokeProxy(); |
| 112 | + |
| 113 | + assertThat(throwableSeenByInterceptor).isSameAs(declaredCheckedException); |
| 114 | + assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void targetThrowsUncheckedException() throws DeclaredCheckedException { |
| 119 | + doThrow(uncheckedException).when(target).doSomething(); |
| 120 | + |
| 121 | + invokeProxy(); |
| 122 | + |
| 123 | + assertThat(throwableSeenByInterceptor).isSameAs(uncheckedException); |
| 124 | + assertThat(throwableSeenByCaller).isSameAs(uncheckedException); |
| 125 | + } |
| 126 | + |
| 127 | + private MethodInterceptor captureThrowable() { |
| 128 | + return invocation -> { |
| 129 | + try { |
| 130 | + return invocation.proceed(); |
| 131 | + } |
| 132 | + catch (Exception e) { |
| 133 | + throwableSeenByInterceptor = e; |
| 134 | + throw e; |
| 135 | + } |
| 136 | + }; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Nested |
| 141 | + class WhenThereAreNoInterceptors { |
| 142 | + |
| 143 | + @BeforeEach |
| 144 | + void beforeEach() { |
| 145 | + proxy = (MyInterface) proxyFactory.getProxy(); |
| 146 | + |
| 147 | + assertProxyType(proxy); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException { |
| 152 | + doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething(); |
| 153 | + |
| 154 | + invokeProxy(); |
| 155 | + |
| 156 | + assertThat(throwableSeenByCaller) |
| 157 | + .isInstanceOf(UndeclaredThrowableException.class) |
| 158 | + .hasCauseReference(undeclaredCheckedException); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException { |
| 163 | + doThrow(declaredCheckedException).when(target).doSomething(); |
| 164 | + |
| 165 | + invokeProxy(); |
| 166 | + |
| 167 | + assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void targetThrowsUncheckedException() throws DeclaredCheckedException { |
| 172 | + doThrow(uncheckedException).when(target).doSomething(); |
| 173 | + |
| 174 | + invokeProxy(); |
| 175 | + |
| 176 | + assertThat(throwableSeenByCaller).isSameAs(uncheckedException); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void invokeProxy() { |
| 181 | + throwableSeenByCaller = catchThrowable(() -> Objects.requireNonNull(proxy).doSomething()); |
| 182 | + } |
| 183 | + |
| 184 | + private Answer<?> sneakyThrow(@SuppressWarnings("SameParameterValue") Throwable throwable) { |
| 185 | + return invocation -> { |
| 186 | + throw throwable; |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + static class MyClass implements MyInterface { |
| 191 | + @Override |
| 192 | + public void doSomething() throws DeclaredCheckedException { |
| 193 | + throw declaredCheckedException; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + protected interface MyInterface { |
| 198 | + void doSomething() throws DeclaredCheckedException; |
| 199 | + } |
| 200 | + |
| 201 | + protected static class UndeclaredCheckedException extends Exception { |
| 202 | + @Serial |
| 203 | + private static final long serialVersionUID = -4199611059122356651L; |
| 204 | + } |
| 205 | + |
| 206 | + protected static class DeclaredCheckedException extends Exception { |
| 207 | + @Serial |
| 208 | + private static final long serialVersionUID = 8851375818059230518L; |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments