Code Samples for BAEL-4130 (#9513)

This commit is contained in:
Mona Mohamadinia 2020-07-03 18:59:56 +04:30 committed by GitHub
parent bb23d56558
commit 78cacb4d2e
8 changed files with 134 additions and 0 deletions

View File

@ -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>

View File

@ -0,0 +1,7 @@
package com.baeldung.undeclared;
public class SomeCheckedException extends Exception {
public SomeCheckedException(String message) {
super(message);
}
}

View File

@ -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 {
}

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.undeclared;
import org.springframework.stereotype.Service;
@Service
public class UndeclaredService {
@ThrowUndeclared
public void doSomething() {}
}

View File

@ -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);
}
}

View File

@ -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();
}
}
}