Search in sources :

Example 46 with NopInterceptor

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

the class UnsupportedInterceptor method testPackageMethodInvocationWithDifferentClassLoader.

@Test
public void testPackageMethodInvocationWithDifferentClassLoader() {
    ClassLoader child = new ClassLoader(getClass().getClassLoader()) {
    };
    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(child);
    assertTrue(AopUtils.isCglibProxy(proxy));
    assertNotEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader());
    // we're stuck in the proxy instance
    assertNull(proxy.getString());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Test(org.junit.Test)

Example 47 with NopInterceptor

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

the class UnsupportedInterceptor method testProtectedMethodInvocation.

@Test
public void testProtectedMethodInvocation() {
    ProtectedMethodTestBean bean = new ProtectedMethodTestBean();
    bean.value = "foo";
    mockTargetSource.setTarget(bean);
    AdvisedSupport as = new AdvisedSupport();
    as.setTargetSource(mockTargetSource);
    as.addAdvice(new NopInterceptor());
    AopProxy aop = new CglibAopProxy(as);
    ProtectedMethodTestBean proxy = (ProtectedMethodTestBean) 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 48 with NopInterceptor

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

the class UnsupportedInterceptor method getAdvisedProxy.

private ITestBean getAdvisedProxy(TestBean target) {
    ProxyFactory pf = new ProxyFactory(new Class<?>[] { ITestBean.class });
    pf.setProxyTargetClass(true);
    MethodInterceptor advice = new NopInterceptor();
    Pointcut pointcut = new Pointcut() {

        @Override
        public ClassFilter getClassFilter() {
            return ClassFilter.TRUE;
        }

        @Override
        public MethodMatcher getMethodMatcher() {
            return MethodMatcher.TRUE;
        }

        @Override
        public boolean equals(Object obj) {
            return true;
        }

        @Override
        public int hashCode() {
            return 0;
        }
    };
    pf.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice));
    pf.setTarget(target);
    pf.setFrozen(true);
    pf.setExposeProxy(false);
    return (ITestBean) pf.getProxy();
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor)

Example 49 with NopInterceptor

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

the class ProxyFactoryBeanTests method testSerializableSingletonProxy.

@Test
public void testSerializableSingletonProxy() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("serializableSingleton");
    assertSame("Should be a Singleton", p, bf.getBean("serializableSingleton"));
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(p, p2);
    assertNotSame(p, p2);
    assertEquals("serializableSingleton", p2.getName());
    // Add unserializable advice
    Advice nop = new NopInterceptor();
    ((Advised) p).addAdvice(nop);
    // Check it still works
    assertEquals(p2.getName(), p2.getName());
    assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p));
    // Remove offending interceptor...
    assertTrue(((Advised) p).removeAdvice(nop));
    assertTrue("Serializable again because offending interceptor was removed", SerializationTestUtils.isSerializable(p));
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Advice(org.aopalliance.aop.Advice) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Person(org.springframework.tests.sample.beans.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 50 with NopInterceptor

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

the class SelectivePrototypeTargetSourceCreator method testCommonInterceptorAndAdvisor.

/**
	 * Check that we can provide a common interceptor that will
	 * appear in the chain before "specific" interceptors,
	 * which are sourced from matching advisors
	 */
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
    BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
    ITestBean test1 = (ITestBean) bf.getBean("test1");
    assertTrue(AopUtils.isAopProxy(test1));
    Lockable lockable1 = (Lockable) test1;
    NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
    NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();
    ITestBean test2 = (ITestBean) bf.getBean("test2");
    Lockable lockable2 = (Lockable) test2;
    // Locking should be independent; nop is shared
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    // equals 2 calls on shared nop, because it's first and sees calls
    // against the Lockable interface introduced by the specific advisor
    assertEquals(2, nop1.getCount());
    assertEquals(0, nop2.getCount());
    lockable1.lock();
    assertTrue(lockable1.locked());
    assertFalse(lockable2.locked());
    assertEquals(5, nop1.getCount());
    assertEquals(0, nop2.getCount());
    PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
    assertEquals(5, nop1.getCount());
    assertEquals(0, nop2.getCount());
    packageVisibleMethod.doSomething();
    assertEquals(6, nop1.getCount());
    assertEquals(1, nop2.getCount());
    assertTrue(packageVisibleMethod instanceof Lockable);
    Lockable lockable3 = (Lockable) packageVisibleMethod;
    lockable3.lock();
    assertTrue(lockable3.locked());
    lockable3.unlock();
    assertFalse(lockable3.locked());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BeanFactory(org.springframework.beans.factory.BeanFactory) 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