use of org.springframework.tests.sample.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, 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);
assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, cca.getCalls());
assertEquals(0, cca.getCalls("getAge"));
assertEquals(target.getAge(), proxied.getAge());
assertEquals(2, cca.getCalls());
assertEquals(2, cca.getCalls("getAge"));
assertEquals(0, cca.getCalls("setAge"));
// Won't be advised
proxied.setAge(26);
assertEquals(2, cca.getCalls());
assertEquals(26, proxied.getAge());
assertEquals(4, cca.getCalls());
try {
proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException) null));
fail("Should have thrown CannotGetJdbcConnectionException");
} catch (SpecializedUncheckedException ex) {
// expected
}
assertEquals(6, cca.getCalls());
}
use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.
@Test
public void testCannotAddInterceptorWhenFrozen() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertFalse(pc.isFrozen());
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
try {
pc.addAdvice(0, new NopInterceptor());
fail("Shouldn't be able to add interceptor when frozen");
} catch (AopConfigException ex) {
assertTrue(ex.getMessage().indexOf("frozen") > -1);
}
// Check it still works: proxy factory state shouldn't have been corrupted
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, ((Advised) proxied).getAdvisors().length);
}
use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddDynamicIntroductionAdviceExceptInIntroductionAdvice.
@Test
public void testCannotAddDynamicIntroductionAdviceExceptInIntroductionAdvice() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
try {
pc.addAdvice(new DummyIntroductionAdviceImpl());
fail("Shouldn't be able to add introduction interceptor except via introduction advice");
} catch (AopConfigException ex) {
assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
}
// Check it still works: proxy factory state shouldn't have been corrupted
ITestBean proxied = (ITestBean) createProxy(pc);
assertEquals(target.getAge(), proxied.getAge());
}
use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddAdvisorWhenFrozenUsingCast.
/**
* Check that casting to Advised can't get around advice freeze.
*/
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertFalse(pc.isFrozen());
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
Advised advised = (Advised) proxied;
assertTrue(pc.isFrozen());
try {
advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
fail("Shouldn't be able to add Advisor when frozen");
} catch (AopConfigException ex) {
assertTrue(ex.getMessage().contains("frozen"));
}
// Check it still works: proxy factory state shouldn't have been corrupted
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, advised.getAdvisors().length);
}
use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testUndeclaredCheckedException.
/**
* An interceptor throws a checked exception not on the method signature.
* For efficiency, we don't bother unifying java.lang.reflect and
* org.springframework.cglib UndeclaredThrowableException
*/
@Test
public void testUndeclaredCheckedException() throws Throwable {
final Exception unexpectedException = new Exception();
// 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();
try {
// Note: exception param below isn't used
tb.getAge();
fail("Should have wrapped exception raised by interceptor");
} catch (UndeclaredThrowableException thrown) {
assertEquals("exception matches", unexpectedException, thrown.getUndeclaredThrowable());
} catch (Exception ex) {
ex.printStackTrace();
fail("Didn't expect exception: " + ex);
}
}
Aggregations