use of cn.taketoday.aop.framework.AopProxy in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testNoInterceptorsAndNoTarget.
@Test
public void testNoInterceptorsAndNoTarget() {
assertThatExceptionOfType(AopConfigException.class).isThrownBy(() -> {
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
// Add no interceptors
AopProxy aop = createAopProxy(pc);
aop.getProxy();
});
}
use of cn.taketoday.aop.framework.AopProxy in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testUndeclaredUncheckedException.
@Test
public void testUndeclaredUncheckedException() throws Throwable {
final RuntimeException unexpectedException = new RuntimeException();
// Test return value
MethodInterceptor mi = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
throw unexpectedException;
}
};
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
pc.addAdvice(mi);
// We don't care about the object
pc.setTarget(new TestBean());
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(tb::getAge).matches(unexpectedException::equals);
}
use of cn.taketoday.aop.framework.AopProxy in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testContext.
/**
* @param context if true, want context
*/
private void testContext(final boolean context) throws Throwable {
final String s = "foo";
// Test return value
MethodInterceptor mi = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) {
if (!context) {
assertNoInvocationContext();
} else {
assertThat(ExposeInvocationInterceptor.currentInvocation()).as("have context").isNotNull();
}
return s;
}
};
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
if (context) {
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
}
pc.addAdvice(mi);
// Keep CGLIB happy
if (requiresTarget()) {
pc.setTarget(new TestBean());
}
AopProxy aop = createAopProxy(pc);
assertNoInvocationContext();
ITestBean tb = (ITestBean) aop.getProxy();
assertNoInvocationContext();
assertThat(tb.getName()).as("correct return value").isSameAs(s);
}
use of cn.taketoday.aop.framework.AopProxy in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testDeclaredException.
@Test
public void testDeclaredException() throws Throwable {
final Exception expectedException = new Exception();
// Test return value
MethodInterceptor mi = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
throw expectedException;
}
};
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
pc.addAdvice(mi);
// We don't care about the object
mockTargetSource.setTarget(new TestBean());
pc.setTargetSource(mockTargetSource);
AopProxy aop = createAopProxy(pc);
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
ITestBean tb = (ITestBean) aop.getProxy();
// Note: exception param below isn't used
tb.exceptional(expectedException);
}).matches(expectedException::equals);
}
use of cn.taketoday.aop.framework.AopProxy in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testTargetCanGetInvocationEvenIfNoAdviceChain.
/**
* Check that although a method is eligible for advice chain optimization and
* direct reflective invocation, it doesn't happen if we've asked to see the proxy,
* so as to guarantee a consistent programming model.
*/
@Test
public void testTargetCanGetInvocationEvenIfNoAdviceChain() throws Throwable {
NeedsToSeeProxy target = new NeedsToSeeProxy();
AdvisedSupport pc = new AdvisedSupport(INeedsToSeeProxy.class);
pc.setTarget(target);
pc.setExposeProxy(true);
// Now let's try it with the special target
AopProxy aop = createAopProxy(pc);
INeedsToSeeProxy proxied = (INeedsToSeeProxy) aop.getProxy();
// It will complain if it can't get the proxy
proxied.incrementViaProxy();
}
Aggregations