JAVA-8280: Split or move spring-aop module

This commit is contained in:
sampadawagde
2021-12-30 20:39:11 +05:30
parent 77dcb8853f
commit 9eaa8f4dff
26 changed files with 123 additions and 7 deletions
@@ -1,49 +0,0 @@
package com.baeldung.method.info;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
class BankAccountServiceIntegrationTest {
private Account account;
@BeforeEach
public void setup() {
account = new Account();
account.setAccountNumber("12345");
account.setBalance(2000.0);
}
@Autowired
BankAccountService bankAccountService;
@Test
void withdraw() {
bankAccountService.withdraw(account, 500.0);
assertTrue(account.getBalance() == 1500.0);
}
@Test
void withdrawWhenLimitReached() {
Assertions.assertThatExceptionOfType(WithdrawLimitException.class)
.isThrownBy(() -> bankAccountService.withdraw(account, 600.0));
assertTrue(account.getBalance() == 2000.0);
}
@Test
void deposit() {
bankAccountService.deposit(account, 500.0);
assertTrue(account.getBalance() == 2500.0);
}
@Test
void getBalance() {
bankAccountService.getBalance();
}
}
@@ -1,7 +0,0 @@
package com.baeldung.undeclared;
public class SomeCheckedException extends Exception {
public SomeCheckedException(String message) {
super(message);
}
}
@@ -1,11 +0,0 @@
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 {
}
@@ -1,12 +0,0 @@
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);
}
}
@@ -1,16 +0,0 @@
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");
}
}
@@ -1,10 +0,0 @@
package com.baeldung.undeclared;
import org.springframework.stereotype.Service;
@Service
public class UndeclaredService {
@ThrowUndeclared
public void doSomething() {}
}
@@ -1,25 +0,0 @@
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);
}
}
@@ -1,48 +0,0 @@
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();
}
}
}