Package org.springframework.tests.aop.interceptor

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


    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    // Permit proxy and invocation checkers to get context from AopContext
    pf1.setExposeProxy(true);
    NopInterceptor di1 = new NopInterceptor();
    pf1.addAdvice(0, di1);
    pf1.addAdvice(1, new ProxyMatcherInterceptor());
    pf1.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
    pf1.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
    // Must be first
    pf1.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
    ITestBean advised1 = (ITestBean) pf1.getProxy();
    advised1.setAge(age1); // = 1 invocation

    TestBean target2 = new TestBean();
    ProxyFactory pf2 = new ProxyFactory(target2);
    pf2.setExposeProxy(true);
    NopInterceptor di2 = new NopInterceptor();
    pf2.addAdvice(0, di2);
    pf2.addAdvice(1, new ProxyMatcherInterceptor());
    pf2.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
    pf2.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
    pf2.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
    //System.err.println(pf2.toProxyConfigString());
    ITestBean advised2 = (ITestBean) createProxy(pf2);
    advised2.setAge(age2);
    advised1.setSpouse(advised2); // = 2 invocations

    assertEquals("Advised one has correct age", age1, advised1.getAge()); // = 3 invocations
    assertEquals("Advised two has correct age", age2, advised2.getAge());
    // Means extra call on advised 2
    assertEquals("Advised one spouse has correct age", age2, advised1.getSpouse().getAge()); // = 4 invocations on 1 and another one on 2

    assertEquals("one was invoked correct number of times", 4, di1.getCount());
    // Got hit by call to advised1.getSpouse().getAge()
    assertEquals("one was invoked correct number of times", 3, di2.getCount());
  }
View Full Code Here


  public void testReentrance() {
    int age1 = 33;

    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    NopInterceptor di1 = new NopInterceptor();
    pf1.addAdvice(0, di1);
    ITestBean advised1 = (ITestBean) createProxy(pf1);
    advised1.setAge(age1); // = 1 invocation
    advised1.setSpouse(advised1); // = 2 invocations

    assertEquals("one was invoked correct number of times", 2, di1.getCount());

    assertEquals("Advised one has correct age", age1, advised1.getAge()); // = 3 invocations
    assertEquals("one was invoked correct number of times", 3, di1.getCount());

    // = 5 invocations, as reentrant call to spouse is advised also
    assertEquals("Advised spouse has correct age", age1, advised1.getSpouse().getAge());

    assertEquals("one was invoked correct number of times", 5, di1.getCount());
  }
View Full Code Here

    assertEquals("one was invoked correct number of times", 5, di1.getCount());
  }

  @Test
  public void testTargetCanGetProxy() {
    NopInterceptor di = new NopInterceptor();
    INeedsToSeeProxy target = new TargetChecker();
    ProxyFactory proxyFactory = new ProxyFactory(target);
    proxyFactory.setExposeProxy(true);
    assertTrue(proxyFactory.isExposeProxy());

    proxyFactory.addAdvice(0, di);
    INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(proxyFactory);
    assertEquals(0, di.getCount());
    assertEquals(0, target.getCount());
    proxied.incrementViaThis();
    assertEquals("Increment happened", 1, target.getCount());

    assertEquals("Only one invocation via AOP as use of this wasn't proxied", 1, di.getCount());
    // 1 invocation
    assertEquals("Increment happened", 1, proxied.getCount());
    proxied.incrementViaProxy(); // 2 invoocations
    assertEquals("Increment happened", 2, target.getCount());
    assertEquals("3 more invocations via AOP as the first call was reentrant through the proxy", 4, di.getCount());
  }
View Full Code Here

  @Test
  public void testCanCastProxyToProxyConfig() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);

    ITestBean t = (ITestBean) createProxy(pc);
    assertEquals(0, di.getCount());
    t.setAge(23);
    assertEquals(23, t.getAge());
    assertEquals(2, di.getCount());

    Advised advised = (Advised) t;
    assertEquals("Have 1 advisor", 1, advised.getAdvisors().length);
    assertEquals(di, advised.getAdvisors()[0].getAdvice());
    NopInterceptor di2 = new NopInterceptor();
    advised.addAdvice(1, di2);
    t.getName();
    assertEquals(3, di.getCount());
    assertEquals(1, di2.getCount());
    // will remove di
    advised.removeAdvisor(0);
    t.getAge();
    // Unchanged
    assertEquals(3, di.getCount());
    assertEquals(2, di2.getCount());

    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    assertEquals(0, cba.getCalls());
    advised.addAdvice(cba);
    t.setAge(16);
View Full Code Here

  public void testAdviceImplementsIntroductionInfo() throws Throwable {
    TestBean tb = new TestBean();
    String name = "tony";
    tb.setName(name);
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(di);
    final long ts = 37;
    pc.addAdvice(new DelegatingIntroductionInterceptor(new TimeStamped() {
      @Override
      public long getTimeStamp() {
View Full Code Here

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

  public void testCannotAddAdvisorWhenFrozenUsingCast() 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);
    Advised advised = (Advised) proxied;

    assertTrue(pc.isFrozen());
    try {
      advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
      fail("Shouldn't be able to add Advisor when frozen");
    }
    catch (AopConfigException ex) {
      assertTrue(ex.getMessage().indexOf("frozen") > -1);
    }
View Full Code Here

  public void testCannotRemoveAdvisorWhenFrozen() 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);
    Advised advised = (Advised) proxied;

    assertTrue(pc.isFrozen());
View Full Code Here

  @Test
  public void testUseAsHashKey() {
    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    pf1.addAdvice(new NopInterceptor());
    ITestBean proxy1 = (ITestBean) createProxy(pf1);

    TestBean target2 = new TestBean();
    ProxyFactory pf2 = new ProxyFactory(target2);
    pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
View Full Code Here

  @Test
  public void testProxyConfigString() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(new Class<?>[] {ITestBean.class});
    pc.addAdvice(new NopInterceptor());
    MethodBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
    pc.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) createProxy(pc);
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.