Search in sources :

Example 71 with ITestBean

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

the class AbstractAopProxyTests method testCannotAddIntroductionAdviceToIntroduceClass.

/**
 * Should only be able to introduce interfaces, not classes.
 */
@Test
public void testCannotAddIntroductionAdviceToIntroduceClass() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertThatIllegalArgumentException().as("Shouldn't be able to add introduction advice that introduces a class, rather than an interface").isThrownBy(() -> pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), TestBean.class))).withMessageContaining("interface");
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Example 72 with ITestBean

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

the class AbstractAopProxyTests method testUndeclaredCheckedException.

/**
 * An interceptor throws a checked exception not on the method signature.
 * For efficiency, we don't bother unifying java.lang.reflect and
 * org.springframework.cglib UndeclaredThrowableException
 */
@Test
public void testUndeclaredCheckedException() throws Throwable {
    final Exception unexpectedException = new Exception();
    // Test return value
    MethodInterceptor mi = invocation -> {
        throw unexpectedException;
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    pc.addAdvice(mi);
    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(tb::getAge).satisfies(ex -> assertThat(ex.getUndeclaredThrowable()).isEqualTo(unexpectedException));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixin(test.mixin.LockMixin) AopUtils(org.springframework.aop.support.AopUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) TargetSource(org.springframework.aop.TargetSource) Pointcuts(org.springframework.aop.support.Pointcuts) Lockable(test.mixin.Lockable) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) Map(java.util.Map) TestBean(org.springframework.beans.testfixture.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) MethodCounter(org.springframework.aop.testfixture.advice.MethodCounter) FileNotFoundException(java.io.FileNotFoundException) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) List(java.util.List) LockMixinAdvisor(test.mixin.LockMixinAdvisor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) HashMap(java.util.HashMap) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) SQLException(java.sql.SQLException) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) MarshalException(java.rmi.MarshalException) Nullable(org.springframework.lang.Nullable) Advisor(org.springframework.aop.Advisor) SerializationTestUtils(org.springframework.core.testfixture.io.SerializationTestUtils) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Iterator(java.util.Iterator) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) DynamicMethodMatcherPointcut(org.springframework.aop.support.DynamicMethodMatcherPointcut) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AfterEach(org.junit.jupiter.api.AfterEach) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Person(org.springframework.beans.testfixture.beans.Person) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 73 with ITestBean

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

the class AbstractAopProxyTests method testUserAttributes.

@Test
public void testUserAttributes() throws Throwable {
    class MapAwareMethodInterceptor implements MethodInterceptor {

        private final Map<String, String> expectedValues;

        private final Map<String, String> valuesToAdd;

        public MapAwareMethodInterceptor(Map<String, String> expectedValues, Map<String, String> valuesToAdd) {
            this.expectedValues = expectedValues;
            this.valuesToAdd = valuesToAdd;
        }

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            ReflectiveMethodInvocation rmi = (ReflectiveMethodInvocation) invocation;
            for (Iterator<String> it = rmi.getUserAttributes().keySet().iterator(); it.hasNext(); ) {
                Object key = it.next();
                assertThat(rmi.getUserAttributes().get(key)).isEqualTo(expectedValues.get(key));
            }
            rmi.getUserAttributes().putAll(valuesToAdd);
            return invocation.proceed();
        }
    }
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap<>(), new HashMap<String, String>());
    Map<String, String> firstValuesToAdd = new HashMap<>();
    firstValuesToAdd.put("test", "");
    MapAwareMethodInterceptor mami2 = new MapAwareMethodInterceptor(new HashMap<>(), firstValuesToAdd);
    MapAwareMethodInterceptor mami3 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
    MapAwareMethodInterceptor mami4 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
    Map<String, String> secondValuesToAdd = new HashMap<>();
    secondValuesToAdd.put("foo", "bar");
    secondValuesToAdd.put("cat", "dog");
    MapAwareMethodInterceptor mami5 = new MapAwareMethodInterceptor(firstValuesToAdd, secondValuesToAdd);
    Map<String, String> finalExpected = new HashMap<>(firstValuesToAdd);
    finalExpected.putAll(secondValuesToAdd);
    MapAwareMethodInterceptor mami6 = new MapAwareMethodInterceptor(finalExpected, secondValuesToAdd);
    pc.addAdvice(mami1);
    pc.addAdvice(mami2);
    pc.addAdvice(mami3);
    pc.addAdvice(mami4);
    pc.addAdvice(mami5);
    pc.addAdvice(mami6);
    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    String newName = "foo";
    tb.setName(newName);
    assertThat(tb.getName()).isEqualTo(newName);
}
Also used : HashMap(java.util.HashMap) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 74 with ITestBean

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

the class AbstractAopProxyTests method testCanCastProxyToProxyConfig.

@Test
public void testCanCastProxyToProxyConfig() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    ITestBean t = (ITestBean) createProxy(pc);
    assertThat(di.getCount()).isEqualTo(0);
    t.setAge(23);
    assertThat(t.getAge()).isEqualTo(23);
    assertThat(di.getCount()).isEqualTo(2);
    Advised advised = (Advised) t;
    assertThat(advised.getAdvisors().length).as("Have 1 advisor").isEqualTo(1);
    assertThat(advised.getAdvisors()[0].getAdvice()).isEqualTo(di);
    NopInterceptor di2 = new NopInterceptor();
    advised.addAdvice(1, di2);
    t.getName();
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(1);
    // will remove di
    advised.removeAdvisor(0);
    t.getAge();
    // Unchanged
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(2);
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    assertThat(cba.getCalls()).isEqualTo(0);
    advised.addAdvice(cba);
    t.setAge(16);
    assertThat(t.getAge()).isEqualTo(16);
    assertThat(cba.getCalls()).isEqualTo(2);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 75 with ITestBean

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

the class AbstractAopProxyTests method testTargetCanGetInvocation.

@Test
public void testTargetCanGetInvocation() throws Throwable {
    final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean();
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    TrapTargetInterceptor tii = new TrapTargetInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            // Assert that target matches BEFORE invocation returns
            assertThat(invocation.getThis()).as("Target is correct").isEqualTo(expectedTarget);
            return super.invoke(invocation);
        }
    };
    pc.addAdvice(tii);
    pc.setTarget(expectedTarget);
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    tb.getName();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Aggregations

ITestBean (org.springframework.beans.testfixture.beans.ITestBean)210 Test (org.junit.jupiter.api.Test)199 TestBean (org.springframework.beans.testfixture.beans.TestBean)121 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)44 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)31 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)23 Advisor (org.springframework.aop.Advisor)22 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)20 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)19 Method (java.lang.reflect.Method)16 TimeStamped (org.springframework.core.testfixture.TimeStamped)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 TimestampIntroductionInterceptor (org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor)15 IOException (java.io.IOException)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)14 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)13 Advised (org.springframework.aop.framework.Advised)13