Search in sources :

Example 1 with Lockable

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

the class PerThisAspect method testIntroductionOnTargetNotImplementingInterface.

/**
	 * In this case the introduction will be made.
	 */
@Test
public void testIntroductionOnTargetNotImplementingInterface() {
    NotLockable notLockableTarget = new NotLockable();
    assertFalse(notLockableTarget instanceof Lockable);
    NotLockable notLockable1 = (NotLockable) createProxy(notLockableTarget, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), NotLockable.class);
    assertTrue(notLockable1 instanceof Lockable);
    Lockable lockable = (Lockable) notLockable1;
    assertFalse(lockable.locked());
    lockable.lock();
    assertTrue(lockable.locked());
    NotLockable notLockable2Target = new NotLockable();
    NotLockable notLockable2 = (NotLockable) createProxy(notLockable2Target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), NotLockable.class);
    assertTrue(notLockable2 instanceof Lockable);
    Lockable lockable2 = (Lockable) notLockable2;
    assertFalse(lockable2.locked());
    notLockable2.setIntValue(1);
    lockable2.lock();
    try {
        notLockable2.setIntValue(32);
        fail();
    } catch (IllegalStateException ex) {
    }
    assertTrue(lockable2.locked());
}
Also used : Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) Test(org.junit.Test)

Example 2 with Lockable

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

the class PerThisAspect method testIntroductionWithArgumentBinding.

/* prereq AspectJ 1.6.7
	@Test
	public void testIntroductionBasedOnAnnotationMatch_Spr5307() {
		AnnotatedTarget target = new AnnotatedTargetImpl();

		List<Advisor> advisors = getFixture().getAdvisors(
				new SingletonMetadataAwareAspectInstanceFactory(new MakeAnnotatedTypeModifiable(),"someBean"));
		Object proxy = createProxy(target,
				advisors,
				AnnotatedTarget.class);
		System.out.println(advisors.get(1));
		assertTrue(proxy instanceof Lockable);
		Lockable lockable = (Lockable)proxy;
		lockable.locked();
	}
	*/
// TODO: Why does this test fail? It hasn't been run before, so it maybe never actually passed...
@Test
@Ignore
public void testIntroductionWithArgumentBinding() {
    TestBean target = new TestBean();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeITestBeanModifiable(), "someBean"));
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")));
    Modifiable modifiable = (Modifiable) createProxy(target, advisors, ITestBean.class);
    assertThat(modifiable, instanceOf(Modifiable.class));
    Lockable lockable = (Lockable) modifiable;
    assertFalse(lockable.locked());
    ITestBean itb = (ITestBean) modifiable;
    assertFalse(modifiable.isModified());
    int oldAge = itb.getAge();
    itb.setAge(oldAge + 1);
    assertTrue(modifiable.isModified());
    modifiable.acceptChanges();
    assertFalse(modifiable.isModified());
    itb.setAge(itb.getAge());
    assertFalse("Setting same value does not modify", modifiable.isModified());
    itb.setName("And now for something completely different");
    assertTrue(modifiable.isModified());
    lockable.lock();
    assertTrue(lockable.locked());
    try {
        itb.setName("Else");
        fail("Should be locked");
    } catch (IllegalStateException ex) {
    // Ok
    }
    lockable.unlock();
    itb.setName("Tony");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Lockable

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

the class PerThisAspect method testIntroductionOnTargetImplementingInterface.

@Test
public void testIntroductionOnTargetImplementingInterface() {
    CannotBeUnlocked target = new CannotBeUnlocked();
    Lockable proxy = (Lockable) createProxy(target, // Ensure that we exclude
    AopUtils.findAdvisorsThatCanApply(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), CannotBeUnlocked.class), CannotBeUnlocked.class);
    assertThat(proxy, instanceOf(Lockable.class));
    Lockable lockable = proxy;
    assertTrue("Already locked", lockable.locked());
    lockable.lock();
    assertTrue("Real target ignores locking", lockable.locked());
    try {
        lockable.unlock();
        fail();
    } catch (UnsupportedOperationException ex) {
    // Ok
    }
}
Also used : Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 DefaultLockable (test.aop.DefaultLockable)3 Lockable (test.aop.Lockable)3 JoinPoint (org.aspectj.lang.JoinPoint)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Ignore (org.junit.Ignore)1 Advisor (org.springframework.aop.Advisor)1 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1