Search in sources :

Example 86 with TestBean

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

the class AopNamespaceHandlerScopeIntegrationTests method testSingletonScoping.

@Test
void testSingletonScoping() throws Exception {
    assertThat(AopUtils.isAopProxy(singletonScoped)).as("Should be AOP proxy").isTrue();
    boolean condition = singletonScoped instanceof TestBean;
    assertThat(condition).as("Should be target class proxy").isTrue();
    String rob = "Rob Harrop";
    String bram = "Bram Smeets";
    assertThat(singletonScoped.getName()).isEqualTo(rob);
    singletonScoped.setName(bram);
    assertThat(singletonScoped.getName()).isEqualTo(bram);
    ITestBean deserialized = SerializationTestUtils.serializeAndDeserialize(singletonScoped);
    assertThat(deserialized.getName()).isEqualTo(bram);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 87 with TestBean

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

the class PerThisAspect method namedPointcuts.

private void namedPointcuts(Object aspectInstance) {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspectInstance, "someBean")), ITestBean.class);
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(-1);
    assertThat(target.getAge()).isEqualTo(realAge);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 88 with TestBean

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

the class PerThisAspect method declarePrecedenceNotSupported.

@Test
void declarePrecedenceNotSupported() {
    TestBean target = new TestBean();
    assertThatIllegalArgumentException().isThrownBy(() -> {
        MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(new DeclarePrecedenceShouldSucceed(), "someBean");
        createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class);
    });
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 89 with TestBean

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

the class PerThisAspect method perThisAspect.

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

Example 90 with TestBean

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

the class PerThisAspect method twoAdvicesOnOneAspect.

@Test
void twoAdvicesOnOneAspect() {
    TestBean target = new TestBean();
    TwoAdviceAspect twoAdviceAspect = new TwoAdviceAspect();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(twoAdviceAspect, "someBean"));
    assertThat(advisors.size()).as("Two advice methods found").isEqualTo(2);
    ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
    itb.setName("");
    assertThat(itb.getAge()).isEqualTo(0);
    int newAge = 32;
    itb.setAge(newAge);
    assertThat(itb.getAge()).isEqualTo(1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advisor(org.springframework.aop.Advisor) TwoAdviceAspect(test.aop.TwoAdviceAspect) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28