use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testDelegateReturnsThisIsMassagedToReturnProxy.
@Test
public void testDelegateReturnsThisIsMassagedToReturnProxy() {
NestedTestBean target = new NestedTestBean();
String company = "Interface21";
target.setCompany(company);
TestBean delegate = new TestBean() {
@Override
public ITestBean getSpouse() {
return this;
}
};
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DelegatingIntroductionInterceptor(delegate));
INestedTestBean proxy = (INestedTestBean) pf.getProxy();
assertEquals(company, proxy.getCompany());
ITestBean introduction = (ITestBean) proxy;
assertSame("Introduced method returning delegate returns proxy", introduction, introduction.getSpouse());
assertTrue("Introduced method returning delegate returns proxy", AopUtils.isAopProxy(introduction.getSpouse()));
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorDoesntReplaceToString.
@SuppressWarnings("serial")
@Test
public void testIntroductionInterceptorDoesntReplaceToString() throws Exception {
TestBean raw = new TestBean();
assertTrue(!(raw instanceof TimeStamped));
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = new SerializableTimeStamped(0);
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts) {
@Override
public String toString() {
throw new UnsupportedOperationException("Shouldn't be invoked");
}
}));
TimeStamped tsp = (TimeStamped) factory.getProxy();
assertEquals(0, tsp.getTimeStamp());
assertEquals(raw.toString(), tsp.toString());
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInSubclass.
@Test
public void testAutomaticInterfaceRecognitionInSubclass() throws Exception {
final long t = 1001L;
@SuppressWarnings("serial")
class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITester {
@Override
public void foo() throws Exception {
}
@Override
public long getTimeStamp() {
return t;
}
}
DelegatingIntroductionInterceptor ii = new TestII();
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii);
assertTrue(ia.isPerInstance());
pf.addAdvisor(0, ia);
//assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
TimeStamped ts = (TimeStamped) pf.getProxy();
assertThat(ts, instanceOf(TimeStamped.class));
// Shoulnd't proxy framework interfaces
assertTrue(!(ts instanceof MethodInterceptor));
assertTrue(!(ts instanceof IntroductionInterceptor));
assertTrue(ts.getTimeStamp() == t);
((ITester) ts).foo();
((ITestBean) ts).getAge();
// Test removal
ii.suppressInterface(TimeStamped.class);
// Note that we need to construct a new proxy factory,
// or suppress the interface on the proxy factory
pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
Object o = pf.getProxy();
assertTrue(!(o instanceof TimeStamped));
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testSerializableDelegatingIntroductionInterceptorSerializable.
@Test
public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception {
SerializablePerson serializableTarget = new SerializablePerson();
String name = "Tony";
serializableTarget.setName("Tony");
ProxyFactory factory = new ProxyFactory(serializableTarget);
factory.addInterface(Person.class);
long time = 1000;
TimeStamped ts = new SerializableTimeStamped(time);
factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
factory.addAdvice(new SerializableNopInterceptor());
Person p = (Person) factory.getProxy();
assertEquals(name, p.getName());
assertEquals(time, ((TimeStamped) p).getTimeStamp());
Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
assertEquals(name, p1.getName());
assertEquals(time, ((TimeStamped) p1).getTimeStamp());
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testIntroductionMasksTargetImplementation.
// Test when target implements the interface: should get interceptor by preference.
@Test
public void testIntroductionMasksTargetImplementation() throws Exception {
final long t = 1001L;
@SuppressWarnings("serial")
class TestII extends DelegatingIntroductionInterceptor implements TimeStamped {
@Override
public long getTimeStamp() {
return t;
}
}
DelegatingIntroductionInterceptor ii = new TestII();
// != t
TestBean target = new TargetClass(t + 1);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
TimeStamped ts = (TimeStamped) pf.getProxy();
// From introduction interceptor, not target
assertTrue(ts.getTimeStamp() == t);
}
Aggregations