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 ProxyTargetClassTrueConfig method testAspectsAreApplied.
@Test
public void testAspectsAreApplied() {
ClassPathXmlApplicationContext bf = newContext("aspects.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian");
assertThat(tb.getAge()).isEqualTo(68);
MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean");
assertThat(AopUtils.isAopProxy(factoryBean.getTargetObject())).isTrue();
assertThat(((ITestBean) factoryBean.getTargetObject()).getAge()).isEqualTo(68);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ProxyTargetClassTrueConfig method testAspectsAreAppliedInDefinedOrder.
@Test
public void testAspectsAreAppliedInDefinedOrder() {
ClassPathXmlApplicationContext bf = newContext("aspectsWithOrdering.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian");
assertThat(tb.getAge()).isEqualTo(71);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ProxyTargetClassTrueConfig method testAdviceUsingJoinPoint.
@Test
public void testAdviceUsingJoinPoint() {
ClassPathXmlApplicationContext bf = newContext("usesJoinPointAspect.xml");
ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
adrian1.getAge();
AdviceUsingThisJoinPoint aspectInstance = (AdviceUsingThisJoinPoint) bf.getBean("aspect");
// (AdviceUsingThisJoinPoint) Aspects.aspectOf(AdviceUsingThisJoinPoint.class);
// assertEquals("method-execution(int TestBean.getAge())",aspectInstance.getLastMethodEntered());
assertThat(aspectInstance.getLastMethodEntered().indexOf("TestBean.getAge())") != 0).isTrue();
}
Aggregations