Search in sources :

Example 66 with ProxyFactory

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()));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) Test(org.junit.Test)

Example 67 with ProxyFactory

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());
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 68 with ProxyFactory

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));
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IntroductionInterceptor(org.springframework.aop.IntroductionInterceptor) TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) Test(org.junit.Test)

Example 69 with ProxyFactory

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());
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) ProxyFactory(org.springframework.aop.framework.ProxyFactory) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Person(org.springframework.tests.sample.beans.Person) Test(org.junit.Test)

Example 70 with ProxyFactory

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);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Aggregations

ProxyFactory (org.springframework.aop.framework.ProxyFactory)81 Test (org.junit.Test)49 ITestBean (org.springframework.tests.sample.beans.ITestBean)20 TestBean (org.springframework.tests.sample.beans.TestBean)20 TimeStamped (org.springframework.tests.TimeStamped)8 INestedTestBean (org.springframework.tests.sample.beans.INestedTestBean)8 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)8 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)5 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)4 Context (javax.naming.Context)3 TargetSource (org.springframework.aop.TargetSource)3 Advised (org.springframework.aop.framework.Advised)3 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)3 Factory (org.springframework.cglib.proxy.Factory)3 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)3 IOException (java.io.IOException)2 EJBLocalObject (javax.ejb.EJBLocalObject)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2