Search in sources :

Example 6 with JndiTemplate

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

the class LocalSlsbInvokerInterceptorTests 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;
        }
    };
    LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor();
    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 7 with JndiTemplate

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

the class LocalStatelessSessionProxyFactoryBeanTests 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("java:comp/env/" + jndiName));
            return home;
        }
    };
    LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
    fb.setJndiName(jndiName);
    fb.setResourceRef(true);
    // 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)

Example 8 with JndiTemplate

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

the class LocalStatelessSessionProxyFactoryBeanTests 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) throws NamingException {
            // parameterize
            assertTrue(name.equals("java:comp/env/" + jndiName));
            return myEjb;
        }
    };
    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);
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) Test(org.junit.Test)

Example 9 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 10 with JndiTemplate

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

the class JmsTemplateTests method createTemplate.

private JmsTemplate createTemplate() {
    JmsTemplate template = new JmsTemplate();
    JndiDestinationResolver destMan = new JndiDestinationResolver();
    destMan.setJndiTemplate(new JndiTemplate() {

        @Override
        protected Context createInitialContext() {
            return jndiContext;
        }
    });
    template.setDestinationResolver(destMan);
    template.setSessionTransacted(useTransactedTemplate());
    return template;
}
Also used : Context(javax.naming.Context) JndiTemplate(org.springframework.jndi.JndiTemplate) JndiDestinationResolver(org.springframework.jms.support.destination.JndiDestinationResolver)

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