Search in sources :

Example 21 with NopInterceptor

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

the class CreatesTestBean method testJdkIntroduction.

@Test
public void testJdkIntroduction() {
    ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals(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());
    assertEquals("introductionUsingJdk", tb.getName());
    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)

Example 22 with NopInterceptor

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

the class AbstractAopProxyTests method testMultiAdvice.

@Test
public void testMultiAdvice() throws Throwable {
    CountingMultiAdvice cca = new CountingMultiAdvice();
    @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {

        @Override
        public boolean matches(Method m, Class<?> targetClass) {
            return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesNoArgs);
    assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertEquals(0, cca.getCalls());
    assertEquals(0, cca.getCalls("getAge"));
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(2, cca.getCalls());
    assertEquals(2, cca.getCalls("getAge"));
    assertEquals(0, cca.getCalls("setAge"));
    // Won't be advised
    proxied.setAge(26);
    assertEquals(2, cca.getCalls());
    assertEquals(26, proxied.getAge());
    assertEquals(4, cca.getCalls());
    try {
        proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException) null));
        fail("Should have thrown CannotGetJdbcConnectionException");
    } catch (SpecializedUncheckedException ex) {
    // expected
    }
    assertEquals(6, cca.getCalls());
}
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) SQLException(java.sql.SQLException) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) 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) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 23 with NopInterceptor

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

the class AbstractAopProxyTests method testSerializationSerializableTargetAndAdvice.

@Test
public void testSerializationSerializableTargetAndAdvice() throws Throwable {
    SerializablePerson personTarget = new SerializablePerson();
    personTarget.setName("jim");
    personTarget.setAge(26);
    assertTrue(SerializationTestUtils.isSerializable(personTarget));
    ProxyFactory pf = new ProxyFactory(personTarget);
    CountingThrowsAdvice cta = new CountingThrowsAdvice();
    pf.addAdvice(new SerializableNopInterceptor());
    // Try various advice types
    pf.addAdvice(new CountingBeforeAdvice());
    pf.addAdvice(new CountingAfterReturningAdvice());
    pf.addAdvice(cta);
    Person p = (Person) createAopProxy(pf).getProxy();
    p.echo(null);
    assertEquals(0, cta.getCalls());
    try {
        p.echo(new IOException());
    } catch (IOException ex) {
    /* expected */
    }
    assertEquals(1, cta.getCalls());
    // Will throw exception if it fails
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertNotSame(p, p2);
    assertEquals(p.getName(), p2.getName());
    assertEquals(p.getAge(), p2.getAge());
    assertTrue("Deserialized object is an AOP proxy", AopUtils.isAopProxy(p2));
    Advised a1 = (Advised) p;
    Advised a2 = (Advised) p2;
    // Check we can manipulate state of p2
    assertEquals(a1.getAdvisors().length, a2.getAdvisors().length);
    // This should work as SerializablePerson is equal
    assertEquals("Proxies should be equal, even after one was serialized", p, p2);
    assertEquals("Proxies should be equal, even after one was serialized", p2, p);
    // Check we can add a new advisor to the target
    NopInterceptor ni = new NopInterceptor();
    p2.getAge();
    assertEquals(0, ni.getCount());
    a2.addAdvice(ni);
    p2.getAge();
    assertEquals(1, ni.getCount());
    cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
    p2.echo(null);
    assertEquals(1, cta.getCalls());
    try {
        p2.echo(new IOException());
    } catch (IOException ex) {
    }
    assertEquals(2, cta.getCalls());
}
Also used : CountingAfterReturningAdvice(org.springframework.tests.aop.advice.CountingAfterReturningAdvice) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) IOException(java.io.IOException) Person(org.springframework.tests.sample.beans.Person) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 24 with NopInterceptor

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

the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.

@Test
public void testCannotAddInterceptorWhenFrozen() 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);
    try {
        pc.addAdvice(0, new NopInterceptor());
        fail("Shouldn't be able to add interceptor when frozen");
    } catch (AopConfigException ex) {
        assertTrue(ex.getMessage().indexOf("frozen") > -1);
    }
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, ((Advised) proxied).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) Test(org.junit.Test)

Example 25 with NopInterceptor

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

the class AbstractAopProxyTests method testSerializationAdviceNotSerializable.

@Test
public void testSerializationAdviceNotSerializable() throws Exception {
    SerializablePerson sp = new SerializablePerson();
    assertTrue(SerializationTestUtils.isSerializable(sp));
    ProxyFactory pf = new ProxyFactory(sp);
    // This isn't serializable
    Advice i = new NopInterceptor();
    pf.addAdvice(i);
    assertFalse(SerializationTestUtils.isSerializable(i));
    Object proxy = createAopProxy(pf).getProxy();
    assertFalse(SerializationTestUtils.isSerializable(proxy));
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) Advice(org.aopalliance.aop.Advice) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) CountingAfterReturningAdvice(org.springframework.tests.aop.advice.CountingAfterReturningAdvice) 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