Package org.springframework.tests.sample.beans

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


    EventPublicationInterceptor interceptor =
        (EventPublicationInterceptor) ctx.getBean("publisher");
    ProxyFactory factory = new ProxyFactory(target);
    factory.addAdvice(0, interceptor);

    ITestBean testBean = (ITestBean) factory.getProxy();

    // invoke any method on the advised proxy to see if the interceptor has been invoked
    testBean.getAge();

    // two events: ContextRefreshedEvent and TestEvent
    assertTrue("Interceptor must have published 2 events", listener.getEventCount() == 2);
    TestListener otherListener = (TestListener) ctx.getBean("&otherListener");
    assertTrue("Interceptor must have published 2 events", otherListener.getEventCount() == 2);
View Full Code Here


    mockTargetSource.setTarget(new Object());
    pc.setTargetSource(mockTargetSource);
    AopProxy aop = createAopProxy(pc);

    try {
      ITestBean tb = (ITestBean) aop.getProxy();
      // Note: exception param below isn't used
      tb.exceptional(expectedException);
      fail("Should have thrown exception raised by interceptor");
    }
    catch (Exception thrown) {
      assertEquals("exception matches", expectedException, thrown);
    }
View Full Code Here

    pc.addAdvice(mi);

    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();

    try {
      // Note: exception param below isn't used
      tb.getAge();
      fail("Should have wrapped exception raised by interceptor");
    }
    catch (UndeclaredThrowableException thrown) {
      assertEquals("exception matches", unexpectedException, thrown.getUndeclaredThrowable());
    }
View Full Code Here

    pc.addAdvice(mi);

    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();

    try {
      // Note: exception param below isn't used
      tb.getAge();
      fail("Should have wrapped exception raised by interceptor");
    }
    catch (RuntimeException thrown) {
      assertEquals("exception matches", unexpectedException, thrown);
    }
View Full Code Here

    };
    pc.addAdvice(tii);
    pc.setTarget(expectedTarget);
    AopProxy aop = createAopProxy(pc);

    ITestBean tb = (ITestBean) aop.getProxy();
    tb.getName();
    // Not safe to trap invocation
    //assertTrue(tii.invocation == target.invocation);

    //assertTrue(target.invocation.getProxy() == tb);
View Full Code Here

    testTestBeanIntroduction(pc);
  }

  private void testTestBeanIntroduction(ProxyFactory pc) {
    int newAge = 65;
    ITestBean itb = (ITestBean) createProxy(pc);
    itb.setAge(newAge);
    assertTrue(itb.getAge() == newAge);

    Lockable lockable = (Lockable) itb;
    assertFalse(lockable.locked());
    lockable.lock();

    assertTrue(itb.getAge() == newAge);
    try {
      itb.setAge(1);
      fail("Setters should fail when locked");
    }
    catch (LockedException ex) {
      // ok
    }
    assertTrue(itb.getAge() == newAge);

    // Unlock
    assertTrue(lockable.locked());
    lockable.unlock();
    itb.setAge(1);
    assertTrue(itb.getAge() == 1);
  }
View Full Code Here

    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
    pc.setTarget(tb);
    pc.addAdvisor(new StringSetterNullReplacementAdvice());

    ITestBean t = (ITestBean) pc.getProxy();
    int newAge = 5;
    t.setAge(newAge);
    assertTrue(t.getAge() == newAge);
    String newName = "greg";
    t.setName(newName);
    assertEquals(newName, t.getName());

    t.setName(null);
    // Null replacement magic should work
    assertTrue(t.getName().equals(""));
  }
View Full Code Here

   * which are sourced from matching advisors
   */
  @Test
  public void testCommonInterceptorAndAdvisor() throws Exception {
    BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
    ITestBean test1 = (ITestBean) bf.getBean("test1");
    assertTrue(AopUtils.isAopProxy(test1));

    Lockable lockable1 = (Lockable) test1;
    NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
    assertEquals(0, nop.getCount());

    ITestBean test2 = (ITestBean) bf.getBean("test2");
    Lockable lockable2 = (Lockable) test2;

    // Locking should be independent; nop is shared
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
View Full Code Here

   * hence no proxying, for this bean
   */
  @Test
  public void testCustomTargetSourceNoMatch() throws Exception {
    BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("test");
    assertFalse(AopUtils.isAopProxy(test));
    assertEquals("Rod", test.getName());
    assertEquals("Kerry", test.getSpouse().getName());
  }
View Full Code Here

  @Test
  public void testCustomPrototypeTargetSource() throws Exception {
    CountingTestBean.count = 0;
    BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("prototypeTest");
    assertTrue(AopUtils.isAopProxy(test));
    Advised advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource);
    assertEquals("Rod", test.getName());
    // Check that references survived prototype creation
    assertEquals("Kerry", test.getSpouse().getName());
    assertEquals("Only 2 CountingTestBeans instantiated", 2, CountingTestBean.count);
    CountingTestBean.count = 0;
  }
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.