Package org.springframework.tests.aop.advice

Examples of org.springframework.tests.aop.advice.CountingBeforeAdvice


    assertTrue("Advisors should not be empty", advisors.length > 0);
  }

  @Test
  public void testAdviceInvokedCorrectly() throws Exception {
    CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter");
    CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter");

    ITestBean bean = getTestBean();

    assertEquals("Incorrect initial getAge count", 0, getAgeCounter.getCalls("getAge"));
    assertEquals("Incorrect initial getName count", 0, getNameCounter.getCalls("getName"));

    bean.getAge();

    assertEquals("Incorrect getAge count on getAge counter", 1, getAgeCounter.getCalls("getAge"));
    assertEquals("Incorrect getAge count on getName counter", 0, getNameCounter.getCalls("getAge"));

    bean.getName();

    assertEquals("Incorrect getName count on getName counter", 1, getNameCounter.getCalls("getName"));
    assertEquals("Incorrect getName count on getAge counter", 0, getAgeCounter.getCalls("getName"));
  }
View Full Code Here


    try {
      Messenger bean = (Messenger) ctx.getBean("messenger");
      assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
      assertTrue("Bean is not an Advised object", bean instanceof Advised);

      CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
      assertEquals(0, advice.getCalls());
      bean.getMessage();
      assertEquals(1, advice.getCalls());
    } finally {
      ctx.close();
    }
  }
View Full Code Here

    try {
      Messenger bean = (Messenger) ctx.getBean("messenger");
      assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
      assertTrue("Bean is not an Advised object", bean instanceof Advised);

      CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
      assertEquals(0, advice.getCalls());
      bean.getMessage();
      assertEquals(1, advice.getCalls());
    } finally {
      ctx.close();
    }
  }
View Full Code Here

  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()));
  }
View Full Code Here

  @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();
View Full Code Here

  @Test
  public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertEquals(-1, pf.indexOf(new NopInterceptor()));
View Full Code Here

  @Test
  public void testRemoveAdvisorByReference() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(cba);
    pf.addAdvice(nop);
    pf.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertEquals(1, cba.getCalls());
    assertEquals(1, nop.getCount());
    assertTrue(pf.removeAdvisor(advisor));
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(2, nop.getCount());
    assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null)));
  }
View Full Code Here

  @Test
  public void testRemoveAdvisorByIndex() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(cba);
    pf.addAdvice(nop);
    pf.addAdvisor(advisor);
    NopInterceptor nop2 = new NopInterceptor();
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertEquals(1, cba.getCalls());
    assertEquals(1, nop.getCount());
    assertEquals(1, nop2.getCount());
    // Removes counting before advisor
    pf.removeAdvisor(1);
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(2, nop.getCount());
    assertEquals(2, nop2.getCount());
    // Removes Nop1
    pf.removeAdvisor(0);
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(2, nop.getCount());
    assertEquals(3, nop2.getCount());

    // Check out of bounds
    try {
View Full Code Here

  @Test
  public void testReplaceAdvisor() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba1 = new CountingBeforeAdvice();
    CountingBeforeAdvice cba2 = new CountingBeforeAdvice();
    Advisor advisor1 = new DefaultPointcutAdvisor(cba1);
    Advisor advisor2 = new DefaultPointcutAdvisor(cba2);
    pf.addAdvisor(advisor1);
    pf.addAdvice(nop);
    ITestBean proxied = (ITestBean) pf.getProxy();
    // Use the type cast feature
    // Replace etc methods on advised should be same as on ProxyFactory
    Advised advised = (Advised) proxied;
    proxied.setAge(5);
    assertEquals(1, cba1.getCalls());
    assertEquals(0, cba2.getCalls());
    assertEquals(1, nop.getCount());
    assertFalse(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2));
    assertTrue(advised.replaceAdvisor(advisor1, advisor2));
    assertEquals(advisor2, pf.getAdvisors()[0]);
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba1.getCalls());
    assertEquals(2, nop.getCount());
    assertEquals(1, cba2.getCalls());
    assertFalse(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1));
  }
View Full Code Here

TOP

Related Classes of org.springframework.tests.aop.advice.CountingBeforeAdvice

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.