Search in sources :

Example 16 with NopInterceptor

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

the class UnsupportedInterceptor method testPackageMethodInvocation.

@Test
public void testPackageMethodInvocation() {
    PackageMethodTestBean bean = new PackageMethodTestBean();
    bean.value = "foo";
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    PackageMethodTestBean proxy = (PackageMethodTestBean) aop.getProxy();
    assertTrue(AopUtils.isCglibProxy(proxy));
    assertEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader());
    assertEquals("foo", proxy.getString());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Test(org.junit.Test)

Example 17 with NopInterceptor

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

the class UnsupportedInterceptor method testProxyAProxyWithAdditionalInterface.

@Test
public void testProxyAProxyWithAdditionalInterface() {
    ITestBean target = new TestBean();
    mockTargetSource.setTarget(target);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    as.addInterface(Serializable.class);
    CglibAopProxy cglib = new CglibAopProxy(as);
    ITestBean proxy1 = (ITestBean) cglib.getProxy();
    mockTargetSource.setTarget(proxy1);
    as = new AdvisedSupport(new Class<?>[] {});
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    cglib = new CglibAopProxy(as);
    ITestBean proxy2 = (ITestBean) cglib.getProxy();
    assertTrue(proxy2 instanceof Serializable);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Serializable(java.io.Serializable) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 18 with NopInterceptor

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

the class UnsupportedInterceptor method testExceptionHandling.

@Test
public void testExceptionHandling() {
    ExceptionThrower bean = new ExceptionThrower();
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    ExceptionThrower proxy = (ExceptionThrower) aop.getProxy();
    try {
        proxy.doTest();
    } catch (Exception ex) {
        assertTrue("Invalid exception class", ex instanceof ApplicationContextException);
    }
    assertTrue("Catch was not invoked", proxy.isCatchInvoked());
    assertTrue("Finally was not invoked", proxy.isFinallyInvoked());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ApplicationContextException(org.springframework.context.ApplicationContextException) ApplicationContextException(org.springframework.context.ApplicationContextException) Test(org.junit.Test)

Example 19 with NopInterceptor

use of org.springframework.tests.aop.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");
    assertEquals(0, nop.getCount());
    assertTrue(AopUtils.isJdkDynamicProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertEquals(2 * nopInterceptorCount, nop.getCount());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor)

Example 20 with NopInterceptor

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

the class CreatesTestBean method testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean.

@Test
public void testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
    ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals("NOP should not have done any work yet", 0, nop.getCount());
    assertTrue(AopUtils.isJdkDynamicProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertTrue("Introduction was made", tb instanceof TimeStamped);
    assertEquals(0, ((TimeStamped) tb).getTimeStamp());
    assertEquals(3, nop.getCount());
    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    tb.setAge(65);
    assertEquals(65, tb.getAge());
    lockable1.lock();
    assertTrue(lockable1.locked());
    // Shouldn't affect second
    assertFalse(lockable2.locked());
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    try {
        tb.setAge(6);
        fail("Mixin should have locked this object");
    } catch (LockedException ex) {
    // Ok
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TimeStamped(org.springframework.tests.TimeStamped) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) LockedException(test.mixin.LockedException) Lockable(test.mixin.Lockable) 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