Search in sources :

Example 26 with ITestBean

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

the class CountingAspectJAdvice method testIsProxy.

@Test
public void testIsProxy() throws Exception {
    ITestBean bean = getTestBean();
    assertThat(AopUtils.isAopProxy(bean)).as("Bean is not a proxy").isTrue();
    // check the advice details
    Advised advised = (Advised) bean;
    Advisor[] advisors = advised.getAdvisors();
    assertThat(advisors.length > 0).as("Advisors should not be empty").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 27 with ITestBean

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

the class CountingAspectJAdvice method testAspectAppliedForInitializeBeanWithEmptyName.

@Test
public void testAspectAppliedForInitializeBeanWithEmptyName() {
    ITestBean bean = (ITestBean) this.context.getAutowireCapableBeanFactory().initializeBean(new TestBean(), "");
    CountingAspectJAdvice advice = (CountingAspectJAdvice) this.context.getBean("countingAdvice");
    assertThat(advice.getBeforeCount()).as("Incorrect before count").isEqualTo(0);
    assertThat(advice.getAfterCount()).as("Incorrect after count").isEqualTo(0);
    bean.setName("Sally");
    assertThat(advice.getBeforeCount()).as("Incorrect before count").isEqualTo(1);
    assertThat(advice.getAfterCount()).as("Incorrect after count").isEqualTo(1);
    bean.getName();
    assertThat(advice.getBeforeCount()).as("Incorrect before count").isEqualTo(1);
    assertThat(advice.getAfterCount()).as("Incorrect after count").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) Test(org.junit.jupiter.api.Test)

Example 28 with ITestBean

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

the class AbstractAopProxyTests method testDynamicMethodPointcutThatAlwaysAppliesStatically.

@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory();
    pc.addInterface(ITestBean.class);
    TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
    pc.addAdvisor(dp);
    pc.setTarget(tb);
    ITestBean it = (ITestBean) createProxy(pc);
    assertThat(dp.count).isEqualTo(0);
    it.getAge();
    assertThat(dp.count).isEqualTo(1);
    it.setAge(11);
    assertThat(it.getAge()).isEqualTo(11);
    assertThat(dp.count).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) Test(org.junit.jupiter.api.Test)

Example 29 with ITestBean

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

the class AbstractAopProxyTests method testCloneInvocationToProceedThreeTimes.

/**
 * There are times when we want to call proceed() twice.
 * We can do this if we clone the invocation.
 */
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    pc.addInterface(ITestBean.class);
    MethodInterceptor twoBirthdayInterceptor = mi -> {
        // Clone the invocation to proceed three times
        // "The Moor's Last Sigh": this technology can cause premature aging
        MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
        MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
        clone1.proceed();
        clone2.proceed();
        return mi.proceed();
    };
    @SuppressWarnings("serial") StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return "haveBirthday".equals(m.getName());
        }
    };
    pc.addAdvisor(advisor);
    ITestBean it = (ITestBean) createProxy(pc);
    final int age = 20;
    it.setAge(age);
    assertThat(it.getAge()).isEqualTo(age);
    // Should return the age before the third, AOP-induced birthday
    assertThat(it.haveBirthday()).isEqualTo((age + 2));
    // Return the final age produced by 3 birthdays
    assertThat(it.getAge()).isEqualTo((age + 3));
}
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) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Nullable(org.springframework.lang.Nullable) Test(org.junit.jupiter.api.Test)

Example 30 with ITestBean

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

the class AbstractAopProxyTests method testCannotAddAdvisorWhenFrozenUsingCast.

/**
 * Check that casting to Advised can't get around advice freeze.
 */
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertThat(pc.isFrozen()).isFalse();
    pc.addAdvice(new NopInterceptor());
    ITestBean proxied = (ITestBean) createProxy(pc);
    pc.setFrozen(true);
    Advised advised = (Advised) proxied;
    assertThat(pc.isFrozen()).isTrue();
    assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add Advisor when frozen").isThrownBy(() -> advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()))).withMessageContaining("frozen");
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(advised.getAdvisors().length).isEqualTo(1);
}
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) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) 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