Search in sources :

Example 16 with CountingBeforeAdvice

use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testCanAddThrowsAdviceWithoutAdvisor.

@Test
public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable {
    DefaultListableBeanFactory f = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(f).loadBeanDefinitions(new ClassPathResource(THROWS_ADVICE_CONTEXT, CLASS));
    MyThrowsHandler th = (MyThrowsHandler) f.getBean("throwsAdvice");
    CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice");
    assertEquals(0, cba.getCalls());
    assertEquals(0, th.getCalls());
    IEcho echo = (IEcho) f.getBean("throwsAdvised");
    int i = 12;
    echo.setA(i);
    assertEquals(i, echo.getA());
    assertEquals(2, cba.getCalls());
    assertEquals(0, th.getCalls());
    Exception expected = new Exception();
    try {
        echo.echoException(1, expected);
        fail();
    } catch (Exception ex) {
        assertEquals(expected, ex);
    }
    // No throws handler method: count should still be 0
    assertEquals(0, th.getCalls());
    // Handler knows how to handle this exception
    expected = new FileNotFoundException();
    try {
        echo.echoException(1, expected);
        fail();
    } catch (IOException ex) {
        assertEquals(expected, ex);
    }
    // One match
    assertEquals(1, th.getCalls("ioException"));
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) MyThrowsHandler(org.springframework.tests.aop.advice.MyThrowsHandler) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LockedException(test.mixin.LockedException) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 17 with CountingBeforeAdvice

use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testGetObjectTypeWithDirectTarget.

@Test
public void testGetObjectTypeWithDirectTarget() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
    // We have a counting before advice here
    CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice");
    assertEquals(0, cba.getCalls());
    ITestBean tb = (ITestBean) bf.getBean("directTarget");
    assertTrue(tb.getName().equals("Adam"));
    assertEquals(1, cba.getCalls());
    ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget");
    assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType()));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Aggregations

CountingBeforeAdvice (org.springframework.tests.aop.advice.CountingBeforeAdvice)17 Test (org.junit.Test)16 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)11 TestBean (org.springframework.tests.sample.beans.TestBean)11 Advisor (org.springframework.aop.Advisor)7 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)7 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)6 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)3 LockMixinAdvisor (test.mixin.LockMixinAdvisor)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileNotFoundException (java.io.FileNotFoundException)1 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1