Search in sources :

Example 96 with TestBean

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

the class IntroductionBenchmarkTests method timeManyInvocations.

@Test
public void timeManyInvocations() {
    StopWatch sw = new StopWatch();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.setProxyTargetClass(false);
    pf.addAdvice(new SimpleCounterIntroduction());
    ITestBean proxy = (ITestBean) pf.getProxy();
    Counter counter = (Counter) proxy;
    sw.start(INVOCATIONS + " invocations on proxy, not hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        proxy.getAge();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on proxy, hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        counter.getCount();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on target");
    for (int i = 0; i < INVOCATIONS; i++) {
        target.getAge();
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StopWatch(org.springframework.util.StopWatch) Test(org.junit.jupiter.api.Test)

Example 97 with TestBean

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

the class ProxyFactoryTests method testIndexOfMethods.

@Test
public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
    assertThat(pf.indexOf(nop)).isEqualTo(0);
    assertThat(pf.indexOf(advisor)).isEqualTo(1);
    assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 98 with TestBean

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

the class PerThisAspect method multiplePerTargetAspects.

@Test
void multiplePerTargetAspects() throws Exception {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new ArrayList<>();
    PerTargetAspect aspect1 = new PerTargetAspect();
    aspect1.count = 100;
    aspect1.setOrder(10);
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspect aspect2 = new PerTargetAspect();
    aspect2.setOrder(5);
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    OrderComparator.sort(advisors);
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0);
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1);
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ArrayList(java.util.ArrayList) Advisor(org.springframework.aop.Advisor) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 99 with TestBean

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

the class PerThisAspect method multiplePerTargetAspectsWithOrderAnnotation.

@Test
void multiplePerTargetAspectsWithOrderAnnotation() throws Exception {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new ArrayList<>();
    PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10();
    aspect1.count = 100;
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    OrderComparator.sort(advisors);
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    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) ArrayList(java.util.ArrayList) Advisor(org.springframework.aop.Advisor) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 100 with TestBean

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

the class PerThisAspect method bindingWithSingleArg.

@Test
void bindingWithSingleArg() {
    TestBean target = new TestBean();
    ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new BindingAspectWithSingleArg(), "someBean")), ITestBean.class);
    itb.setAge(10);
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(20);
    assertThat(target.getAge()).isEqualTo(20);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) 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