Package org.springframework.tests.sample.beans

Examples of org.springframework.tests.sample.beans.ITestBean


    ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
    NopInterceptor di = new NopInterceptor();
    TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge");
    pc.addAdvisor(sp);
    pc.setTarget(tb);
    ITestBean it = (ITestBean) createProxy(pc);
    assertEquals(di.getCount(), 0);
    it.getAge();
    assertEquals(di.getCount(), 1);
    it.setAge(11);
    assertEquals(it.getAge(), 11);
    assertEquals(di.getCount(), 2);
  }
View Full Code Here


    assertEquals("onlyJdk", tb.getName());
  }

  @Test
  public void testJdkProxyWithDoubleProxying() {
    ITestBean tb = (ITestBean) beanFactory.getBean("doubleJdk");
    jdkAssertions(tb, 2);
    assertEquals("doubleJdk", tb.getName());
  }
View Full Code Here

    assertEquals("doubleJdk", tb.getName());
  }

  @Test
  public void testJdkIntroduction() {
    ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals(0, nop.getCount());
    assertTrue(AopUtils.isJdkDynamicProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertTrue("Introduction was made", tb instanceof TimeStamped);
    assertEquals(0, ((TimeStamped) tb).getTimeStamp());
    assertEquals(3, nop.getCount());
    assertEquals("introductionUsingJdk", tb.getName());

    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");

    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    tb.setAge(65);
    assertEquals(65, tb.getAge());
    lockable1.lock();
    assertTrue(lockable1.locked());
    // Shouldn't affect second
    assertFalse(lockable2.locked());
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    try {
      tb.setAge(6);
      fail("Mixin should have locked this object");
    }
View Full Code Here

      public boolean matches(Method m, Class<?> targetClass) {
        return "haveBirthday".equals(m.getName());
      }
    };
    pc.addAdvisor(advisor);
    ITestBean it = (ITestBean) createProxy(pc);

    final int age = 20;
    it.setAge(age);
    assertEquals(age, it.getAge());
    // Should return the age before the third, AOP-induced birthday
    assertEquals(age + 2, it.haveBirthday());
    // Return the final age produced by 3 birthdays
    assertEquals(age + 3, it.getAge());
  }
View Full Code Here

    NameSaver saver = new NameSaver();

    pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, nameReverter));
    pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, saver));
    ITestBean it = (ITestBean) createProxy(pc);

    String name1 = "tony";
    String name2 = "gordon";

    tb.setName(name1);
    assertEquals(name1, tb.getName());

    it.setName(name2);
    // NameReverter saved it back
    assertEquals(name1, it.getName());
    assertEquals(2, saver.names.size());
    assertEquals(name2, saver.names.get(0));
    assertEquals(name1, saver.names.get(1));
  }
View Full Code Here

    }
  }

  @Test
  public void testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
    ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals("NOP should not have done any work yet", 0, nop.getCount());
    assertTrue(AopUtils.isJdkDynamicProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertTrue("Introduction was made", tb instanceof TimeStamped);
    assertEquals(0, ((TimeStamped) tb).getTimeStamp());
    assertEquals(3, nop.getCount());

    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");

    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    tb.setAge(65);
    assertEquals(65, tb.getAge());
    lockable1.lock();
    assertTrue(lockable1.locked());
    // Shouldn't affect second
    assertFalse(lockable2.locked());
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    try {
      tb.setAge(6);
      fail("Mixin should have locked this object");
    }
View Full Code Here

  public void testProxyIsBoundBeforeTargetSourceInvoked() {
    final TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new DebugInterceptor());
    pf.setExposeProxy(true);
    final ITestBean proxy = (ITestBean) createProxy(pf);
    Advised config = (Advised) proxy;
    // This class just checks proxy is bound before getTarget() call
    config.setTargetSource(new TargetSource() {
      @Override
      public Class<?> getTargetClass() {
        return TestBean.class;
      }

      @Override
      public boolean isStatic() {
        return false;
      }

      @Override
      public Object getTarget() throws Exception {
        assertEquals(proxy, AopContext.currentProxy());
        return target;
      }

      @Override
      public void releaseTarget(Object target) throws Exception {
      }
    });

    // Just test anything: it will fail if context wasn't found
    assertEquals(0, proxy.getAge());
  }
View Full Code Here

    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

    }
  }

  @Test
  public void testJdkProxyWithWildcardMatch() {
    ITestBean tb = (ITestBean) beanFactory.getBean("jdk1");
    jdkAssertions(tb, 1);
    assertEquals("jdk1", tb.getName());
  }
View Full Code Here

    assertEquals("cglib1", tb.getName());
  }

  @Test
  public void testWithFrozenProxy() {
    ITestBean testBean = (ITestBean) beanFactory.getBean("frozenBean");
    assertTrue(((Advised)testBean).isFrozen());
  }
View Full Code Here

TOP

Related Classes of org.springframework.tests.sample.beans.ITestBean

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.