Search in sources :

Example 36 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testPrototypeInterceptorSingletonTarget.

@Test
public void testPrototypeInterceptorSingletonTarget() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(CONTEXT, CLASS));
    ITestBean bean1 = (ITestBean) bf.getBean("prototypeTestBeanProxySingletonTarget");
    ITestBean bean2 = (ITestBean) bf.getBean("prototypeTestBeanProxySingletonTarget");
    bean1.setAge(1);
    bean2.setAge(2);
    assertEquals(2, bean1.getAge());
    ((Lockable) bean1).lock();
    try {
        bean1.setAge(5);
        fail("expected LockedException");
    } catch (LockedException ex) {
    // expected
    }
    try {
        bean2.setAge(6);
    } catch (LockedException ex) {
        fail("did not expect LockedException");
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) LockedException(test.mixin.LockedException) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Lockable(test.mixin.Lockable) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 37 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testIsDynamicProxyWhenInterfaceSpecified.

@Test
public void testIsDynamicProxyWhenInterfaceSpecified() {
    ITestBean test1 = (ITestBean) factory.getBean("test1");
    assertTrue("test1 is a dynamic proxy", Proxy.isProxyClass(test1.getClass()));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) Test(org.junit.Test)

Example 38 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testIsDynamicProxyWhenAutodetectingInterfacesForPrototype.

@Test
public void testIsDynamicProxyWhenAutodetectingInterfacesForPrototype() {
    ITestBean test1 = (ITestBean) factory.getBean("test4");
    assertTrue("test4 is a dynamic proxy", Proxy.isProxyClass(test1.getClass()));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) Test(org.junit.Test)

Example 39 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class UnsupportedInterceptor method testMultipleProxies.

@Test
public void testMultipleProxies() {
    TestBean target = new TestBean();
    target.setAge(20);
    TestBean target2 = new TestBean();
    target2.setAge(21);
    ITestBean proxy1 = getAdvisedProxy(target);
    ITestBean proxy2 = getAdvisedProxy(target2);
    assertSame(proxy1.getClass(), proxy2.getClass());
    assertEquals(target.getAge(), proxy1.getAge());
    assertEquals(target2.getAge(), proxy2.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 40 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class UnsupportedInterceptor method testProxyAProxyWithAdditionalInterface.

@Test
public void testProxyAProxyWithAdditionalInterface() {
    ITestBean target = new TestBean();
    mockTargetSource.setTarget(target);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    as.addInterface(Serializable.class);
    CglibAopProxy cglib = new CglibAopProxy(as);
    ITestBean proxy1 = (ITestBean) cglib.getProxy();
    mockTargetSource.setTarget(proxy1);
    as = new AdvisedSupport(new Class<?>[] {});
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    cglib = new CglibAopProxy(as);
    ITestBean proxy2 = (ITestBean) cglib.getProxy();
    assertTrue(proxy2 instanceof Serializable);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Serializable(java.io.Serializable) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Aggregations

ITestBean (org.springframework.tests.sample.beans.ITestBean)221 Test (org.junit.Test)205 TestBean (org.springframework.tests.sample.beans.TestBean)127 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)37 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)24 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 IOException (java.io.IOException)15 Advisor (org.springframework.aop.Advisor)15 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)15 Method (java.lang.reflect.Method)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)14 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)13 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)13 MethodInvocation (org.aopalliance.intercept.MethodInvocation)12 Advised (org.springframework.aop.framework.Advised)12 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)11 LockedException (test.mixin.LockedException)11 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)10