Package org.springframework.tests.aop.interceptor

Examples of org.springframework.tests.aop.interceptor.NopInterceptor


    assertFalse(AopUtils.canApply(no, Object.class));
  }

  @Test
  public void testPointcutAlwaysApplies() {
    assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), Object.class));
    assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), TestBean.class));
  }
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()));
    assertEquals(0, pf.indexOf(nop));
    assertEquals(1, pf.indexOf(advisor));
    assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null)));
  }
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 {
      pf.removeAdvisor(-1);
    }
    catch (AopConfigException ex) {
      // Ok
    }

    try {
      pf.removeAdvisor(2);
    }
    catch (AopConfigException ex) {
      // Ok
    }

    assertEquals(5, proxied.getAge());
    assertEquals(4, nop2.getCount());
  }
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

      public Object invoke(MethodInvocation invocation) throws Throwable {
        throw new UnsupportedOperationException();
      }
    }

    NopInterceptor di = new NopInterceptor();
    NopInterceptor diUnused = new NopInterceptor();
    ProxyFactory factory = new ProxyFactory(new TestBean());
    factory.addAdvice(0, di);
    assertThat(factory.getProxy(), instanceOf(ITestBean.class));
    assertTrue(factory.adviceIncluded(di));
    assertTrue(!factory.adviceIncluded(diUnused));
View Full Code Here

  @Test
  public void testSerializable() throws Throwable {
    testSets();
    // Count is now 2
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
    NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
    p2.getName();
    assertEquals(2, nop2.getCount());
    p2.echo(null);
    assertEquals(3, nop2.getCount());
  }
View Full Code Here

  @Test
  public void testMatches() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));

    // Not advised, not under One
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(0, nop.getCount());

    // Will be advised
    assertEquals(target.getAge(), new One().getAge(proxied));
    assertEquals(1, nop.getCount());

    // Won't be advised
    assertEquals(target.getAge(), new One().nomatch(proxied));
    assertEquals(1, nop.getCount());
    assertEquals(3, cflow.getEvaluations());
  }
View Full Code Here

   */
  @Test
  public void testSelectiveApplication() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
    Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));

    // Not advised, not under One
    target.setAge(16);
    assertEquals(0, nop.getCount());

    // Not advised; under One but not a setter
    assertEquals(16, new One().getAge(proxied));
    assertEquals(0, nop.getCount());

    // Won't be advised
    new One().set(proxied);
    assertEquals(1, nop.getCount());

    // We saved most evaluations
    assertEquals(1, cflow.getEvaluations());
  }
View Full Code Here

  public void testSinglePattern() throws Throwable {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
    ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
    // Interceptor behind regexp advisor
    NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
    assertEquals(0, nop.getCount());

    int newAge = 12;
    // Not advised
    advised.exceptional(null);
    assertEquals(0, nop.getCount());
    advised.setAge(newAge);
    assertEquals(newAge, advised.getAge());
    // Only setter fired
    assertEquals(1, nop.getCount());
  }
View Full Code Here

TOP

Related Classes of org.springframework.tests.aop.interceptor.NopInterceptor

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.