Search in sources :

Example 21 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class CreatesTestBean method jdkIntroductionAppliesToCreatedObjectsNotFactoryBean.

@Test
void jdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
    ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertThat(nop.getCount()).as("NOP should not have done any work yet").isEqualTo(0);
    assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
    int age = 5;
    tb.setAge(age);
    assertThat(tb.getAge()).isEqualTo(age);
    assertThat(tb).as("Introduction was made").isInstanceOf(TimeStamped.class);
    assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(3);
    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertThat(lockable1.locked()).isFalse();
    assertThat(lockable2.locked()).isFalse();
    tb.setAge(65);
    assertThat(tb.getAge()).isEqualTo(65);
    lockable1.lock();
    assertThat(lockable1.locked()).isTrue();
    // Shouldn't affect second
    assertThat(lockable2.locked()).isFalse();
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    assertThatExceptionOfType(LockedException.class).as("mixin should have locked this object").isThrownBy(() -> tb.setAge(6));
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimeStamped(org.springframework.core.testfixture.TimeStamped) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Lockable(test.mixin.Lockable) Test(org.junit.jupiter.api.Test)

Example 22 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testSingletonInstancesAreEqual.

/**
 * The instances are equal, but do not have object identity.
 * Interceptors and interfaces and the target are the same.
 */
@Test
public void testSingletonInstancesAreEqual() {
    ITestBean test1 = (ITestBean) factory.getBean("test1");
    ITestBean test1_1 = (ITestBean) factory.getBean("test1");
    // assertTrue("Singleton instances ==", test1 == test1_1);
    assertThat(test1_1).as("Singleton instances ==").isEqualTo(test1);
    test1.setAge(25);
    assertThat(test1_1.getAge()).isEqualTo(test1.getAge());
    test1.setAge(250);
    assertThat(test1_1.getAge()).isEqualTo(test1.getAge());
    Advised pc1 = (Advised) test1;
    Advised pc2 = (Advised) test1_1;
    assertThat(pc2.getAdvisors()).isEqualTo(pc1.getAdvisors());
    int oldLength = pc1.getAdvisors().length;
    NopInterceptor di = new NopInterceptor();
    pc1.addAdvice(1, di);
    assertThat(pc2.getAdvisors()).isEqualTo(pc1.getAdvisors());
    assertThat(pc2.getAdvisors().length).as("Now have one more advisor").isEqualTo((oldLength + 1));
    assertThat(0).isEqualTo(di.getCount());
    test1.setAge(5);
    assertThat(test1.getAge()).isEqualTo(test1_1.getAge());
    assertThat(3).isEqualTo(di.getCount());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Test(org.junit.jupiter.api.Test)

Example 23 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testCanAddAndRemoveAdvicesOnSingleton.

/**
 * Note that we can't add or remove interfaces without reconfiguring the
 * singleton.
 */
@Test
public void testCanAddAndRemoveAdvicesOnSingleton() {
    ITestBean it = (ITestBean) factory.getBean("test1");
    Advised pc = (Advised) it;
    it.getAge();
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    assertThat(di.getCount()).isEqualTo(0);
    it.setAge(25);
    assertThat(it.getAge()).isEqualTo(25);
    assertThat(di.getCount()).isEqualTo(2);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Test(org.junit.jupiter.api.Test)

Example 24 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class UnsupportedInterceptor method testProxyAProxyWithAdditionalInterface.

@Test
public void testProxyAProxyWithAdditionalInterface() {
    ITestBean target = new TestBean();
    mockTargetSource.setTarget(target);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    as.addInterface(Serializable.class);
    CglibAopProxy cglib = new CglibAopProxy(as);
    ITestBean proxy1 = (ITestBean) cglib.getProxy();
    mockTargetSource.setTarget(proxy1);
    as = new AdvisedSupport(new Class<?>[] {});
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    cglib = new CglibAopProxy(as);
    ITestBean proxy2 = (ITestBean) cglib.getProxy();
    assertThat(proxy2 instanceof Serializable).isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Serializable(java.io.Serializable) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 25 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class UnsupportedInterceptor method testPackageMethodInvocation.

@Test
public void testPackageMethodInvocation() {
    PackageMethodTestBean bean = new PackageMethodTestBean();
    bean.value = "foo";
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    PackageMethodTestBean proxy = (PackageMethodTestBean) aop.getProxy();
    assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
    assertThat(bean.getClass().getClassLoader()).isEqualTo(proxy.getClass().getClassLoader());
    assertThat(proxy.getString()).isEqualTo("foo");
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)60 Test (org.junit.jupiter.api.Test)57 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)39 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)33 TestBean (org.springframework.beans.testfixture.beans.TestBean)32 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)13 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)11 Advisor (org.springframework.aop.Advisor)10 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 Method (java.lang.reflect.Method)6 LockMixinAdvisor (test.mixin.LockMixinAdvisor)6 IOException (java.io.IOException)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 CountingAfterReturningAdvice (org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 Person (org.springframework.beans.testfixture.beans.Person)4 Nullable (org.springframework.lang.Nullable)4