Search in sources :

Example 16 with NopInterceptor

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

the class AbstractAopProxyTests method testAdviceSupportListeners.

@Test
public void testAdviceSupportListeners() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    CountingAdvisorListener l = new CountingAdvisorListener(pc);
    pc.addListener(l);
    RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
    // Should be automatically added as a listener
    pc.addListener(acf);
    assertThat(pc.isActive()).isFalse();
    assertThat(l.activates).isEqualTo(0);
    assertThat(acf.refreshes).isEqualTo(0);
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(acf.refreshes).isEqualTo(1);
    assertThat(l.activates).isEqualTo(1);
    assertThat(pc.isActive()).isTrue();
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(l.adviceChanges).isEqualTo(0);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    assertThat(l.adviceChanges).isEqualTo(1);
    assertThat(acf.refreshes).isEqualTo(2);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    pc.removeAdvice(di);
    assertThat(l.adviceChanges).isEqualTo(2);
    assertThat(acf.refreshes).isEqualTo(3);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    pc.getProxy();
    assertThat(l.activates).isEqualTo(1);
    pc.removeListener(l);
    assertThat(l.adviceChanges).isEqualTo(2);
    pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
    // No longer counting
    assertThat(l.adviceChanges).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) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 17 with NopInterceptor

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

the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.

@Test
public void testCannotAddInterceptorWhenFrozen() 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);
    assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add interceptor when frozen").isThrownBy(() -> pc.addAdvice(0, new NopInterceptor())).withMessageContaining("frozen");
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(((Advised) proxied).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) Test(org.junit.jupiter.api.Test)

Example 18 with NopInterceptor

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

the class AbstractAopProxyTests method testSerializableTargetAndAdvice.

@Test
public void testSerializableTargetAndAdvice() throws Throwable {
    SerializablePerson personTarget = new SerializablePerson();
    personTarget.setName("jim");
    personTarget.setAge(26);
    assertThat(SerializationTestUtils.isSerializable(personTarget)).isTrue();
    ProxyFactory pf = new ProxyFactory(personTarget);
    CountingThrowsAdvice cta = new CountingThrowsAdvice();
    pf.addAdvice(new SerializableNopInterceptor());
    // Try various advice types
    pf.addAdvice(new CountingBeforeAdvice());
    pf.addAdvice(new CountingAfterReturningAdvice());
    pf.addAdvice(cta);
    Person p = (Person) createAopProxy(pf).getProxy();
    p.echo(null);
    assertThat(cta.getCalls()).isEqualTo(0);
    try {
        p.echo(new IOException());
    } catch (IOException ex) {
    /* expected */
    }
    assertThat(cta.getCalls()).isEqualTo(1);
    // Will throw exception if it fails
    Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
    assertThat(p2).isNotSameAs(p);
    assertThat(p2.getName()).isEqualTo(p.getName());
    assertThat(p2.getAge()).isEqualTo(p.getAge());
    assertThat(AopUtils.isAopProxy(p2)).as("Deserialized object is an AOP proxy").isTrue();
    Advised a1 = (Advised) p;
    Advised a2 = (Advised) p2;
    // Check we can manipulate state of p2
    assertThat(a2.getAdvisors().length).isEqualTo(a1.getAdvisors().length);
    // This should work as SerializablePerson is equal
    assertThat(p2).as("Proxies should be equal, even after one was serialized").isEqualTo(p);
    assertThat(p).as("Proxies should be equal, even after one was serialized").isEqualTo(p2);
    // Check we can add a new advisor to the target
    NopInterceptor ni = new NopInterceptor();
    p2.getAge();
    assertThat(ni.getCount()).isEqualTo(0);
    a2.addAdvice(ni);
    p2.getAge();
    assertThat(ni.getCount()).isEqualTo(1);
    cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
    p2.echo(null);
    assertThat(cta.getCalls()).isEqualTo(1);
    try {
        p2.echo(new IOException());
    } catch (IOException ex) {
    }
    assertThat(cta.getCalls()).isEqualTo(2);
}
Also used : CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) IOException(java.io.IOException) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) Person(org.springframework.beans.testfixture.beans.Person) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 19 with NopInterceptor

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

the class CreatesTestBean method jdkAssertions.

private void jdkAssertions(ITestBean tb, int nopInterceptorCount) {
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor");
    assertThat(nop.getCount()).isEqualTo(0);
    assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
    int age = 5;
    tb.setAge(age);
    assertThat(tb.getAge()).isEqualTo(age);
    assertThat(nop.getCount()).isEqualTo((2 * nopInterceptorCount));
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor)

Example 20 with NopInterceptor

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

the class CreatesTestBean method jdkIntroduction.

@Test
void jdkIntroduction() {
    ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertThat(nop.getCount()).isEqualTo(0);
    assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
    int age = 5;
    tb.setAge(age);
    assertThat(tb.getAge()).isEqualTo(age);
    boolean condition = tb instanceof TimeStamped;
    assertThat(condition).as("Introduction was made").isTrue();
    assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(3);
    assertThat(tb.getName()).isEqualTo("introductionUsingJdk");
    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)

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