Search in sources :

Example 51 with NopInterceptor

use of org.springframework.aop.testfixture.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");
    assertThat(bf.getBean("serializableSingleton")).as("Should be a Singleton").isSameAs(p);
    Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
    assertThat(p2).isEqualTo(p);
    assertThat(p2).isNotSameAs(p);
    assertThat(p2.getName()).isEqualTo("serializableSingleton");
    // Add unserializable advice
    Advice nop = new NopInterceptor();
    ((Advised) p).addAdvice(nop);
    // Check it still works
    assertThat(p2.getName()).isEqualTo(p2.getName());
    assertThat(SerializationTestUtils.isSerializable(p)).as("Not serializable because an interceptor isn't serializable").isFalse();
    // Remove offending interceptor...
    assertThat(((Advised) p).removeAdvice(nop)).isTrue();
    assertThat(SerializationTestUtils.isSerializable(p)).as("Serializable again because offending interceptor was removed").isTrue();
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Advice(org.aopalliance.aop.Advice) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Person(org.springframework.beans.testfixture.beans.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 52 with NopInterceptor

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

the class AbstractAopProxyTests method testValuesStick.

/**
 * Simple test that if we set values we can get them out again.
 */
@Test
public void testValuesStick() {
    int age1 = 33;
    int age2 = 37;
    String name = "tony";
    TestBean target1 = new TestBean();
    target1.setAge(age1);
    ProxyFactory pf1 = new ProxyFactory(target1);
    pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
    pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor()));
    ITestBean tb = (ITestBean) pf1.getProxy();
    assertThat(tb.getAge()).isEqualTo(age1);
    tb.setAge(age2);
    assertThat(tb.getAge()).isEqualTo(age2);
    assertThat(tb.getName()).isNull();
    tb.setName(name);
    assertThat(tb.getName()).isEqualTo(name);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 53 with NopInterceptor

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

the class AbstractAopProxyTests method testEquals.

@Test
public void testEquals() {
    IOther a = new AllInstancesAreEqual();
    IOther b = new AllInstancesAreEqual();
    NopInterceptor i1 = new NopInterceptor();
    NopInterceptor i2 = new NopInterceptor();
    ProxyFactory pfa = new ProxyFactory(a);
    pfa.addAdvice(i1);
    ProxyFactory pfb = new ProxyFactory(b);
    pfb.addAdvice(i2);
    IOther proxyA = (IOther) createProxy(pfa);
    IOther proxyB = (IOther) createProxy(pfb);
    assertThat(pfb.getAdvisors().length).isEqualTo(pfa.getAdvisors().length);
    assertThat(b).isEqualTo(a);
    assertThat(i2).isEqualTo(i1);
    assertThat(proxyB).isEqualTo(proxyA);
    assertThat(proxyB.hashCode()).isEqualTo(proxyA.hashCode());
    assertThat(proxyA.equals(a)).isFalse();
    // Equality checks were handled by the proxy
    assertThat(i1.getCount()).isEqualTo(0);
    // When we invoke A, it's NopInterceptor will have count == 1
    // and won't think it's equal to B's NopInterceptor
    proxyA.absquatulate();
    assertThat(i1.getCount()).isEqualTo(1);
    assertThat(proxyA.equals(proxyB)).isFalse();
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) IOther(org.springframework.beans.testfixture.beans.IOther) Test(org.junit.jupiter.api.Test)

Example 54 with NopInterceptor

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

the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.

@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    CountingBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
    pc.addAdvisor(advisor);
    assertThat(pc.isOpaque()).as("Opaque defaults to false").isFalse();
    pc.setOpaque(true);
    assertThat(pc.isOpaque()).as("Opaque now true for this config").isTrue();
    ITestBean proxied = (ITestBean) createProxy(pc);
    proxied.setAge(10);
    assertThat(proxied.getAge()).isEqualTo(10);
    assertThat(mba.getCalls()).isEqualTo(1);
    boolean condition = proxied instanceof Advised;
    assertThat(condition).as("Cannot be cast to Advised").isFalse();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) Test(org.junit.jupiter.api.Test)

Example 55 with NopInterceptor

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

the class AbstractAopProxyTests method testSerializationAdviceAndTargetNotSerializable.

@Test
public void testSerializationAdviceAndTargetNotSerializable() throws Exception {
    TestBean tb = new TestBean();
    assertThat(SerializationTestUtils.isSerializable(tb)).isFalse();
    ProxyFactory pf = new ProxyFactory(tb);
    pf.addAdvice(new NopInterceptor());
    ITestBean proxy = (ITestBean) createAopProxy(pf).getProxy();
    assertThat(SerializationTestUtils.isSerializable(proxy)).isFalse();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)60 Test (org.junit.jupiter.api.Test)57 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)39 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)33 TestBean (org.springframework.beans.testfixture.beans.TestBean)32 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.aop.testfixture.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 IOException (java.io.IOException)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 CountingAfterReturningAdvice (org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 Person (org.springframework.beans.testfixture.beans.Person)4 Nullable (org.springframework.lang.Nullable)4