Search in sources :

Example 6 with SideEffectBean

use of org.springframework.tests.sample.beans.SideEffectBean in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testPrototypeInstancesAreIndependent.

/**
	 * Uses its own bean factory XML for clarity
	 * @param beanName name of the ProxyFactoryBean definition that should
	 * be a prototype
	 */
private Object testPrototypeInstancesAreIndependent(String beanName) {
    // Initial count value set in bean factory XML
    int INITIAL_COUNT = 10;
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(PROTOTYPE_CONTEXT, CLASS));
    // Check it works without AOP
    SideEffectBean raw = (SideEffectBean) bf.getBean("prototypeTarget");
    assertEquals(INITIAL_COUNT, raw.getCount());
    raw.doWork();
    assertEquals(INITIAL_COUNT + 1, raw.getCount());
    raw = (SideEffectBean) bf.getBean("prototypeTarget");
    assertEquals(INITIAL_COUNT, raw.getCount());
    // Now try with advised instances
    SideEffectBean prototype2FirstInstance = (SideEffectBean) bf.getBean(beanName);
    assertEquals(INITIAL_COUNT, prototype2FirstInstance.getCount());
    prototype2FirstInstance.doWork();
    assertEquals(INITIAL_COUNT + 1, prototype2FirstInstance.getCount());
    SideEffectBean prototype2SecondInstance = (SideEffectBean) bf.getBean(beanName);
    assertFalse("Prototypes are not ==", prototype2FirstInstance == prototype2SecondInstance);
    assertEquals(INITIAL_COUNT, prototype2SecondInstance.getCount());
    assertEquals(INITIAL_COUNT + 1, prototype2FirstInstance.getCount());
    return prototype2FirstInstance;
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) SideEffectBean(org.springframework.tests.sample.beans.SideEffectBean) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 7 with SideEffectBean

use of org.springframework.tests.sample.beans.SideEffectBean in project spring-framework by spring-projects.

the class CommonsPool2TargetSourceTests method testFunctionality.

private void testFunctionality(String name) {
    SideEffectBean pooled = (SideEffectBean) beanFactory.getBean(name);
    assertEquals(INITIAL_COUNT, pooled.getCount());
    pooled.doWork();
    assertEquals(INITIAL_COUNT + 1, pooled.getCount());
    pooled = (SideEffectBean) beanFactory.getBean(name);
    // Just check that it works--we can't make assumptions
    // about the count
    pooled.doWork();
//assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
}
Also used : SideEffectBean(org.springframework.tests.sample.beans.SideEffectBean)

Example 8 with SideEffectBean

use of org.springframework.tests.sample.beans.SideEffectBean in project spring-framework by spring-projects.

the class HotSwappableTargetSourceTests method testValidSwaps.

@Test
public void testValidSwaps() {
    SideEffectBean target1 = (SideEffectBean) beanFactory.getBean("target1");
    SideEffectBean target2 = (SideEffectBean) beanFactory.getBean("target2");
    SideEffectBean proxied = (SideEffectBean) beanFactory.getBean("swappable");
    assertEquals(target1.getCount(), proxied.getCount());
    proxied.doWork();
    assertEquals(INITIAL_COUNT + 1, proxied.getCount());
    HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper");
    Object old = swapper.swap(target2);
    assertEquals("Correct old target was returned", target1, old);
    // TODO should be able to make this assertion: need to fix target handling
    // in AdvisedSupport
    //assertEquals(target2, ((Advised) proxied).getTarget());
    assertEquals(20, proxied.getCount());
    proxied.doWork();
    assertEquals(21, target2.getCount());
    // Swap it back
    swapper.swap(target1);
    assertEquals(target1.getCount(), proxied.getCount());
}
Also used : SideEffectBean(org.springframework.tests.sample.beans.SideEffectBean) Test(org.junit.Test)

Example 9 with SideEffectBean

use of org.springframework.tests.sample.beans.SideEffectBean in project spring-framework by spring-projects.

the class PrototypeTargetSourceTests method testPrototypeAndSingletonBehaveDifferently.

/**
	 * Test that multiple invocations of the prototype bean will result
	 * in no change to visible state, as a new instance is used.
	 * With the singleton, there will be change.
	 */
@Test
public void testPrototypeAndSingletonBehaveDifferently() {
    SideEffectBean singleton = (SideEffectBean) beanFactory.getBean("singleton");
    assertEquals(INITIAL_COUNT, singleton.getCount());
    singleton.doWork();
    assertEquals(INITIAL_COUNT + 1, singleton.getCount());
    SideEffectBean prototype = (SideEffectBean) beanFactory.getBean("prototype");
    assertEquals(INITIAL_COUNT, prototype.getCount());
    prototype.doWork();
    assertEquals(INITIAL_COUNT, prototype.getCount());
}
Also used : SideEffectBean(org.springframework.tests.sample.beans.SideEffectBean) Test(org.junit.Test)

Example 10 with SideEffectBean

use of org.springframework.tests.sample.beans.SideEffectBean in project spring-framework by spring-projects.

the class ThreadLocalTargetSourceTests method testReuseInSameThread.

@Test
public void testReuseInSameThread() {
    SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
    assertEquals(INITIAL_COUNT, apartment.getCount());
    apartment.doWork();
    assertEquals(INITIAL_COUNT + 1, apartment.getCount());
    apartment = (SideEffectBean) beanFactory.getBean("apartment");
    assertEquals(INITIAL_COUNT + 1, apartment.getCount());
}
Also used : SideEffectBean(org.springframework.tests.sample.beans.SideEffectBean) Test(org.junit.Test)

Aggregations

SideEffectBean (org.springframework.tests.sample.beans.SideEffectBean)10 Test (org.junit.Test)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1