Search in sources :

Example 11 with JndiTemplate

use of org.springframework.jndi.JndiTemplate in project spring-framework by spring-projects.

the class LocalStatelessSessionProxyFactoryBeanTests method testInvokesMethod.

@Test
public void testInvokesMethod() throws Exception {
    final int value = 11;
    final String jndiName = "foo";
    MyEjb myEjb = mock(MyEjb.class);
    given(myEjb.getValue()).willReturn(value);
    final MyHome home = mock(MyHome.class);
    given(home.create()).willReturn(myEjb);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        public Object lookup(String name) throws NamingException {
            // parameterize
            assertTrue(name.equals("java:comp/env/" + jndiName));
            return home;
        }
    };
    LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
    fb.setJndiName(jndiName);
    fb.setResourceRef(true);
    fb.setBusinessInterface(MyBusinessMethods.class);
    fb.setJndiTemplate(jt);
    // Need lifecycle methods
    fb.afterPropertiesSet();
    MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
    assertTrue(Proxy.isProxyClass(mbm.getClass()));
    assertTrue(mbm.getValue() == value);
    verify(myEjb).remove();
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) Test(org.junit.Test)

Example 12 with JndiTemplate

use of org.springframework.jndi.JndiTemplate in project spring-framework by spring-projects.

the class SimpleRemoteSlsbInvokerInterceptorTests method configuredInterceptor.

private SimpleRemoteSlsbInvokerInterceptor configuredInterceptor(final Context mockCtx, String jndiName) throws Exception {
    SimpleRemoteSlsbInvokerInterceptor si = createInterceptor();
    si.setJndiTemplate(new JndiTemplate() {

        @Override
        protected Context createInitialContext() {
            return mockCtx;
        }
    });
    si.setResourceRef(true);
    si.setJndiName(jndiName);
    return si;
}
Also used : Context(javax.naming.Context) JndiTemplate(org.springframework.jndi.JndiTemplate)

Example 13 with JndiTemplate

use of org.springframework.jndi.JndiTemplate in project spring-framework by spring-projects.

the class SimpleRemoteStatelessSessionProxyFactoryBeanTests method testInvokesMethod.

@Test
public void testInvokesMethod() throws Exception {
    final int value = 11;
    final String jndiName = "foo";
    MyEjb myEjb = mock(MyEjb.class);
    given(myEjb.getValue()).willReturn(value);
    final MyHome home = mock(MyHome.class);
    given(home.create()).willReturn(myEjb);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        public Object lookup(String name) {
            // parameterize
            assertTrue(name.equals("java:comp/env/" + jndiName));
            return home;
        }
    };
    SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
    fb.setJndiName(jndiName);
    fb.setResourceRef(true);
    fb.setBusinessInterface(MyBusinessMethods.class);
    fb.setJndiTemplate(jt);
    // Need lifecycle methods
    fb.afterPropertiesSet();
    MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
    assertTrue(Proxy.isProxyClass(mbm.getClass()));
    assertEquals("Returns expected value", value, mbm.getValue());
    verify(myEjb).remove();
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) Test(org.junit.Test)

Example 14 with JndiTemplate

use of org.springframework.jndi.JndiTemplate in project spring-framework by spring-projects.

the class SimpleRemoteStatelessSessionProxyFactoryBeanTests method testCreateExceptionWithLocalBusinessInterface.

@Test
public void testCreateExceptionWithLocalBusinessInterface() throws Exception {
    final String jndiName = "foo";
    final CreateException cex = new CreateException();
    final MyHome home = mock(MyHome.class);
    given(home.create()).willThrow(cex);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        public Object lookup(String name) {
            // parameterize
            assertTrue(name.equals(jndiName));
            return home;
        }
    };
    SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
    fb.setJndiName(jndiName);
    // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
    fb.setBusinessInterface(MyLocalBusinessMethods.class);
    assertEquals(fb.getBusinessInterface(), MyLocalBusinessMethods.class);
    fb.setJndiTemplate(jt);
    // Need lifecycle methods
    fb.afterPropertiesSet();
    MyLocalBusinessMethods mbm = (MyLocalBusinessMethods) fb.getObject();
    assertTrue(Proxy.isProxyClass(mbm.getClass()));
    try {
        mbm.getValue();
        fail("Should have failed to create EJB");
    } catch (RemoteAccessException ex) {
        assertTrue(ex.getCause() == cex);
    }
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) RemoteAccessException(org.springframework.remoting.RemoteAccessException) CreateException(javax.ejb.CreateException) Test(org.junit.Test)

Example 15 with JndiTemplate

use of org.springframework.jndi.JndiTemplate in project spring-framework by spring-projects.

the class LocalSlsbInvokerInterceptorTests method configuredInterceptor.

protected LocalSlsbInvokerInterceptor configuredInterceptor(final Context mockCtx, final String jndiName) throws Exception {
    LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor();
    si.setJndiTemplate(new JndiTemplate() {

        @Override
        protected Context createInitialContext() throws NamingException {
            return mockCtx;
        }
    });
    si.setJndiName(jndiName);
    si.setResourceRef(true);
    si.afterPropertiesSet();
    return si;
}
Also used : Context(javax.naming.Context) JndiTemplate(org.springframework.jndi.JndiTemplate) NamingException(javax.naming.NamingException)

Aggregations

JndiTemplate (org.springframework.jndi.JndiTemplate)16 Test (org.junit.Test)12 CreateException (javax.ejb.CreateException)3 Context (javax.naming.Context)3 NamingException (javax.naming.NamingException)3 RemoteException (java.rmi.RemoteException)2 JndiDestinationResolver (org.springframework.jms.support.destination.JndiDestinationResolver)1 RemoteAccessException (org.springframework.remoting.RemoteAccessException)1