Search in sources :

Example 1 with Lockable

use of test.mixin.Lockable in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testTestBeanIntroduction.

private void testTestBeanIntroduction(ProxyFactory pc) {
    int newAge = 65;
    ITestBean itb = (ITestBean) createProxy(pc);
    itb.setAge(newAge);
    assertEquals(newAge, itb.getAge());
    Lockable lockable = (Lockable) itb;
    assertFalse(lockable.locked());
    lockable.lock();
    assertEquals(newAge, itb.getAge());
    try {
        itb.setAge(1);
        fail("Setters should fail when locked");
    } catch (LockedException ex) {
    // ok
    }
    assertEquals(newAge, itb.getAge());
    // Unlock
    assertTrue(lockable.locked());
    lockable.unlock();
    itb.setAge(1);
    assertEquals(1, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) LockedException(test.mixin.LockedException) Lockable(test.mixin.Lockable)

Example 2 with Lockable

use of test.mixin.Lockable in project spring-framework by spring-projects.

the class NonAnnotatedMakeLockable method testLockingWorks.

// TODO if you change type pattern from org.springframework.beans..*
// to org.springframework..* it also matches introduction.
// Perhaps generated advisor bean definition could be made to depend
// on the introduction, in which case this would not be a problem.
@Test
public void testLockingWorks() {
    Assume.group(TestGroup.LONG_RUNNING);
    Object introductionObject = ctx.getBean("introduction");
    assertFalse("Introduction should not be proxied", AopUtils.isAopProxy(introductionObject));
    Lockable lockable = (Lockable) testBeanProxy;
    assertFalse(lockable.locked());
    // Invoke a non-advised method
    testBeanProxy.getAge();
    testBeanProxy.setName("");
    lockable.lock();
    try {
        testBeanProxy.setName(" ");
        fail("Should be locked");
    } catch (IllegalStateException ex) {
    // expected
    }
}
Also used : Lockable(test.mixin.Lockable) Test(org.junit.Test)

Example 3 with Lockable

use of test.mixin.Lockable in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testPrototypeAdvisor.

@Test
public void testPrototypeAdvisor() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(CONTEXT, CLASS));
    ITestBean bean1 = (ITestBean) bf.getBean("prototypeTestBeanProxy");
    ITestBean bean2 = (ITestBean) bf.getBean("prototypeTestBeanProxy");
    bean1.setAge(3);
    bean2.setAge(4);
    assertEquals(3, bean1.getAge());
    assertEquals(4, bean2.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 4 with Lockable

use of test.mixin.Lockable 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 5 with Lockable

use of test.mixin.Lockable in project spring-framework by spring-projects.

the class CreatesTestBean method testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean.

@Test
public void testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
    ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals("NOP should not have done any work yet", 0, nop.getCount());
    assertTrue(AopUtils.isJdkDynamicProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertTrue("Introduction was made", tb instanceof TimeStamped);
    assertEquals(0, ((TimeStamped) tb).getTimeStamp());
    assertEquals(3, nop.getCount());
    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    tb.setAge(65);
    assertEquals(65, tb.getAge());
    lockable1.lock();
    assertTrue(lockable1.locked());
    // Shouldn't affect second
    assertFalse(lockable2.locked());
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    try {
        tb.setAge(6);
        fail("Mixin should have locked this object");
    } catch (LockedException ex) {
    // Ok
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TimeStamped(org.springframework.tests.TimeStamped) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) LockedException(test.mixin.LockedException) Lockable(test.mixin.Lockable) Test(org.junit.Test)

Aggregations

Lockable (test.mixin.Lockable)7 Test (org.junit.Test)6 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 LockedException (test.mixin.LockedException)5 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 TimeStamped (org.springframework.tests.TimeStamped)2 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1