Search in sources :

Example 6 with Lockable

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

the class CreatesTestBean method testJdkIntroduction.

@Test
public void testJdkIntroduction() {
    ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals(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());
    assertEquals("introductionUsingJdk", tb.getName());
    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)

Example 7 with Lockable

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

the class SelectivePrototypeTargetSourceCreator method testCommonInterceptorAndAdvisor.

/**
	 * Check that we can provide a common interceptor that will
	 * appear in the chain before "specific" interceptors,
	 * which are sourced from matching advisors
	 */
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
    BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
    ITestBean test1 = (ITestBean) bf.getBean("test1");
    assertTrue(AopUtils.isAopProxy(test1));
    Lockable lockable1 = (Lockable) test1;
    NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
    NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();
    ITestBean test2 = (ITestBean) bf.getBean("test2");
    Lockable lockable2 = (Lockable) test2;
    // Locking should be independent; nop is shared
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    // equals 2 calls on shared nop, because it's first and sees calls
    // against the Lockable interface introduced by the specific advisor
    assertEquals(2, nop1.getCount());
    assertEquals(0, nop2.getCount());
    lockable1.lock();
    assertTrue(lockable1.locked());
    assertFalse(lockable2.locked());
    assertEquals(5, nop1.getCount());
    assertEquals(0, nop2.getCount());
    PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
    assertEquals(5, nop1.getCount());
    assertEquals(0, nop2.getCount());
    packageVisibleMethod.doSomething();
    assertEquals(6, nop1.getCount());
    assertEquals(1, nop2.getCount());
    assertTrue(packageVisibleMethod instanceof Lockable);
    Lockable lockable3 = (Lockable) packageVisibleMethod;
    lockable3.lock();
    assertTrue(lockable3.locked());
    lockable3.unlock();
    assertFalse(lockable3.locked());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BeanFactory(org.springframework.beans.factory.BeanFactory) 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