Search in sources :

Example 31 with ProxyFactory

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"));
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 32 with ProxyFactory

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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestEvent(org.springframework.context.event.test.TestEvent) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) TestListener(org.springframework.context.TestListener) Test(org.junit.Test)

Example 33 with ProxyFactory

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();
}
Also used : Context(javax.naming.Context) ProxyFactory(org.springframework.aop.framework.ProxyFactory) EJBLocalObject(javax.ejb.EJBLocalObject) Test(org.junit.Test)

Example 34 with ProxyFactory

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();
}
Also used : Context(javax.naming.Context) ProxyFactory(org.springframework.aop.framework.ProxyFactory) CreateException(javax.ejb.CreateException) NamingException(javax.naming.NamingException)

Example 35 with ProxyFactory

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)));
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) Test(org.junit.Test)

Aggregations

ProxyFactory (org.springframework.aop.framework.ProxyFactory)81 Test (org.junit.Test)49 ITestBean (org.springframework.tests.sample.beans.ITestBean)20 TestBean (org.springframework.tests.sample.beans.TestBean)20 TimeStamped (org.springframework.tests.TimeStamped)8 INestedTestBean (org.springframework.tests.sample.beans.INestedTestBean)8 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)8 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)5 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)4 Context (javax.naming.Context)3 TargetSource (org.springframework.aop.TargetSource)3 Advised (org.springframework.aop.framework.Advised)3 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)3 Factory (org.springframework.cglib.proxy.Factory)3 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)3 IOException (java.io.IOException)2 EJBLocalObject (javax.ejb.EJBLocalObject)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2