Search in sources :

Example 91 with TestBean

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

the class CallCountingInterceptor method testSimpleAdvice.

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

Example 92 with TestBean

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

the class MethodInvocationProceedingJoinPointTests method testCanGetSourceLocationFromJoinPoint.

@Test
public void testCanGetSourceLocationFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()).as("Same source location must be returned on subsequent requests").isEqualTo(sloc);
        assertThat(sloc.getWithinType()).isEqualTo(TestBean.class);
        assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getLine);
        assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getFileName);
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SourceLocation(org.aspectj.lang.reflect.SourceLocation) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 93 with TestBean

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

the class MethodInvocationProceedingJoinPointTests method testCanGetStaticPartFromJoinPoint.

@Test
public void testCanGetStaticPartFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart);
        assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION);
        assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature());
        assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) Test(org.junit.jupiter.api.Test)

Example 94 with TestBean

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

the class MethodInvocationProceedingJoinPointTests method testCanGetMethodSignatureFromJoinPoint.

@Test
public void testCanGetMethodSignatureFromJoinPoint() {
    final Object raw = new TestBean();
    // Will be set by advice during a method call
    final int newAge = 23;
    ProxyFactory pf = new ProxyFactory(raw);
    pf.setExposeProxy(true);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    AtomicInteger depth = new AtomicInteger();
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
        assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
        // Ensure that these don't cause problems
        jp.toShortString();
        jp.toLongString();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(target);
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
        ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
        assertThat(thisProxy).isNotSameAs(target);
        // Check getting again doesn't cause a problem
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
        // Be sure to increment depth to avoid infinite recursion
        if (depth.getAndIncrement() == 0) {
            // Check that toString doesn't cause a problem
            thisProxy.toString();
            // Change age, so this will be returned by invocation
            thisProxy.setAge(newAge);
            assertThat(thisProxy.getAge()).isEqualTo(newAge);
        }
        assertThat(thisProxy).isSameAs(AopContext.currentProxy());
        assertThat(raw).isSameAs(target);
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getName());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getModifiers());
        MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
        assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
        assertThat(msig.getDeclaringType()).isEqualTo(method.getDeclaringClass());
        assertThat(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes())).isTrue();
        assertThat(msig.getReturnType()).isEqualTo(method.getReturnType());
        assertThat(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())).isTrue();
        msig.toLongString();
        msig.toShortString();
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    assertThat(itb.getAge()).as("Advice reentrantly set age").isEqualTo(newAge);
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 95 with TestBean

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

the class AopProxyUtilsTests method testProxiedUserInterfacesWithMultipleInterfaces.

@Test
public void testProxiedUserInterfacesWithMultipleInterfaces() {
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(new TestBean());
    pf.addInterface(ITestBean.class);
    pf.addInterface(Comparable.class);
    Object proxy = pf.getProxy();
    Class<?>[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy);
    assertThat(userInterfaces.length).isEqualTo(2);
    assertThat(userInterfaces[0]).isEqualTo(ITestBean.class);
    assertThat(userInterfaces[1]).isEqualTo(Comparable.class);
}
Also used : 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