use of org.springframework.tests.aop.interceptor.NopInterceptor 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.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testBeforeAdviceThrowsException.
@Test
public void testBeforeAdviceThrowsException() {
final RuntimeException rex = new RuntimeException();
@SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {
@Override
public void before(Method m, Object[] args, Object target) throws Throwable {
super.before(m, args, target);
if (m.getName().startsWith("set"))
throw rex;
}
};
TestBean target = new TestBean();
target.setAge(80);
NopInterceptor nop1 = new NopInterceptor();
NopInterceptor nop2 = new NopInterceptor();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(nop1);
pf.addAdvice(ba);
pf.addAdvice(nop2);
ITestBean proxied = (ITestBean) createProxy(pf);
// Won't throw an exception
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, ba.getCalls());
assertEquals(1, ba.getCalls("getAge"));
assertEquals(1, nop1.getCount());
assertEquals(1, nop2.getCount());
// Will fail, after invoking Nop1
try {
proxied.setAge(26);
fail("before advice should have ended chain");
} catch (RuntimeException ex) {
assertEquals(rex, ex);
}
assertEquals(2, ba.getCalls());
assertEquals(2, nop1.getCount());
// Nop2 didn't get invoked when the exception was thrown
assertEquals(1, nop2.getCount());
// Shouldn't have changed value in joinpoint
assertEquals(target.getAge(), proxied.getAge());
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testDynamicMethodPointcutThatAlwaysAppliesStatically.
@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
pc.addAdvisor(dp);
pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc);
assertEquals(dp.count, 0);
it.getAge();
assertEquals(dp.count, 1);
it.setAge(11);
assertEquals(it.getAge(), 11);
assertEquals(dp.count, 2);
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class MBeanExporterTests method testExportJdkProxy.
@Test
public void testExportJdkProxy() throws Exception {
JmxTestBean bean = new JmxTestBean();
bean.setName("Rob Harrop");
ProxyFactory factory = new ProxyFactory();
factory.setTarget(bean);
factory.addAdvice(new NopInterceptor());
factory.setInterfaces(IJmxTestBean.class);
IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
String name = "bean:mmm=whatever";
Map<String, Object> beans = new HashMap<>();
beans.put(name, proxy);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.registerBeans();
ObjectName oname = ObjectName.getInstance(name);
Object nameValue = server.getAttribute(oname, "Name");
assertEquals("Rob Harrop", nameValue);
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testExistingProxyChangesTarget.
@Test
public void testExistingProxyChangesTarget() throws Throwable {
TestBean tb1 = new TestBean();
tb1.setAge(33);
TestBean tb2 = new TestBean();
tb2.setAge(26);
tb2.setName("Juergen");
TestBean tb3 = new TestBean();
tb3.setAge(37);
ProxyFactory pc = new ProxyFactory(tb1);
NopInterceptor nop = new NopInterceptor();
pc.addAdvice(nop);
ITestBean proxy = (ITestBean) createProxy(pc);
assertEquals(nop.getCount(), 0);
assertEquals(tb1.getAge(), proxy.getAge());
assertEquals(nop.getCount(), 1);
// Change to a new static target
pc.setTarget(tb2);
assertEquals(tb2.getAge(), proxy.getAge());
assertEquals(nop.getCount(), 2);
// Change to a new dynamic target
HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
pc.setTargetSource(hts);
assertEquals(tb3.getAge(), proxy.getAge());
assertEquals(nop.getCount(), 3);
hts.swap(tb1);
assertEquals(tb1.getAge(), proxy.getAge());
tb1.setName("Colin");
assertEquals(tb1.getName(), proxy.getName());
assertEquals(nop.getCount(), 5);
// Change back, relying on casting to Advised
Advised advised = (Advised) proxy;
assertSame(hts, advised.getTargetSource());
SingletonTargetSource sts = new SingletonTargetSource(tb2);
advised.setTargetSource(sts);
assertEquals(tb2.getName(), proxy.getName());
assertSame(sts, advised.getTargetSource());
assertEquals(tb2.getAge(), proxy.getAge());
}
Aggregations