Search in sources :

Example 1 with JndiTemplate

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

the class SimpleRemoteSlsbInvokerInterceptorTests method testLookupFailure.

@Test
public void testLookupFailure() throws Exception {
    final NamingException nex = new NamingException();
    final String jndiName = "foobar";
    JndiTemplate jt = new JndiTemplate() {

        @Override
        public Object lookup(String name) throws NamingException {
            assertTrue(jndiName.equals(name));
            throw nex;
        }
    };
    SimpleRemoteSlsbInvokerInterceptor si = new SimpleRemoteSlsbInvokerInterceptor();
    si.setJndiName("foobar");
    // default resourceRef=false should cause this to fail, as java:/comp/env will not
    // automatically be added
    si.setJndiTemplate(jt);
    try {
        si.afterPropertiesSet();
        fail("Should have failed with naming exception");
    } catch (NamingException ex) {
        assertTrue(ex == nex);
    }
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 2 with JndiTemplate

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

the class SimpleRemoteStatelessSessionProxyFactoryBeanTests method testCreateException.

@Test
public void testCreateException() 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(MyBusinessMethods.class);
    assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class);
    fb.setJndiTemplate(jt);
    // Need lifecycle methods
    fb.afterPropertiesSet();
    MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
    assertTrue(Proxy.isProxyClass(mbm.getClass()));
    try {
        mbm.getValue();
        fail("Should have failed to create EJB");
    } catch (RemoteException ex) {
    // expected
    }
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) RemoteException(java.rmi.RemoteException) CreateException(javax.ejb.CreateException) Test(org.junit.Test)

Example 3 with JndiTemplate

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

the class SimpleRemoteStatelessSessionProxyFactoryBeanTests method testInvokesMethodOnEjb3StyleBean.

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

        @Override
        public Object lookup(String name) {
            // parameterize
            assertTrue(name.equals("java:comp/env/" + jndiName));
            return myEjb;
        }
    };
    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());
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) Test(org.junit.Test)

Example 4 with JndiTemplate

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

the class SimpleRemoteStatelessSessionProxyFactoryBeanTests method testRemoteException.

@Override
@Test
public void testRemoteException() throws Exception {
    final RemoteException rex = new RemoteException();
    final String jndiName = "foo";
    MyEjb myEjb = mock(MyEjb.class);
    given(myEjb.getValue()).willThrow(rex);
    // TODO might want to control this behaviour...
    // Do we really want to call remove after a remote exception?
    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()));
    try {
        mbm.getValue();
        fail("Should've thrown remote exception");
    } catch (RemoteException ex) {
        assertSame("Threw expected RemoteException", rex, ex);
    }
    verify(myEjb).remove();
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) RemoteException(java.rmi.RemoteException) Test(org.junit.Test)

Example 5 with JndiTemplate

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

the class SimpleRemoteStatelessSessionProxyFactoryBeanTests method testNoBusinessInterfaceSpecified.

@Test
public void testNoBusinessInterfaceSpecified() throws Exception {
    // Will do JNDI lookup to get home but won't call create
    // Could actually try to figure out interface from create?
    final String jndiName = "foo";
    final MyHome home = mock(MyHome.class);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        public Object lookup(String name) throws NamingException {
            // 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
    // Don't set business interface
    fb.setJndiTemplate(jt);
    // Check it's a singleton
    assertTrue(fb.isSingleton());
    try {
        fb.afterPropertiesSet();
        fail("Should have failed to create EJB");
    } catch (IllegalArgumentException ex) {
        // TODO more appropriate exception?
        assertTrue(ex.getMessage().indexOf("businessInterface") != 1);
    }
    // Expect no methods on home
    verifyZeroInteractions(home);
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) Test(org.junit.Test)

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