use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testTargetCanGetInvocation.
@Test
public void testTargetCanGetInvocation() throws Throwable {
final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean();
AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// Assert that target matches BEFORE invocation returns
assertThat(invocation.getThis()).as("Target is correct").isEqualTo(expectedTarget);
return super.invoke(invocation);
}
};
pc.addAdvice(tii);
pc.setTarget(expectedTarget);
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
tb.getName();
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testRejectsBogusDynamicIntroductionAdviceWithNoAdapter.
@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
// TODO May fail on either call: may want to tighten up definition
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.getName();
});
// TODO used to catch UnknownAdviceTypeException, but
// with CGLIB some errors are in proxy creation and are wrapped
// in aspect exception. Error message is still fine.
// assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testUndeclaredUncheckedException.
@Test
public void testUndeclaredUncheckedException() throws Throwable {
final RuntimeException unexpectedException = new RuntimeException();
// Test return value
MethodInterceptor mi = invocation -> {
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 org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testMultiAdvice.
@Test
public void testMultiAdvice() throws Throwable {
CountingMultiAdvice cca = new CountingMultiAdvice();
@SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
ITestBean proxied = (ITestBean) createProxy(pf);
assertThat(cca.getCalls()).isEqualTo(0);
assertThat(cca.getCalls("getAge")).isEqualTo(0);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(cca.getCalls()).isEqualTo(2);
assertThat(cca.getCalls("getAge")).isEqualTo(2);
assertThat(cca.getCalls("setAge")).isEqualTo(0);
// Won't be advised
proxied.setAge(26);
assertThat(cca.getCalls()).isEqualTo(2);
assertThat(proxied.getAge()).isEqualTo(26);
assertThat(cca.getCalls()).isEqualTo(4);
assertThatExceptionOfType(SpecializedUncheckedException.class).as("Should have thrown CannotGetJdbcConnectionException").isThrownBy(() -> proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException) null)));
assertThat(cca.getCalls()).isEqualTo(6);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAdviceSupportListeners.
@Test
public void testAdviceSupportListeners() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
CountingAdvisorListener l = new CountingAdvisorListener(pc);
pc.addListener(l);
RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
// Should be automatically added as a listener
pc.addListener(acf);
assertThat(pc.isActive()).isFalse();
assertThat(l.activates).isEqualTo(0);
assertThat(acf.refreshes).isEqualTo(0);
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(acf.refreshes).isEqualTo(1);
assertThat(l.activates).isEqualTo(1);
assertThat(pc.isActive()).isTrue();
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(l.adviceChanges).isEqualTo(0);
NopInterceptor di = new NopInterceptor();
pc.addAdvice(0, di);
assertThat(l.adviceChanges).isEqualTo(1);
assertThat(acf.refreshes).isEqualTo(2);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.removeAdvice(di);
assertThat(l.adviceChanges).isEqualTo(2);
assertThat(acf.refreshes).isEqualTo(3);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.getProxy();
assertThat(l.activates).isEqualTo(1);
pc.removeListener(l);
assertThat(l.adviceChanges).isEqualTo(2);
pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
// No longer counting
assertThat(l.adviceChanges).isEqualTo(2);
}
Aggregations