Search in sources :

Example 96 with ITestBean

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

the class JndiObjectFactoryBeanTests method testLookupWithProxyInterfaceWithNotCache.

@Test
public void testLookupWithProxyInterfaceWithNotCache() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    final TestBean tb = new TestBean();
    jof.setJndiTemplate(new JndiTemplate() {

        @Override
        public Object lookup(String name) {
            if ("foo".equals(name)) {
                tb.setName("tb");
                tb.setAge(tb.getAge() + 1);
                return tb;
            }
            return null;
        }
    });
    jof.setJndiName("foo");
    jof.setProxyInterface(ITestBean.class);
    jof.setCache(false);
    jof.afterPropertiesSet();
    assertTrue(jof.getObject() instanceof ITestBean);
    ITestBean proxy = (ITestBean) jof.getObject();
    assertEquals("tb", tb.getName());
    assertEquals(1, tb.getAge());
    proxy.returnsThis();
    assertEquals(2, tb.getAge());
    proxy.haveBirthday();
    assertEquals(4, tb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 97 with ITestBean

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

the class JndiObjectFactoryBeanTests method testLookupWithProxyInterface.

@Test
public void testLookupWithProxyInterface() throws Exception {
    JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
    TestBean tb = new TestBean();
    jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
    jof.setJndiName("foo");
    jof.setProxyInterface(ITestBean.class);
    jof.afterPropertiesSet();
    assertTrue(jof.getObject() instanceof ITestBean);
    ITestBean proxy = (ITestBean) jof.getObject();
    assertEquals(0, tb.getAge());
    proxy.setAge(99);
    assertEquals(99, tb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ExpectedLookupTemplate(org.springframework.tests.mock.jndi.ExpectedLookupTemplate) Test(org.junit.Test)

Example 98 with ITestBean

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

the class JmsInvokerTests method receiveTimeoutExpired.

@Test
public void receiveTimeoutExpired() {
    JmsInvokerProxyFactoryBean pfb = new JmsInvokerProxyFactoryBean() {

        @Override
        protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
            // faking no message received
            return null;
        }
    };
    pfb.setServiceInterface(ITestBean.class);
    pfb.setConnectionFactory(this.mockConnectionFactory);
    pfb.setQueue(this.mockQueue);
    pfb.setReceiveTimeout(1500);
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    thrown.expect(RemoteTimeoutException.class);
    thrown.expectMessage("1500 ms");
    thrown.expectMessage("getAge");
    proxy.getAge();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) Queue(javax.jms.Queue) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 99 with ITestBean

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

the class JmsInvokerTests method doTestJmsInvokerProxyFactoryBeanAndServiceExporter.

private void doTestJmsInvokerProxyFactoryBeanAndServiceExporter(boolean dynamicQueue) throws Throwable {
    TestBean target = new TestBean("myname", 99);
    final JmsInvokerServiceExporter exporter = new JmsInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.setMessageConverter(new MockSimpleMessageConverter());
    exporter.afterPropertiesSet();
    JmsInvokerProxyFactoryBean pfb = new JmsInvokerProxyFactoryBean() {

        @Override
        protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
            Session mockExporterSession = mock(Session.class);
            ResponseStoringProducer mockProducer = new ResponseStoringProducer();
            given(mockExporterSession.createProducer(requestMessage.getJMSReplyTo())).willReturn(mockProducer);
            exporter.onMessage(requestMessage, mockExporterSession);
            assertTrue(mockProducer.closed);
            return mockProducer.response;
        }
    };
    pfb.setServiceInterface(ITestBean.class);
    pfb.setConnectionFactory(this.mockConnectionFactory);
    if (dynamicQueue) {
        pfb.setQueueName("myQueue");
    } else {
        pfb.setQueue(this.mockQueue);
    }
    pfb.setMessageConverter(new MockSimpleMessageConverter());
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
    proxy.setAge(50);
    assertEquals(50, proxy.getAge());
    proxy.setStringArray(new String[] { "str1", "str2" });
    assertTrue(Arrays.equals(new String[] { "str1", "str2" }, proxy.getStringArray()));
    try {
        proxy.exceptional(new IllegalStateException());
        fail("Should have thrown IllegalStateException");
    } catch (IllegalStateException ex) {
    // expected
    }
    try {
        proxy.exceptional(new IllegalAccessException());
        fail("Should have thrown IllegalAccessException");
    } catch (IllegalAccessException ex) {
    // expected
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Queue(javax.jms.Queue) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession)

Example 100 with ITestBean

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

the class TxNamespaceHandlerTests method isProxy.

@Test
public void isProxy() throws Exception {
    ITestBean bean = getTestBean();
    assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) 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