Search in sources :

Example 1 with SideEffectBean

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

the class CommonsPool2TargetSourceTests method testConfigMixin.

@Test
void testConfigMixin() {
    SideEffectBean pooled = (SideEffectBean) beanFactory.getBean("pooledWithMixin");
    assertThat(pooled.getCount()).isEqualTo(INITIAL_COUNT);
    PoolingConfig conf = (PoolingConfig) beanFactory.getBean("pooledWithMixin");
    // TODO one invocation from setup
    // assertEquals(1, conf.getInvocations());
    pooled.doWork();
    // assertEquals("No objects active", 0, conf.getActive());
    assertThat(conf.getMaxSize()).as("Correct target source").isEqualTo(25);
    // assertTrue("Some free", conf.getFree() > 0);
    // assertEquals(2, conf.getInvocations());
    assertThat(conf.getMaxSize()).isEqualTo(25);
}
Also used : SideEffectBean(org.springframework.beans.testfixture.beans.SideEffectBean) Test(org.junit.jupiter.api.Test)

Example 2 with SideEffectBean

use of org.springframework.beans.testfixture.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");
    assertThat(raw.getCount()).isEqualTo(INITIAL_COUNT);
    raw.doWork();
    assertThat(raw.getCount()).isEqualTo(INITIAL_COUNT + 1);
    raw = (SideEffectBean) bf.getBean("prototypeTarget");
    assertThat(raw.getCount()).isEqualTo(INITIAL_COUNT);
    // Now try with advised instances
    SideEffectBean prototype2FirstInstance = (SideEffectBean) bf.getBean(beanName);
    assertThat(prototype2FirstInstance.getCount()).isEqualTo(INITIAL_COUNT);
    prototype2FirstInstance.doWork();
    assertThat(prototype2FirstInstance.getCount()).isEqualTo(INITIAL_COUNT + 1);
    SideEffectBean prototype2SecondInstance = (SideEffectBean) bf.getBean(beanName);
    assertThat(prototype2FirstInstance == prototype2SecondInstance).as("Prototypes are not ==").isFalse();
    assertThat(prototype2SecondInstance.getCount()).isEqualTo(INITIAL_COUNT);
    assertThat(prototype2FirstInstance.getCount()).isEqualTo(INITIAL_COUNT + 1);
    return prototype2FirstInstance;
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) SideEffectBean(org.springframework.beans.testfixture.beans.SideEffectBean) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 3 with SideEffectBean

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

the class HotSwappableTargetSourceTests method testBasicFunctionality.

/**
 * Check it works like a normal invoker
 */
@Test
public void testBasicFunctionality() {
    SideEffectBean proxied = (SideEffectBean) beanFactory.getBean("swappable");
    assertThat(proxied.getCount()).isEqualTo(INITIAL_COUNT);
    proxied.doWork();
    assertThat(proxied.getCount()).isEqualTo((INITIAL_COUNT + 1));
    proxied = (SideEffectBean) beanFactory.getBean("swappable");
    proxied.doWork();
    assertThat(proxied.getCount()).isEqualTo((INITIAL_COUNT + 2));
}
Also used : SideEffectBean(org.springframework.beans.testfixture.beans.SideEffectBean) Test(org.junit.jupiter.api.Test)

Example 4 with SideEffectBean

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

the class ThreadLocalTargetSourceTests method testUseDifferentManagedInstancesInSameThread.

/**
 * Check we can use two different ThreadLocalTargetSources
 * managing objects of different types without them interfering
 * with one another.
 */
@Test
public void testUseDifferentManagedInstancesInSameThread() {
    SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
    assertThat(apartment.getCount()).isEqualTo(INITIAL_COUNT);
    apartment.doWork();
    assertThat(apartment.getCount()).isEqualTo((INITIAL_COUNT + 1));
    ITestBean test = (ITestBean) beanFactory.getBean("threadLocal2");
    assertThat(test.getName()).isEqualTo("Rod");
    assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) SideEffectBean(org.springframework.beans.testfixture.beans.SideEffectBean) Test(org.junit.jupiter.api.Test)

Example 5 with SideEffectBean

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

the class ThreadLocalTargetSourceTests method testCanGetStatsViaMixin.

/**
 * Relies on introduction.
 */
@Test
public void testCanGetStatsViaMixin() {
    ThreadLocalTargetSourceStats stats = (ThreadLocalTargetSourceStats) beanFactory.getBean("apartment");
    // +1 because creating target for stats call counts
    assertThat(stats.getInvocationCount()).isEqualTo(1);
    SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
    apartment.doWork();
    // +1 again
    assertThat(stats.getInvocationCount()).isEqualTo(3);
    // + 1 for states call!
    assertThat(stats.getHitCount()).isEqualTo(3);
    apartment.doWork();
    assertThat(stats.getInvocationCount()).isEqualTo(6);
    assertThat(stats.getHitCount()).isEqualTo(6);
    // Only one thread so only one object can have been bound
    assertThat(stats.getObjectCount()).isEqualTo(1);
}
Also used : SideEffectBean(org.springframework.beans.testfixture.beans.SideEffectBean) Test(org.junit.jupiter.api.Test)

Aggregations

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