use of org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddIntroductionAdviceWithUnimplementedInterface.
/**
* Check that the introduction advice isn't allowed to introduce interfaces
* that are unsupported by the IntroductionInterceptor.
*/
@Test
public void testCannotAddIntroductionAdviceWithUnimplementedInterface() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
try {
pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), ITestBean.class));
fail("Shouldn't be able to add introduction advice introducing an unimplemented interface");
} catch (IllegalArgumentException 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.aop.interceptor.TimestampIntroductionInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddIntroductionAdviceToIntroduceClass.
/**
* Should only be able to introduce interfaces, not classes.
*/
@Test
public void testCannotAddIntroductionAdviceToIntroduceClass() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
try {
pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), TestBean.class));
fail("Shouldn't be able to add introduction advice that introduces a class, rather than an interface");
} catch (IllegalArgumentException ex) {
assertTrue(ex.getMessage().contains("interface"));
}
// 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.aop.interceptor.TimestampIntroductionInterceptor in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testCanAddAndRemoveAspectInterfacesOnPrototype.
/**
* Try adding and removing interfaces and interceptors on prototype.
* Changes will only affect future references obtained from the factory.
* Each instance will be independent.
*/
@Test
public void testCanAddAndRemoveAspectInterfacesOnPrototype() {
assertThat("Shouldn't implement TimeStamped before manipulation", factory.getBean("test2"), not(instanceOf(TimeStamped.class)));
ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2");
long time = 666L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
ti.setTime(time);
// Add to head 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) factory.getBean("test2");
assertEquals(time, ts.getTimeStamp());
// Can remove
config.removeAdvice(ti);
assertTrue(config.getAdvisors().length == oldCount);
// Check no change on existing object reference
assertTrue(ts.getTimeStamp() == time);
assertThat("Should no longer implement TimeStamped", factory.getBean("test2"), not(instanceOf(TimeStamped.class)));
// 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();
// Won't affect existing reference
assertTrue(debugInterceptor.getCount() == 0);
it = (ITestBean) factory.getBean("test2");
it.getSpouse();
assertEquals(1, debugInterceptor.getCount());
config.removeAdvice(debugInterceptor);
it.getSpouse();
// Still invoked wiht old reference
assertEquals(2, debugInterceptor.getCount());
// not invoked with new object
it = (ITestBean) factory.getBean("test2");
it.getSpouse();
assertEquals(2, debugInterceptor.getCount());
// Our own timestamped reference should still work
assertEquals(time, ts.getTimeStamp());
}
use of org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testValuesStick.
/**
* Simple test that if we set values we can get them out again.
*/
@Test
public void testValuesStick() {
int age1 = 33;
int age2 = 37;
String name = "tony";
TestBean target1 = new TestBean();
target1.setAge(age1);
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor()));
ITestBean tb = (ITestBean) pf1.getProxy();
assertEquals(age1, tb.getAge());
tb.setAge(age2);
assertEquals(age2, tb.getAge());
assertNull(tb.getName());
tb.setName(name);
assertEquals(name, tb.getName());
}
use of org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor 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