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();
}
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();
}
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());
}
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);
}
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);
}
Aggregations