Package org.springframework.tests.sample.beans

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


    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);
    assertEquals(16, t.getAge());
    assertEquals(2, cba.getCalls());
  }
View Full Code Here


  @Test
  public void testLazyInitTargetSource() throws Exception {
    CountingTestBean.count = 0;
    BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
    assertTrue(AopUtils.isAopProxy(test));
    Advised advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource);
    assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count);
    assertEquals("Rod", test.getName());
    assertEquals("Kerry", test.getSpouse().getName());
    assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count);
    CountingTestBean.count = 0;
  }
View Full Code Here

  @Test
  public void testQuickTargetSourceCreator() throws Exception {
    ClassPathXmlApplicationContext bf =
        new ClassPathXmlApplicationContext(QUICK_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("test");
    assertFalse(AopUtils.isAopProxy(test));
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());

    // Now test the pooled one
    test = (ITestBean) bf.getBean(":test");
    assertTrue(AopUtils.isAopProxy(test));
    Advised advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof CommonsPoolTargetSource);
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());

    // Now test the ThreadLocal one
    test = (ITestBean) bf.getBean("%test");
    assertTrue(AopUtils.isAopProxy(test));
    advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof ThreadLocalTargetSource);
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());

    // Now test the Prototype TargetSource
    test = (ITestBean) bf.getBean("!test");
    assertTrue(AopUtils.isAopProxy(test));
    advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource);
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());


    ITestBean test2 = (ITestBean) bf.getBean("!test");
    assertFalse("Prototypes cannot be the same object", test == test2);
    assertEquals("Rod", test2.getName());
    assertEquals("Kerry", test2.getSpouse().getName());
    bf.close();
  }
View Full Code Here

      public long getTimeStamp() {
        return ts;
      }
    }));

    ITestBean proxied = (ITestBean) createProxy(pc);
    assertEquals(name, proxied.getName());
    TimeStamped intro = (TimeStamped) proxied;
    assertEquals(ts, intro.getTimeStamp());
  }
View Full Code Here

    }
    catch (AopConfigException ex) {
      assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
    }
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertEquals(target.getAge(), proxied.getAge());
  }
View Full Code Here

    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
    try {
      // TODO May fail on either call: may want to tighten up definition
      ITestBean proxied = (ITestBean) createProxy(pc);
      proxied.getName();
      fail("Bogus introduction");
    }
    catch (Exception ex) {
      // TODO used to catch UnknownAdviceTypeException, but
      // with CGLIB some errors are in proxy creation and are wrapped
View Full Code Here

  @Test
  public void testWithOptimizedProxy() throws Exception {
    BeanFactory beanFactory = new ClassPathXmlApplicationContext(OPTIMIZED_CONTEXT, CLASS);

    ITestBean testBean = (ITestBean) beanFactory.getBean("optimizedTestBean");
    assertTrue(AopUtils.isAopProxy(testBean));

    CountingBeforeAdvice beforeAdvice = (CountingBeforeAdvice) beanFactory.getBean("countingAdvice");

    testBean.setAge(23);
    testBean.getAge();

    assertEquals("Incorrect number of calls to proxy", 2, beforeAdvice.getCalls());
  }
View Full Code Here

    }
    catch (IllegalArgumentException ex) {
      //assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
    }
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertEquals(target.getAge(), proxied.getAge());
  }
View Full Code Here

    }
    catch (IllegalArgumentException ex) {
      assertTrue(ex.getMessage().indexOf("interface") > -1);
    }
    // Check it still works: proxy factory state shouldn't have been corrupted
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertEquals(target.getAge(), proxied.getAge());
  }
View Full Code Here

    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);
    }
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, ((Advised) proxied).getAdvisors().length);
  }
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.