Search in sources :

Example 1 with TestBean

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

the class PerThisAspect method testPerTargetAspect.

@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")), TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    Advised advised = (Advised) itb;
    SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
    assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
    LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
    assertFalse(maaif.isMaterialized());
    // Check that the perclause pointcut is valid
    assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertTrue(maaif.isMaterialized());
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Advised(org.springframework.aop.framework.Advised) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Test(org.junit.Test)

Example 2 with TestBean

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

the class PerThisAspect method testFailureWithoutExplicitDeclarePrecedence.

@Test
public void testFailureWithoutExplicitDeclarePrecedence() {
    TestBean target = new TestBean();
    MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(new NoDeclarePrecedenceShouldFail(), "someBean");
    ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class);
    itb.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 3 with TestBean

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

the class PerThisAspect method testPerThisAspect.

@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")), TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    Advised advised = (Advised) itb;
    // Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
    assertEquals(4, advised.getAdvisors().length);
    SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
    assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
    LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
    assertFalse(maaif.isMaterialized());
    // Check that the perclause pointcut is valid
    assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertTrue(maaif.isMaterialized());
    assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Advised(org.springframework.aop.framework.Advised) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Test(org.junit.Test)

Example 4 with TestBean

use of org.springframework.tests.sample.beans.TestBean 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 5 with TestBean

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

the class CallCountingInterceptor method testSimpleAdvice.

@Test
public void testSimpleAdvice() {
    String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())";
    CallCountingInterceptor interceptor = new CallCountingInterceptor();
    TestBean testBean = getAdvisedProxy(expression, interceptor);
    assertEquals("Calls should be 0", 0, interceptor.getCount());
    testBean.getAge();
    assertEquals("Calls should be 1", 1, interceptor.getCount());
    testBean.setAge(90);
    assertEquals("Calls should still be 1", 1, interceptor.getCount());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)788 Test (org.junit.Test)740 ITestBean (org.springframework.tests.sample.beans.ITestBean)456 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)248 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)225 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)164 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)160 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)144 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)86 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)44 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)40 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)40 Properties (java.util.Properties)35 Errors (org.springframework.validation.Errors)34 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)33 PropertyEditorSupport (java.beans.PropertyEditorSupport)31 HashMap (java.util.HashMap)30 List (java.util.List)27 Map (java.util.Map)27 PageContext (javax.servlet.jsp.PageContext)27