Code Samples for BAEL-4130 (#9513)
This commit is contained in:
parent
bb23d56558
commit
78cacb4d2e
@ -18,6 +18,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.undeclared;
|
||||
|
||||
public class SomeCheckedException extends Exception {
|
||||
public SomeCheckedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.baeldung.undeclared;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ThrowUndeclared {
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.baeldung.undeclared;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class UndeclaredApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UndeclaredApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.undeclared;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class UndeclaredAspect {
|
||||
|
||||
@Around("@annotation(undeclared)")
|
||||
public Object advise(ProceedingJoinPoint pjp, ThrowUndeclared undeclared) throws Throwable {
|
||||
throw new SomeCheckedException("AOP Checked Exception");
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.undeclared;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UndeclaredService {
|
||||
|
||||
@ThrowUndeclared
|
||||
public void doSomething() {}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.undeclared;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = UndeclaredApplication.class)
|
||||
public class UndeclaredThrowableExceptionIntegrationTest {
|
||||
|
||||
@Autowired private UndeclaredService service;
|
||||
|
||||
@Test
|
||||
public void givenAnAspect_whenCallingAdvisedMethod_thenShouldWrapTheException() {
|
||||
assertThatThrownBy(service::doSomething)
|
||||
.isInstanceOf(UndeclaredThrowableException.class)
|
||||
.hasCauseInstanceOf(SomeCheckedException.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.undeclared;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class UndeclaredThrowableExceptionUnitTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void givenAProxy_whenProxyUndeclaredThrowsCheckedException_thenShouldBeWrapped() {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
InvocationHandler invocationHandler = new ExceptionalInvocationHandler();
|
||||
List<String> proxy = (List<String>) Proxy.newProxyInstance(classLoader, new Class[] { List.class }, invocationHandler);
|
||||
|
||||
assertThatThrownBy(proxy::size)
|
||||
.isInstanceOf(UndeclaredThrowableException.class)
|
||||
.hasCauseInstanceOf(SomeCheckedException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void givenAProxy_whenProxyThrowsUncheckedException_thenShouldBeThrownAsIs() {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
InvocationHandler invocationHandler = new ExceptionalInvocationHandler();
|
||||
List<String> proxy = (List<String>) Proxy.newProxyInstance(classLoader, new Class[] { List.class }, invocationHandler);
|
||||
|
||||
assertThatThrownBy(proxy::isEmpty).isInstanceOf(RuntimeException.class);
|
||||
}
|
||||
|
||||
private static class ExceptionalInvocationHandler implements InvocationHandler {
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
if ("size".equals(method.getName())) {
|
||||
throw new SomeCheckedException("Always fails");
|
||||
}
|
||||
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user