use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class ApplicationListenerMethodAdapterTests method invokeListenerInvalidProxy.
@Test
public void invokeListenerInvalidProxy() {
Object target = new InvalidProxyTestBean();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.addInterface(SimpleService.class);
Object bean = proxyFactory.getProxy(getClass().getClassLoader());
Method method = ReflectionUtils.findMethod(InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class);
StaticApplicationListenerMethodAdapter listener = new StaticApplicationListenerMethodAdapter(method, bean);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("handleIt2");
listener.onApplicationEvent(createGenericTestEvent("test"));
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class EventPublicationInterceptorTests method testExpectedBehavior.
@Test
public void testExpectedBehavior() throws Exception {
TestBean target = new TestBean();
final TestListener listener = new TestListener();
class TestContext extends StaticApplicationContext {
@Override
protected void onRefresh() throws BeansException {
addApplicationListener(listener);
}
}
StaticApplicationContext ctx = new TestContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("applicationEventClass", TestEvent.class.getName());
// should automatically receive applicationEventPublisher reference
ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs);
ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
ctx.refresh();
EventPublicationInterceptor interceptor = (EventPublicationInterceptor) ctx.getBean("publisher");
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(0, interceptor);
ITestBean testBean = (ITestBean) factory.getProxy();
// invoke any method on the advised proxy to see if the interceptor has been invoked
testBean.getAge();
// two events: ContextRefreshedEvent and TestEvent
assertTrue("Interceptor must have published 2 events", listener.getEventCount() == 2);
TestListener otherListener = (TestListener) ctx.getBean("&otherListener");
assertTrue("Interceptor must have published 2 events", otherListener.getEventCount() == 2);
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class LocalSlsbInvokerInterceptorTests method testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods.
@Test
public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception {
Object retVal = new Object();
LocalInterface ejb = mock(LocalInterface.class);
given(ejb.targetMethod()).willReturn(retVal);
String jndiName = "foobar";
Context mockContext = mockContext(jndiName, ejb);
LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class });
pf.addAdvice(si);
BusinessMethods target = (BusinessMethods) pf.getProxy();
assertTrue(target.targetMethod() == retVal);
verify(mockContext).close();
verify(ejb).remove();
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class LocalSlsbInvokerInterceptorTests method testException.
private void testException(Exception expected) throws Exception {
LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class);
given(ejb.targetMethod()).willThrow(expected);
String jndiName = "foobar";
Context mockContext = mockContext(jndiName, ejb);
LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
ProxyFactory pf = new ProxyFactory(new Class<?>[] { LocalInterfaceWithBusinessMethods.class });
pf.addAdvice(si);
LocalInterfaceWithBusinessMethods target = (LocalInterfaceWithBusinessMethods) pf.getProxy();
try {
target.targetMethod();
fail("Should have thrown exception");
} catch (Exception thrown) {
assertTrue(thrown == expected);
}
verify(mockContext).close();
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class FormattingConversionServiceTests method proxiedFormatter.
@Test
public void proxiedFormatter() throws ParseException {
Formatter<?> formatter = new NumberStyleFormatter();
formattingService.addFormatter((Formatter<?>) new ProxyFactory(formatter).getProxy());
assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
Aggregations