use of org.springframework.aop.support.DefaultIntroductionAdvisor in project spring-framework by spring-projects.
the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.
/**
* Should see effect immediately on behavior.
*/
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
ProxyFactory config = new ProxyFactory(new TestBean());
assertFalse("Shouldn't implement TimeStamped before manipulation", config.getProxy() instanceof TimeStamped);
long time = 666L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
ti.setTime(time);
// Add to front of interceptor chain
int oldCount = config.getAdvisors().length;
config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
assertTrue(config.getAdvisors().length == oldCount + 1);
TimeStamped ts = (TimeStamped) config.getProxy();
assertTrue(ts.getTimeStamp() == time);
// Can remove
config.removeAdvice(ti);
assertTrue(config.getAdvisors().length == oldCount);
try {
// Existing reference will fail
ts.getTimeStamp();
fail("Existing object won't implement this interface any more");
} catch (RuntimeException ex) {
}
assertFalse("Should no longer implement TimeStamped", config.getProxy() instanceof TimeStamped);
// Now check non-effect of removing interceptor that isn't there
config.removeAdvice(new DebugInterceptor());
assertTrue(config.getAdvisors().length == oldCount);
ITestBean it = (ITestBean) ts;
DebugInterceptor debugInterceptor = new DebugInterceptor();
config.addAdvice(0, debugInterceptor);
it.getSpouse();
assertEquals(1, debugInterceptor.getCount());
config.removeAdvice(debugInterceptor);
it.getSpouse();
// not invoked again
assertTrue(debugInterceptor.getCount() == 1);
}
use of org.springframework.aop.support.DefaultIntroductionAdvisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testIntroductionThrowsUncheckedException.
/**
* Note that an introduction can't throw an unexpected checked exception,
* as it's constained by the interface.
*/
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@SuppressWarnings("serial")
class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {
/**
* @see test.util.TimeStamped#getTimeStamp()
*/
@Override
public long getTimeStamp() {
throw new UnsupportedOperationException();
}
}
pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));
TimeStamped ts = (TimeStamped) createProxy(pc);
try {
ts.getTimeStamp();
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException ex) {
}
}
use of org.springframework.aop.support.DefaultIntroductionAdvisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testUseAsHashKey.
@Test
public void testUseAsHashKey() {
TestBean target1 = new TestBean();
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvice(new NopInterceptor());
ITestBean proxy1 = (ITestBean) createProxy(pf1);
TestBean target2 = new TestBean();
ProxyFactory pf2 = new ProxyFactory(target2);
pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
ITestBean proxy2 = (ITestBean) createProxy(pf2);
HashMap<ITestBean, Object> h = new HashMap<>();
Object value1 = "foo";
Object value2 = "bar";
assertNull(h.get(proxy1));
h.put(proxy1, value1);
h.put(proxy2, value2);
assertEquals(h.get(proxy1), value1);
assertEquals(h.get(proxy2), value2);
}
Aggregations