use of cn.taketoday.aop.MethodBeforeAdvice in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testProxyConfigString.
/**
* Check that the string is informative.
*/
@Test
public void testProxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
MethodBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
pc.addAdvisor(advisor);
ITestBean proxied = (ITestBean) createProxy(pc);
String proxyConfigString = ((Advised) proxied).toProxyConfigString();
assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
use of cn.taketoday.aop.MethodBeforeAdvice in project today-framework by TAKETODAY.
the class MethodInvocationProceedingJoinPointTests method testCanGetStaticPartFromJoinPoint.
@Test
public void testCanGetStaticPartFromJoinPoint() {
final Object raw = new TestBean();
ProxyFactory pf = new ProxyFactory(raw);
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
pf.addAdvice((MethodBeforeAdvice) (method) -> {
StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart);
assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION);
assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature());
assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
});
ITestBean itb = (ITestBean) pf.getProxy();
// Any call will do
itb.getAge();
}
use of cn.taketoday.aop.MethodBeforeAdvice in project today-framework by TAKETODAY.
the class MethodInvocationProceedingJoinPointTests method testCanGetMethodSignatureFromJoinPoint.
@Test
public void testCanGetMethodSignatureFromJoinPoint() {
final Object raw = new TestBean();
// Will be set by advice during a method call
final int newAge = 23;
ProxyFactory pf = new ProxyFactory(raw);
pf.setExposeProxy(true);
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
AtomicInteger depth = new AtomicInteger();
pf.addAdvice((MethodBeforeAdvice) (method) -> {
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
assertThat(jp.toString().contains(method.getMethod().getName())).as("Method named in toString").isTrue();
jp.toShortString();
jp.toLongString();
assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(method.getThis());
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
assertThat(thisProxy).isNotSameAs(method.getThis());
assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
if (depth.getAndIncrement() == 0) {
thisProxy.toString();
thisProxy.setAge(newAge);
assertThat(thisProxy.getAge()).isEqualTo(newAge);
}
assertThat(thisProxy).isSameAs(AopContext.currentProxy());
assertThat(raw).isSameAs(method.getThis());
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getMethod().getName());
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getMethod().getModifiers());
MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
assertThat(msig.getDeclaringType()).isEqualTo(method.getMethod().getDeclaringClass());
assertThat(Arrays.equals(method.getMethod().getParameterTypes(), msig.getParameterTypes())).isTrue();
assertThat(msig.getReturnType()).isEqualTo(method.getMethod().getReturnType());
assertThat(Arrays.equals(method.getMethod().getExceptionTypes(), msig.getExceptionTypes())).isTrue();
msig.toLongString();
msig.toShortString();
});
ITestBean itb = (ITestBean) pf.getProxy();
// Any call will do
assertThat(itb.getAge()).as("Advice reentrantly set age").isEqualTo(newAge);
}
use of cn.taketoday.aop.MethodBeforeAdvice in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testProxyConfigString.
/**
* Check that the string is informative.
*/
@Test
public void testProxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
MethodBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
pc.addAdvisor(advisor);
ITestBean proxied = (ITestBean) createProxy(pc);
String proxyConfigString = ((Advised) proxied).toProxyConfigString();
assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
use of cn.taketoday.aop.MethodBeforeAdvice in project today-infrastructure by TAKETODAY.
the class MethodInvocationProceedingJoinPointTests method testCanGetMethodSignatureFromJoinPoint.
@Test
public void testCanGetMethodSignatureFromJoinPoint() {
final Object raw = new TestBean();
// Will be set by advice during a method call
final int newAge = 23;
ProxyFactory pf = new ProxyFactory(raw);
pf.setExposeProxy(true);
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
AtomicInteger depth = new AtomicInteger();
pf.addAdvice((MethodBeforeAdvice) (method) -> {
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
assertThat(jp.toString().contains(method.getMethod().getName())).as("Method named in toString").isTrue();
jp.toShortString();
jp.toLongString();
assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(method.getThis());
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
assertThat(thisProxy).isNotSameAs(method.getThis());
assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
if (depth.getAndIncrement() == 0) {
thisProxy.toString();
thisProxy.setAge(newAge);
assertThat(thisProxy.getAge()).isEqualTo(newAge);
}
assertThat(thisProxy).isSameAs(AopContext.currentProxy());
assertThat(raw).isSameAs(method.getThis());
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getMethod().getName());
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getMethod().getModifiers());
MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
assertThat(msig.getDeclaringType()).isEqualTo(method.getMethod().getDeclaringClass());
assertThat(Arrays.equals(method.getMethod().getParameterTypes(), msig.getParameterTypes())).isTrue();
assertThat(msig.getReturnType()).isEqualTo(method.getMethod().getReturnType());
assertThat(Arrays.equals(method.getMethod().getExceptionTypes(), msig.getExceptionTypes())).isTrue();
msig.toLongString();
msig.toShortString();
});
ITestBean itb = (ITestBean) pf.getProxy();
// Any call will do
assertThat(itb.getAge()).as("Advice reentrantly set age").isEqualTo(newAge);
}
Aggregations