Search in sources :

Example 26 with NopInterceptor

use of org.springframework.tests.aop.interceptor.NopInterceptor 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);
    assertFalse(pc.isFrozen());
    pc.addAdvice(new NopInterceptor());
    ITestBean proxied = (ITestBean) createProxy(pc);
    pc.setFrozen(true);
    Advised advised = (Advised) proxied;
    assertTrue(pc.isFrozen());
    try {
        advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
        fail("Shouldn't be able to add Advisor when frozen");
    } catch (AopConfigException ex) {
        assertTrue(ex.getMessage().contains("frozen"));
    }
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, advised.getAdvisors().length);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.Test)

Example 27 with NopInterceptor

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

the class AbstractAopProxyTests method testBeforeAdviceThrowsException.

@Test
public void testBeforeAdviceThrowsException() {
    final RuntimeException rex = new RuntimeException();
    @SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {

        @Override
        public void before(Method m, Object[] args, Object target) throws Throwable {
            super.before(m, args, target);
            if (m.getName().startsWith("set"))
                throw rex;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    NopInterceptor nop1 = new NopInterceptor();
    NopInterceptor nop2 = new NopInterceptor();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(nop1);
    pf.addAdvice(ba);
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) createProxy(pf);
    // Won't throw an exception
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, ba.getCalls());
    assertEquals(1, ba.getCalls("getAge"));
    assertEquals(1, nop1.getCount());
    assertEquals(1, nop2.getCount());
    // Will fail, after invoking Nop1
    try {
        proxied.setAge(26);
        fail("before advice should have ended chain");
    } catch (RuntimeException ex) {
        assertEquals(rex, ex);
    }
    assertEquals(2, ba.getCalls());
    assertEquals(2, nop1.getCount());
    // Nop2 didn't get invoked when the exception was thrown
    assertEquals(1, nop2.getCount());
    // Shouldn't have changed value in joinpoint
    assertEquals(target.getAge(), proxied.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 28 with NopInterceptor

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

the class AbstractAopProxyTests method testDynamicMethodPointcutThatAlwaysAppliesStatically.

@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory();
    pc.addInterface(ITestBean.class);
    TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
    pc.addAdvisor(dp);
    pc.setTarget(tb);
    ITestBean it = (ITestBean) createProxy(pc);
    assertEquals(dp.count, 0);
    it.getAge();
    assertEquals(dp.count, 1);
    it.setAge(11);
    assertEquals(it.getAge(), 11);
    assertEquals(dp.count, 2);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 29 with NopInterceptor

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

the class MBeanExporterTests method testExportJdkProxy.

@Test
public void testExportJdkProxy() throws Exception {
    JmxTestBean bean = new JmxTestBean();
    bean.setName("Rob Harrop");
    ProxyFactory factory = new ProxyFactory();
    factory.setTarget(bean);
    factory.addAdvice(new NopInterceptor());
    factory.setInterfaces(IJmxTestBean.class);
    IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
    String name = "bean:mmm=whatever";
    Map<String, Object> beans = new HashMap<>();
    beans.put(name, proxy);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.registerBeans();
    ObjectName oname = ObjectName.getInstance(name);
    Object nameValue = server.getAttribute(oname, "Name");
    assertEquals("Rob Harrop", nameValue);
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) HashMap(java.util.HashMap) IJmxTestBean(org.springframework.jmx.IJmxTestBean) JmxTestBean(org.springframework.jmx.JmxTestBean) IJmxTestBean(org.springframework.jmx.IJmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 30 with NopInterceptor

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

the class AbstractAopProxyTests method testExistingProxyChangesTarget.

@Test
public void testExistingProxyChangesTarget() throws Throwable {
    TestBean tb1 = new TestBean();
    tb1.setAge(33);
    TestBean tb2 = new TestBean();
    tb2.setAge(26);
    tb2.setName("Juergen");
    TestBean tb3 = new TestBean();
    tb3.setAge(37);
    ProxyFactory pc = new ProxyFactory(tb1);
    NopInterceptor nop = new NopInterceptor();
    pc.addAdvice(nop);
    ITestBean proxy = (ITestBean) createProxy(pc);
    assertEquals(nop.getCount(), 0);
    assertEquals(tb1.getAge(), proxy.getAge());
    assertEquals(nop.getCount(), 1);
    // Change to a new static target
    pc.setTarget(tb2);
    assertEquals(tb2.getAge(), proxy.getAge());
    assertEquals(nop.getCount(), 2);
    // Change to a new dynamic target
    HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
    pc.setTargetSource(hts);
    assertEquals(tb3.getAge(), proxy.getAge());
    assertEquals(nop.getCount(), 3);
    hts.swap(tb1);
    assertEquals(tb1.getAge(), proxy.getAge());
    tb1.setName("Colin");
    assertEquals(tb1.getName(), proxy.getName());
    assertEquals(nop.getCount(), 5);
    // Change back, relying on casting to Advised
    Advised advised = (Advised) proxy;
    assertSame(hts, advised.getTargetSource());
    SingletonTargetSource sts = new SingletonTargetSource(tb2);
    advised.setTargetSource(sts);
    assertEquals(tb2.getName(), proxy.getName());
    assertSame(sts, advised.getTargetSource());
    assertEquals(tb2.getAge(), proxy.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) Test(org.junit.Test)

Aggregations

NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)61 Test (org.junit.Test)57 ITestBean (org.springframework.tests.sample.beans.ITestBean)40 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)34 TestBean (org.springframework.tests.sample.beans.TestBean)34 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.tests.aop.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 LockedException (test.mixin.LockedException)5 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 CountingAfterReturningAdvice (org.springframework.tests.aop.advice.CountingAfterReturningAdvice)4 FileNotFoundException (java.io.FileNotFoundException)3