Search in sources :

Example 1 with DefaultIntroductionAdvisor

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

the class ProxyFactoryTests method testGetsAllInterfaces.

@Test
public void testGetsAllInterfaces() {
    // Extend to get new interface
    class TestBeanSubclass extends TestBean implements Comparable<Object> {

        @Override
        public int compareTo(Object arg0) {
            throw new UnsupportedOperationException("compareTo");
        }
    }
    TestBeanSubclass raw = new TestBeanSubclass();
    ProxyFactory factory = new ProxyFactory(raw);
    // System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
    assertThat(factory.getProxiedInterfaces().length).as("Found correct number of interfaces").isEqualTo(5);
    ITestBean tb = (ITestBean) factory.getProxy();
    assertThat(tb).as("Picked up secondary interface").isInstanceOf(IOther.class);
    raw.setAge(25);
    assertThat(tb.getAge() == raw.getAge()).isTrue();
    long t = 555555L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
    Class<?>[] oldProxiedInterfaces = factory.getProxiedInterfaces();
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
    assertThat(newProxiedInterfaces.length).as("Advisor proxies one more interface after introduction").isEqualTo(oldProxiedInterfaces.length + 1);
    TimeStamped ts = (TimeStamped) factory.getProxy();
    assertThat(ts.getTimeStamp() == t).isTrue();
    // Shouldn't fail;
    ((IOther) ts).absquatulate();
}
Also used : TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) IOther(org.springframework.beans.testfixture.beans.IOther) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimeStamped(org.springframework.core.testfixture.TimeStamped) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultIntroductionAdvisor

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

the class ProxyFactoryBeanTests method testCanAddAndRemoveAspectInterfacesOnPrototype.

/**
 * Try adding and removing interfaces and interceptors on prototype.
 * Changes will only affect future references obtained from the factory.
 * Each instance will be independent.
 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnPrototype() {
    assertThat(factory.getBean("test2")).as("Shouldn't implement TimeStamped before manipulation").isNotInstanceOf(TimeStamped.class);
    ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2");
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to head of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
    TimeStamped ts = (TimeStamped) factory.getBean("test2");
    assertThat(ts.getTimeStamp()).isEqualTo(time);
    // Can remove
    config.removeAdvice(ti);
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    // Check no change on existing object reference
    assertThat(ts.getTimeStamp() == time).isTrue();
    assertThat(factory.getBean("test2")).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    // Won't affect existing reference
    assertThat(debugInterceptor.getCount() == 0).isTrue();
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(1);
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // Still invoked with old reference
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // not invoked with new object
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // Our own timestamped reference should still work
    assertThat(ts.getTimeStamp()).isEqualTo(time);
}
Also used : TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultIntroductionAdvisor

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

the class AbstractAopProxyTests method testCannotAddIntroductionAdviceWithUnimplementedInterface.

/**
 * Check that the introduction advice isn't allowed to introduce interfaces
 * that are unsupported by the IntroductionInterceptor.
 */
@Test
public void testCannotAddIntroductionAdviceWithUnimplementedInterface() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertThatIllegalArgumentException().isThrownBy(() -> pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), ITestBean.class)));
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultIntroductionAdvisor

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

the class AbstractAopProxyTests method testCannotAddIntroductionAdviceToIntroduceClass.

/**
 * Should only be able to introduce interfaces, not classes.
 */
@Test
public void testCannotAddIntroductionAdviceToIntroduceClass() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertThatIllegalArgumentException().as("Shouldn't be able to add introduction advice that introduces a class, rather than an interface").isThrownBy(() -> pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), TestBean.class))).withMessageContaining("interface");
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultIntroductionAdvisor

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

the class AbstractAopProxyTests method testRejectsBogusDynamicIntroductionAdviceWithNoAdapter.

@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
        // TODO May fail on either call: may want to tighten up definition
        ITestBean proxied = (ITestBean) createProxy(pc);
        proxied.getName();
    });
// TODO used to catch UnknownAdviceTypeException, but
// with CGLIB some errors are in proxy creation and are wrapped
// in aspect exception. Error message is still fine.
// assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)8 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)8 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)8 TestBean (org.springframework.beans.testfixture.beans.TestBean)7 TimestampIntroductionInterceptor (org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor)6 TimeStamped (org.springframework.core.testfixture.TimeStamped)4 DebugInterceptor (org.springframework.aop.interceptor.DebugInterceptor)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MarshalException (java.rmi.MarshalException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)1 DelegatingIntroductionInterceptor (org.springframework.aop.support.DelegatingIntroductionInterceptor)1 Pointcuts (org.springframework.aop.support.Pointcuts)1 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)1 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)1 IOther (org.springframework.beans.testfixture.beans.IOther)1