org.springframework.jndi.JndiTemplate Java Examples

The following examples show how to use org.springframework.jndi.JndiTemplate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleRemoteSlsbInvokerInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
	}
}
 
Example #2
Source File: DbConfig.java    From jqm with Apache License 2.0 6 votes vote down vote up
@Bean
public DataSource dataSource()
{
    try
    {
        // When running inside a container, use its resource directory.
        return (DataSource) new JndiTemplate().lookup("jdbc/spring_ds");
    }
    catch (NamingException e)
    {
        // When running on the command line, just create a temporary file DB (only needed for debug).
        System.out.println("JNDI datasource does not exist - falling back on hard coded DS");
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:./target/TEST.db");
        ds.setUser("sa");
        ds.setPassword("sa");
        return ds;
    }
}
 
Example #3
Source File: LocalSlsbInvokerInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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;
}
 
Example #4
Source File: LocalSlsbInvokerInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example #5
Source File: LocalSlsbInvokerInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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;
}
 
Example #6
Source File: LocalSlsbInvokerInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example #7
Source File: LocalSlsbInvokerInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #8
Source File: LocalSlsbInvokerInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
	}
}
 
Example #9
Source File: SimpleRemoteSlsbInvokerInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example #10
Source File: SimpleRemoteSlsbInvokerInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example #11
Source File: SpringBootWebApplication.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Bean(destroyMethod = "")
public MongoClient mongoClient(@Value("${mongo.host:}") String host, @Value("${mongo.jndiName:}") String jndiName) throws NamingException
{
	if (!Strings.isNullOrEmpty(jndiName))
	{
		JndiTemplate jndiTemplate = new JndiTemplate();
		return jndiTemplate.lookup(jndiName, MongoClient.class);
	}
	else if (!Strings.isNullOrEmpty(host))
	{
		return MongoClients.create(host);
	}
	else
	{
		throw new RuntimeException("Either mongo.host or mongo.jndiName must be set");
	}
}
 
Example #12
Source File: SpringWebSphereUowTransactionManager.java    From jadira with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance
 * @param template The JndiTemplate
 */
private TransactionAdapter(JndiTemplate template) {
    try {
        extendedJTATransaction = template.lookup("java:comp/websphere/ExtendedJTATransaction");

    } catch (NamingException e) {
        throw new IllegalStateException("Could not find ExtendedJTATransaction in JNDI: " + e.getMessage(),
                e);
    }
}
 
Example #13
Source File: SimpleRemoteStatelessSessionProxyFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #14
Source File: SimpleRemoteStatelessSessionProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #15
Source File: SimpleRemoteSlsbInvokerInterceptorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #16
Source File: LocalStatelessSessionProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: JmsTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #18
Source File: LocalStatelessSessionProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #19
Source File: PersistenceAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public <T> T lookup(String jndiName, Class<T> requiredType) throws Exception {
	JndiLocatorDelegate locator = new JndiLocatorDelegate();
	if (jndiEnvironment instanceof JndiTemplate) {
		locator.setJndiTemplate((JndiTemplate) jndiEnvironment);
	}
	else if (jndiEnvironment instanceof Properties) {
		locator.setJndiEnvironment((Properties) jndiEnvironment);
	}
	else if (jndiEnvironment != null) {
		throw new IllegalStateException("Illegal 'jndiEnvironment' type: " + jndiEnvironment.getClass());
	}
	locator.setResourceRef(resourceRef);
	return locator.lookup(jndiName, requiredType);
}
 
Example #20
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public <T> T lookup(String jndiName, Class<T> requiredType) throws Exception {
	JndiLocatorDelegate locator = new JndiLocatorDelegate();
	if (jndiEnvironment instanceof JndiTemplate) {
		locator.setJndiTemplate((JndiTemplate) jndiEnvironment);
	}
	else if (jndiEnvironment instanceof Properties) {
		locator.setJndiEnvironment((Properties) jndiEnvironment);
	}
	else if (jndiEnvironment != null) {
		throw new IllegalStateException("Illegal 'jndiEnvironment' type: " + jndiEnvironment.getClass());
	}
	locator.setResourceRef(resourceRef);
	return locator.lookup(jndiName, requiredType);
}
 
Example #21
Source File: JtaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
	// Rely on default serialization; just initialize state after deserialization.
	ois.defaultReadObject();

	// Create template for client-side JNDI lookup.
	this.jndiTemplate = new JndiTemplate();

	// Perform a fresh lookup for JTA handles.
	initUserTransactionAndTransactionManager();
	initTransactionSynchronizationRegistry();
}
 
Example #22
Source File: PersistenceAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
public <T> T lookup(String jndiName, Class<T> requiredType) throws Exception {
	JndiLocatorDelegate locator = new JndiLocatorDelegate();
	if (jndiEnvironment instanceof JndiTemplate) {
		locator.setJndiTemplate((JndiTemplate) jndiEnvironment);
	}
	else if (jndiEnvironment instanceof Properties) {
		locator.setJndiEnvironment((Properties) jndiEnvironment);
	}
	else if (jndiEnvironment != null) {
		throw new IllegalStateException("Illegal 'jndiEnvironment' type: " + jndiEnvironment.getClass());
	}
	locator.setResourceRef(resourceRef);
	return locator.lookup(jndiName, requiredType);
}
 
Example #23
Source File: SimpleRemoteStatelessSessionProxyFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example #24
Source File: SimpleRemoteStatelessSessionProxyFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
	}
}
 
Example #25
Source File: SimpleRemoteStatelessSessionProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
	}
}
 
Example #26
Source File: JtaConfigurerTest.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * If there is a NamingException when calling JNDI, this configurer should handle it and throw an appropriate
 * ConfigurationException.
 */
@Test(expected = ConfigurationException.class)
public void testAfterPropertiesSet_ConfigurationException() throws Exception {
    initializeJtaJndiConfig();
    JtaConfigurer configurer = new JtaConfigurer();
    configurer.setJndiTemplate(new JndiTemplate() {
        @Override
        public Object lookup(String name) throws NamingException {
            throw new NamingException("Throwing NamingException from JtaConfigurerTest!");
        }
    });
    configurer.afterPropertiesSet();
}
 
Example #27
Source File: SimpleRemoteStatelessSessionProxyFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
}
 
Example #28
Source File: SimpleRemoteSlsbInvokerInterceptorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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;
}
 
Example #29
Source File: SimpleRemoteStatelessSessionProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #30
Source File: LocalStatelessSessionProxyFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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) throws NamingException {
			// parameterize
			assertTrue(name.equals(jndiName));
			return home;
		}
	};

	LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
	fb.setJndiName(jndiName);
	fb.setResourceRef(false);	// no 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 (EjbAccessException ex) {
		assertSame(cex, ex.getCause());
	}
}