Search in sources :

Example 41 with ITestBean

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

the class UnsupportedInterceptor method getIntroductionAdvisorProxy.

private ITestBean getIntroductionAdvisorProxy(TestBean target) {
    ProxyFactory pf = new ProxyFactory(ITestBean.class);
    pf.setProxyTargetClass(true);
    pf.addAdvisor(new LockMixinAdvisor());
    pf.setTarget(target);
    pf.setFrozen(true);
    pf.setExposeProxy(false);
    return (ITestBean) pf.getProxy();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) LockMixinAdvisor(test.mixin.LockMixinAdvisor)

Example 42 with ITestBean

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

the class JdkDynamicProxyTests method testTargetCanGetInvocationWithPrivateClass.

@Test
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable {
    final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {

        @Override
        protected void assertions(MethodInvocation invocation) {
            assertEquals(this, invocation.getThis());
            assertEquals("Invocation should be on ITestBean: " + invocation.getMethod(), ITestBean.class, invocation.getMethod().getDeclaringClass());
        }
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    TrapTargetInterceptor tii = new TrapTargetInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            // Assert that target matches BEFORE invocation returns
            assertEquals("Target is correct", expectedTarget, invocation.getThis());
            return super.invoke(invocation);
        }
    };
    pc.addAdvice(tii);
    pc.setTarget(expectedTarget);
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    tb.getName();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 43 with ITestBean

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

the class JdkDynamicProxyTests method testInterceptorIsInvokedWithNoTarget.

@Test
public void testInterceptorIsInvokedWithNoTarget() throws Throwable {
    // Test return value
    final int age = 25;
    MethodInterceptor mi = (invocation -> age);
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(mi);
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    assertEquals("correct return value", age, tb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 44 with ITestBean

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

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFallbackToDynamicProxy.

@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
    StaticApplicationContext sac = new StaticApplicationContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("proxyFactoryBean", "false");
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
    sac.registerSingleton("noInterfaces", NoInterfaces.class);
    sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
    sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class);
    sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class);
    sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.class);
    sac.refresh();
    MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
    NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
    ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly = (ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
    ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
    ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
    ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
    assertFalse(AopUtils.isCglibProxy(messageSource));
    assertTrue(AopUtils.isCglibProxy(noInterfaces));
    assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
    assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
    assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
    assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonNoInterceptor.getName();
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonToBeProxied.getAge();
    assertEquals(1, tapc.testInterceptor.nrOfInvocations);
    prototypeToBeProxied.getSpouse();
    assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.Test)

Example 45 with ITestBean

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

the class AutoProxyCreatorTests method testCustomAutoProxyCreator.

@Test
public void testCustomAutoProxyCreator() {
    StaticApplicationContext sac = new StaticApplicationContext();
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
    sac.registerSingleton("noInterfaces", NoInterfaces.class);
    sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
    sac.registerSingleton("singletonNoInterceptor", TestBean.class);
    sac.registerSingleton("singletonToBeProxied", TestBean.class);
    sac.registerPrototype("prototypeToBeProxied", TestBean.class);
    sac.refresh();
    MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
    NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
    ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly = (ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
    ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
    ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
    ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
    assertFalse(AopUtils.isCglibProxy(messageSource));
    assertTrue(AopUtils.isCglibProxy(noInterfaces));
    assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
    assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
    assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
    assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied));
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonNoInterceptor.getName();
    assertEquals(0, tapc.testInterceptor.nrOfInvocations);
    singletonToBeProxied.getAge();
    assertEquals(1, tapc.testInterceptor.nrOfInvocations);
    prototypeToBeProxied.getSpouse();
    assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.Test)

Aggregations

ITestBean (org.springframework.tests.sample.beans.ITestBean)221 Test (org.junit.Test)205 TestBean (org.springframework.tests.sample.beans.TestBean)127 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)37 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)24 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 IOException (java.io.IOException)15 Advisor (org.springframework.aop.Advisor)15 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)15 Method (java.lang.reflect.Method)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)14 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)13 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)13 MethodInvocation (org.aopalliance.intercept.MethodInvocation)12 Advised (org.springframework.aop.framework.Advised)12 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)11 LockedException (test.mixin.LockedException)11 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)10