Search in sources :

Example 46 with ITestBean

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

the class ProxyFactoryBeanTests method testCanAddAndRemoveAspectInterfacesOnPrototype.

/**
 * Try adding and removing interfaces and interceptors on prototype.
 * Changes will only affect future references obtained from the factory.
 * Each instance will be independent.
 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnPrototype() {
    assertThat(factory.getBean("test2")).as("Shouldn't implement TimeStamped before manipulation").isNotInstanceOf(TimeStamped.class);
    ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2");
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to head of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
    TimeStamped ts = (TimeStamped) factory.getBean("test2");
    assertThat(ts.getTimeStamp()).isEqualTo(time);
    // Can remove
    config.removeAdvice(ti);
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    // Check no change on existing object reference
    assertThat(ts.getTimeStamp() == time).isTrue();
    assertThat(factory.getBean("test2")).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    // Won't affect existing reference
    assertThat(debugInterceptor.getCount() == 0).isTrue();
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(1);
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // Still invoked with old reference
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // not invoked with new object
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // Our own timestamped reference should still work
    assertThat(ts.getTimeStamp()).isEqualTo(time);
}
Also used : TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.jupiter.api.Test)

Example 47 with ITestBean

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

the class ProxyFactoryBeanTests method testDetectsInterfaces.

@Test
public void testDetectsInterfaces() {
    ProxyFactoryBean fb = new ProxyFactoryBean();
    fb.setTarget(new TestBean());
    fb.addAdvice(new DebugInterceptor());
    fb.setBeanFactory(new DefaultListableBeanFactory());
    ITestBean proxy = (ITestBean) fb.getObject();
    assertThat(AopUtils.isJdkDynamicProxy(proxy)).isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.jupiter.api.Test)

Example 48 with ITestBean

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

the class TestNamespaceHandler method testProxyingDecorator.

@Test
public void testProxyingDecorator() throws Exception {
    ITestBean bean = (ITestBean) this.beanFactory.getBean("debuggingTestBean");
    assertTestBean(bean);
    assertThat(AopUtils.isAopProxy(bean)).isTrue();
    Advisor[] advisors = ((Advised) bean).getAdvisors();
    assertThat(advisors.length).as("Incorrect number of advisors").isEqualTo(1);
    assertThat(advisors[0].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(DebugInterceptor.class);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 49 with ITestBean

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

the class OverloadLookup method testAutoProxiedLookup.

@Test
public void testAutoProxiedLookup() {
    OverloadLookup olup = (OverloadLookup) applicationContext.getBean("autoProxiedOverload");
    ITestBean jenny = olup.newTestBean();
    assertThat(jenny.getName()).isEqualTo("Jenny");
    assertThat(olup.testMethod()).isEqualTo("foo");
    assertInterceptorCount(2);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 50 with ITestBean

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

the class ConfigurationClassProcessingTests method configurationWithPostProcessor.

@Test
public void configurationWithPostProcessor() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithPostProcessor.class);
    @SuppressWarnings("deprecation") RootBeanDefinition placeholderConfigurer = new RootBeanDefinition(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.class);
    placeholderConfigurer.getPropertyValues().add("properties", "myProp=myValue");
    ctx.registerBeanDefinition("placeholderConfigurer", placeholderConfigurer);
    ctx.refresh();
    TestBean foo = ctx.getBean("foo", TestBean.class);
    ITestBean bar = ctx.getBean("bar", ITestBean.class);
    ITestBean baz = ctx.getBean("baz", ITestBean.class);
    assertThat(foo.getName()).isEqualTo("foo-processed-myValue");
    assertThat(bar.getName()).isEqualTo("bar-processed-myValue");
    assertThat(baz.getName()).isEqualTo("baz-processed-myValue");
    SpousyTestBean listener = ctx.getBean("listenerTestBean", SpousyTestBean.class);
    assertThat(listener.refreshed).isTrue();
    ctx.close();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) 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