Search in sources :

Example 11 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class TrickyAspectJPointcutExpressionTests method testManualProxyJavaWithUnconditionalPointcut.

@Test
public void testManualProxyJavaWithUnconditionalPointcut() throws Exception {
    TestService target = new TestServiceImpl();
    LogUserAdvice logAdvice = new LogUserAdvice();
    testAdvice(new DefaultPointcutAdvisor(logAdvice), logAdvice, target, "TestServiceImpl");
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 12 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class AspectJPrecedenceComparatorTests method createSpringAOPAfterAdvice.

private Advisor createSpringAOPAfterAdvice(int order) {
    AfterReturningAdvice advice = (returnValue, method, args, target) -> {
    };
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(this.anyOldPointcut, advice);
    advisor.setOrder(order);
    return advisor;
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test) AspectJAfterAdvice(org.springframework.aop.aspectj.AspectJAfterAdvice) AspectJAfterReturningAdvice(org.springframework.aop.aspectj.AspectJAfterReturningAdvice) AspectJAroundAdvice(org.springframework.aop.aspectj.AspectJAroundAdvice) AspectJAfterThrowingAdvice(org.springframework.aop.aspectj.AspectJAfterThrowingAdvice) AspectJMethodBeforeAdvice(org.springframework.aop.aspectj.AspectJMethodBeforeAdvice) AspectJPointcutAdvisor(org.springframework.aop.aspectj.AspectJPointcutAdvisor) BeforeAdvice(org.springframework.aop.BeforeAdvice) AbstractAspectJAdvice(org.springframework.aop.aspectj.AbstractAspectJAdvice) Method(java.lang.reflect.Method) AspectJExpressionPointcut(org.springframework.aop.aspectj.AspectJExpressionPointcut) Advisor(org.springframework.aop.Advisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) AspectJAfterReturningAdvice(org.springframework.aop.aspectj.AspectJAfterReturningAdvice) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor)

Example 13 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class ProxyFactoryTests method testIndexOfMethods.

@Test
public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
    assertThat(pf.indexOf(nop)).isEqualTo(0);
    assertThat(pf.indexOf(advisor)).isEqualTo(1);
    assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 14 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.

the class HotSwappableTargetSourceTests method testSerialization.

@Test
public void testSerialization() throws Exception {
    SerializablePerson sp1 = new SerializablePerson();
    sp1.setName("Tony");
    SerializablePerson sp2 = new SerializablePerson();
    sp1.setName("Gordon");
    HotSwappableTargetSource hts = new HotSwappableTargetSource(sp1);
    ProxyFactory pf = new ProxyFactory();
    pf.addInterface(Person.class);
    pf.setTargetSource(hts);
    pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor()));
    Person p = (Person) pf.getProxy();
    assertThat(p.getName()).isEqualTo(sp1.getName());
    hts.swap(sp2);
    assertThat(p.getName()).isEqualTo(sp2.getName());
    p = SerializationTestUtils.serializeAndDeserialize(p);
    // We need to get a reference to the client-side targetsource
    hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource();
    assertThat(p.getName()).isEqualTo(sp2.getName());
    hts.swap(sp1);
    assertThat(p.getName()).isEqualTo(sp1.getName());
}
Also used : SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Person(org.springframework.beans.testfixture.beans.Person) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) Test(org.junit.jupiter.api.Test)

Example 15 with DefaultPointcutAdvisor

use of org.springframework.aop.support.DefaultPointcutAdvisor 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

DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)45 Test (org.junit.jupiter.api.Test)29 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)16 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)16 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 TestBean (org.springframework.beans.testfixture.beans.TestBean)11 Advisor (org.springframework.aop.Advisor)8 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)7 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 AspectJExpressionPointcut (org.springframework.aop.aspectj.AspectJExpressionPointcut)6 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)5 Pointcut (org.springframework.aop.Pointcut)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)4 Test (org.junit.Test)3 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)3 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)3