Search in sources :

Example 6 with DefaultIntroductionAdvisor

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

the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.

/**
	 * Should see effect immediately on behavior.
	 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
    ProxyFactory config = new ProxyFactory(new TestBean());
    assertFalse("Shouldn't implement TimeStamped before manipulation", config.getProxy() instanceof TimeStamped);
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to front of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertTrue(config.getAdvisors().length == oldCount + 1);
    TimeStamped ts = (TimeStamped) config.getProxy();
    assertTrue(ts.getTimeStamp() == time);
    // Can remove
    config.removeAdvice(ti);
    assertTrue(config.getAdvisors().length == oldCount);
    try {
        // Existing reference will fail
        ts.getTimeStamp();
        fail("Existing object won't implement this interface any more");
    } catch (RuntimeException ex) {
    }
    assertFalse("Should no longer implement TimeStamped", config.getProxy() instanceof TimeStamped);
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertTrue(config.getAdvisors().length == oldCount);
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    assertEquals(1, debugInterceptor.getCount());
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // not invoked again
    assertTrue(debugInterceptor.getCount() == 1);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.Test)

Example 7 with DefaultIntroductionAdvisor

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

the class AbstractAopProxyTests method testIntroductionThrowsUncheckedException.

/**
	 * Note that an introduction can't throw an unexpected checked exception,
	 * as it's constained by the interface.
	 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    @SuppressWarnings("serial")
    class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {

        /**
			 * @see test.util.TimeStamped#getTimeStamp()
			 */
        @Override
        public long getTimeStamp() {
            throw new UnsupportedOperationException();
        }
    }
    pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));
    TimeStamped ts = (TimeStamped) createProxy(pc);
    try {
        ts.getTimeStamp();
        fail("Should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }
}
Also used : DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.Test)

Example 8 with DefaultIntroductionAdvisor

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

the class AbstractAopProxyTests method testUseAsHashKey.

@Test
public void testUseAsHashKey() {
    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    pf1.addAdvice(new NopInterceptor());
    ITestBean proxy1 = (ITestBean) createProxy(pf1);
    TestBean target2 = new TestBean();
    ProxyFactory pf2 = new ProxyFactory(target2);
    pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
    ITestBean proxy2 = (ITestBean) createProxy(pf2);
    HashMap<ITestBean, Object> h = new HashMap<>();
    Object value1 = "foo";
    Object value2 = "bar";
    assertNull(h.get(proxy1));
    h.put(proxy1, value1);
    h.put(proxy2, value2);
    assertEquals(h.get(proxy1), value1);
    assertEquals(h.get(proxy2), value2);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) HashMap(java.util.HashMap) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)8 ITestBean (org.springframework.tests.sample.beans.ITestBean)8 TestBean (org.springframework.tests.sample.beans.TestBean)7 TimeStamped (org.springframework.tests.TimeStamped)4 TimestampIntroductionInterceptor (org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor)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 DelegatingIntroductionInterceptor (org.springframework.aop.support.DelegatingIntroductionInterceptor)1 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)1 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)1 IOther (org.springframework.tests.sample.beans.IOther)1 LockedException (test.mixin.LockedException)1