Search in sources :

Example 96 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 97 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 98 with ITestBean

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

the class JdkDynamicProxyTests method testInterceptorIsInvokedWithNoTarget.

@Test
public void testInterceptorIsInvokedWithNoTarget() {
    // Test return value
    final int age = 25;
    MethodInterceptor mi = (invocation -> age);
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(mi);
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    assertThat(tb.getAge()).as("correct return value").isEqualTo(age);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.jupiter.api.Test)

Example 99 with ITestBean

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

the class JdkDynamicProxyTests method testTargetCanGetInvocationWithPrivateClass.

@Test
public void testTargetCanGetInvocationWithPrivateClass() {
    final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {

        @Override
        protected void assertions(MethodInvocation invocation) {
            assertThat(invocation.getThis()).isEqualTo(this);
            assertThat(invocation.getMethod().getDeclaringClass()).as("Invocation should be on ITestBean: " + invocation.getMethod()).isEqualTo(ITestBean.class);
        }
    };
    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)

Example 100 with ITestBean

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

the class ProxyFactoryBeanTests method testPrototypeInstancesAreNotEqual.

@Test
public void testPrototypeInstancesAreNotEqual() {
    assertThat(ITestBean.class.isAssignableFrom(factory.getType("prototype"))).as("Has correct object type").isTrue();
    ITestBean test2 = (ITestBean) factory.getBean("prototype");
    ITestBean test2_1 = (ITestBean) factory.getBean("prototype");
    assertThat(test2 != test2_1).as("Prototype instances !=").isTrue();
    assertThat(test2.equals(test2_1)).as("Prototype instances equal").isTrue();
    assertThat(ITestBean.class.isAssignableFrom(factory.getType("prototype"))).as("Has correct object type").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) 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