Examples of CountingBeforeAdvice


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

  /**
   * Also has counting before advice.
   */
  private void cglibAssertions(TestBean tb) {
    CountingBeforeAdvice cba = (CountingBeforeAdvice) beanFactory.getBean("countingBeforeAdvice");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor");
    assertEquals(0, cba.getCalls());
    assertEquals(0, nop.getCount());
    assertTrue(AopUtils.isCglibProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertEquals(2, nop.getCount());
    assertEquals(2, cba.getCalls());
  }
View Full Code Here

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

    assertFalse(proxyA.equals(proxyB));
  }

  @Test
  public void testBeforeAdvisorIsInvoked() {
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    @SuppressWarnings("serial")
    Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
      @Override
      public boolean matches(Method m, Class<?> targetClass) {
        return m.getParameterTypes().length == 0;
      }
    };
    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, cba.getCalls());
    assertEquals(0, cba.getCalls("getAge"));
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(1, cba.getCalls("getAge"));
    assertEquals(0, cba.getCalls("setAge"));
    // Won't be advised
    proxied.setAge(26);
    assertEquals(1, cba.getCalls());
    assertEquals(26, proxied.getAge());
  }
View Full Code Here

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

  }

  @Test
  public void testBeforeAdviceThrowsException() {
    final RuntimeException rex = new RuntimeException();
    @SuppressWarnings("serial")
    CountingBeforeAdvice ba = new CountingBeforeAdvice() {
      @Override
      public void before(Method m, Object[] args, Object target) throws Throwable {
        super.before(m, args, target);
        if (m.getName().startsWith("set"))
          throw rex;
      }
    };

    TestBean target = new TestBean();
    target.setAge(80);
    NopInterceptor nop1 = new NopInterceptor();
    NopInterceptor nop2 = new NopInterceptor();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(nop1);
    pf.addAdvice(ba);
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) createProxy(pf);
    // Won't throw an exception
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, ba.getCalls());
    assertEquals(1, ba.getCalls("getAge"));
    assertEquals(1, nop1.getCount());
    assertEquals(1, nop2.getCount());
    // Will fail, after invoking Nop1
    try {
      proxied.setAge(26);
      fail("before advice should have ended chain");
    }
    catch (RuntimeException ex) {
      assertEquals(rex, ex);
    }
    assertEquals(2, ba.getCalls());
    assertEquals(2, nop1.getCount());
    // Nop2 didn't get invoked when the exception was thrown
    assertEquals(1, nop2.getCount());
    // Shouldn't have changed value in joinpoint
    assertEquals(target.getAge(), proxied.getAge());
View Full Code Here

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

  }

  @Test
  public void testAddAdviceAtRuntime() {
    TestBean bean = new TestBean();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();

    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(bean);
    pf.setFrozen(false);
    pf.setOpaque(false);
    pf.setProxyTargetClass(true);

    TestBean proxy = (TestBean) pf.getProxy();
    assertTrue(AopUtils.isCglibProxy(proxy));

    proxy.getAge();
    assertEquals(0, cba.getCalls());

    ((Advised) proxy).addAdvice(cba);
    proxy.getAge();
    assertEquals(1, cba.getCalls());
  }
View Full Code Here

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

    assertEquals(1, cba.getCalls());
  }

  @Test
  public void testProxyProtectedMethod() throws Exception {
    CountingBeforeAdvice advice = new CountingBeforeAdvice();
    ProxyFactory proxyFactory = new ProxyFactory(new MyBean());
    proxyFactory.addAdvice(advice);
    proxyFactory.setProxyTargetClass(true);

    MyBean proxy = (MyBean) proxyFactory.getProxy();
    assertEquals(4, proxy.add(1, 3));
    assertEquals(1, advice.getCalls("add"));
  }
View Full Code Here

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

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

    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

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

    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

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

  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

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

  @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
TOP
Copyright © 2018 www.massapi.com. 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.