Search in sources :

Example 51 with ITestBean

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

the class ScopingTests method testScopedProxyConfiguration.

@Test
public void testScopedProxyConfiguration() throws Exception {
    TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedInterfaceDep");
    ITestBean spouse = singleton.getSpouse();
    boolean condition = spouse instanceof ScopedObject;
    assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
    String beanName = "scopedProxyInterface";
    String scopedBeanName = "scopedTarget." + beanName;
    // get hidden bean
    assertThat(spouse.getName()).isEqualTo(flag);
    ITestBean spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
    // the scope proxy has kicked in
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // create a new bean
    customScope.createNewScope = true;
    // get the bean again from the BF
    spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
    // make sure the name has been updated
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // get the bean again
    spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ScopedObject(org.springframework.aop.scope.ScopedObject) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 52 with ITestBean

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

the class ScopingTests method testScopedProxyConfigurationWithClasses.

@Test
public void testScopedProxyConfigurationWithClasses() throws Exception {
    TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedClassDep");
    ITestBean spouse = singleton.getSpouse();
    boolean condition = spouse instanceof ScopedObject;
    assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
    String beanName = "scopedProxyClass";
    String scopedBeanName = "scopedTarget." + beanName;
    // get hidden bean
    assertThat(spouse.getName()).isEqualTo(flag);
    TestBean spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
    // the scope proxy has kicked in
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // create a new bean
    customScope.createNewScope = true;
    flag = "boo";
    // get the bean again from the BF
    spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
    // make sure the name has been updated
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // get the bean again
    spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ScopedObject(org.springframework.aop.scope.ScopedObject) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 53 with ITestBean

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

the class LazyInitTargetSourceTests method testLazyInitSingletonTargetSource.

@Test
public void testLazyInitSingletonTargetSource() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SINGLETON_CONTEXT);
    bf.preInstantiateSingletons();
    ITestBean tb = (ITestBean) bf.getBean("proxy");
    assertThat(bf.containsSingleton("target")).isFalse();
    assertThat(tb.getAge()).isEqualTo(10);
    assertThat(bf.containsSingleton("target")).isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 54 with ITestBean

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

the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInDelegate.

@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
    final long t = 1001L;
    class Tester implements TimeStamped, ITester {

        @Override
        public void foo() throws Exception {
        }

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    // assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
    TimeStamped ts = (TimeStamped) pf.getProxy();
    assertThat(ts.getTimeStamp() == t).isTrue();
    ((ITester) ts).foo();
    ((ITestBean) ts).getAge();
}
Also used : TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 55 with ITestBean

use of org.springframework.beans.testfixture.beans.ITestBean 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)

Aggregations

ITestBean (org.springframework.beans.testfixture.beans.ITestBean)210 Test (org.junit.jupiter.api.Test)199 TestBean (org.springframework.beans.testfixture.beans.TestBean)121 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)44 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)31 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)23 Advisor (org.springframework.aop.Advisor)22 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)20 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)19 Method (java.lang.reflect.Method)16 TimeStamped (org.springframework.core.testfixture.TimeStamped)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 TimestampIntroductionInterceptor (org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor)15 IOException (java.io.IOException)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)14 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)13 Advised (org.springframework.aop.framework.Advised)13