finished moving .ejb.* unit tests from .testsuite -> .context

This commit is contained in:
Chris Beams
2008-12-16 19:19:41 +00:00
parent 0b0c8f1506
commit 2952609672
7 changed files with 132 additions and 21 deletions

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.parsing;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.core.CollectionFactory;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class CollectingReaderEventListener implements ReaderEventListener {
private final List defaults = new LinkedList();
private final Map componentDefinitions = CollectionFactory.createLinkedMapIfPossible(8);
private final Map aliasMap = CollectionFactory.createLinkedMapIfPossible(8);
private final List imports = new LinkedList();
public void defaultsRegistered(DefaultsDefinition defaultsDefinition) {
this.defaults.add(defaultsDefinition);
}
public List getDefaults() {
return Collections.unmodifiableList(this.defaults);
}
public void componentRegistered(ComponentDefinition componentDefinition) {
this.componentDefinitions.put(componentDefinition.getName(), componentDefinition);
}
public ComponentDefinition getComponentDefinition(String name) {
return (ComponentDefinition) this.componentDefinitions.get(name);
}
public ComponentDefinition[] getComponentDefinitions() {
Collection collection = this.componentDefinitions.values();
return (ComponentDefinition[]) collection.toArray(new ComponentDefinition[collection.size()]);
}
public void aliasRegistered(AliasDefinition aliasDefinition) {
List aliases = (List) this.aliasMap.get(aliasDefinition.getBeanName());
if(aliases == null) {
aliases = new ArrayList();
this.aliasMap.put(aliasDefinition.getBeanName(), aliases);
}
aliases.add(aliasDefinition);
}
public List getAliases(String beanName) {
List aliases = (List) this.aliasMap.get(beanName);
return aliases == null ? null : Collections.unmodifiableList(aliases);
}
public void importProcessed(ImportDefinition importDefinition) {
this.imports.add(importDefinition);
}
public List getImports() {
return Collections.unmodifiableList(this.imports);
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ejb.config;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.parsing.CollectingReaderEventListener;
import org.springframework.beans.factory.parsing.ComponentDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
/**
* @author Torsten Juergeleit
* @author Juergen Hoeller
* @author Chris Beams
*/
public class JeeNamespaceHandlerEventTests {
private CollectingReaderEventListener eventListener = new CollectingReaderEventListener();
private XmlBeanDefinitionReader reader;
private DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@Before
public void setUp() throws Exception {
this.reader = new XmlBeanDefinitionReader(this.beanFactory);
this.reader.setEventListener(this.eventListener);
this.reader.loadBeanDefinitions(new ClassPathResource("jeeNamespaceHandlerTests.xml", getClass()));
}
@Test
public void testJndiLookupComponentEventReceived() {
ComponentDefinition component = this.eventListener.getComponentDefinition("simple");
assertTrue(component instanceof BeanComponentDefinition);
}
@Test
public void testLocalSlsbComponentEventReceived() {
ComponentDefinition component = this.eventListener.getComponentDefinition("simpleLocalEjb");
assertTrue(component instanceof BeanComponentDefinition);
}
@Test
public void testRemoteSlsbComponentEventReceived() {
ComponentDefinition component = this.eventListener.getComponentDefinition("simpleRemoteEjb");
assertTrue(component instanceof BeanComponentDefinition);
}
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ejb.config;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.ITestBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean;
import org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean;
import org.springframework.jndi.JndiObjectFactoryBean;
/**
* @author Rob Harrop
* @author Juergen Hoeller
* @author Chris Beams
*/
public class JeeNamespaceHandlerTests {
private ConfigurableListableBeanFactory beanFactory;
@Before
public void setUp() throws Exception {
GenericApplicationContext ctx = new GenericApplicationContext();
new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(
new ClassPathResource("jeeNamespaceHandlerTests.xml", getClass()));
ctx.refresh();
this.beanFactory = ctx.getBeanFactory();
this.beanFactory.getBeanNamesForType(ITestBean.class);
}
@Test
public void testSimpleDefinition() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("simple");
assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
assertPropertyValue(beanDefinition, "resourceRef", "true");
}
@Test
public void testComplexDefinition() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complex");
assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
assertPropertyValue(beanDefinition, "resourceRef", "true");
assertPropertyValue(beanDefinition, "cache", "true");
assertPropertyValue(beanDefinition, "lookupOnStartup", "true");
assertPropertyValue(beanDefinition, "exposeAccessContext", "true");
assertPropertyValue(beanDefinition, "expectedType", "com.myapp.DefaultFoo");
assertPropertyValue(beanDefinition, "proxyInterface", "com.myapp.Foo");
assertPropertyValue(beanDefinition, "defaultObject", "myValue");
}
@Test
public void testWithEnvironment() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("withEnvironment");
assertPropertyValue(beanDefinition, "jndiEnvironment", "foo=bar");
assertPropertyValue(beanDefinition, "defaultObject", new RuntimeBeanReference("myBean"));
}
@Test
public void testWithReferencedEnvironment() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("withReferencedEnvironment");
assertPropertyValue(beanDefinition, "jndiEnvironment", new RuntimeBeanReference("myEnvironment"));
assertFalse(beanDefinition.getPropertyValues().contains("environmentRef"));
}
@Test
public void testSimpleLocalSlsb() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("simpleLocalEjb");
assertEquals(LocalStatelessSessionProxyFactoryBean.class.getName(), beanDefinition.getBeanClassName());
assertPropertyValue(beanDefinition, "businessInterface", ITestBean.class.getName());
assertPropertyValue(beanDefinition, "jndiName", "ejb/MyLocalBean");
}
@Test
public void testSimpleRemoteSlsb() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("simpleRemoteEjb");
assertEquals(SimpleRemoteStatelessSessionProxyFactoryBean.class.getName(), beanDefinition.getBeanClassName());
assertPropertyValue(beanDefinition, "businessInterface", ITestBean.class.getName());
assertPropertyValue(beanDefinition, "jndiName", "ejb/MyRemoteBean");
}
@Test
public void testComplexLocalSlsb() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complexLocalEjb");
assertEquals(LocalStatelessSessionProxyFactoryBean.class.getName(), beanDefinition.getBeanClassName());
assertPropertyValue(beanDefinition, "businessInterface", ITestBean.class.getName());
assertPropertyValue(beanDefinition, "jndiName", "ejb/MyLocalBean");
assertPropertyValue(beanDefinition, "cacheHome", "true");
assertPropertyValue(beanDefinition, "lookupHomeOnStartup", "true");
assertPropertyValue(beanDefinition, "resourceRef", "true");
assertPropertyValue(beanDefinition, "jndiEnvironment", "foo=bar");
}
@Test
public void testComplexRemoteSlsb() throws Exception {
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complexRemoteEjb");
assertEquals(SimpleRemoteStatelessSessionProxyFactoryBean.class.getName(), beanDefinition.getBeanClassName());
assertPropertyValue(beanDefinition, "businessInterface", ITestBean.class.getName());
assertPropertyValue(beanDefinition, "jndiName", "ejb/MyRemoteBean");
assertPropertyValue(beanDefinition, "cacheHome", "true");
assertPropertyValue(beanDefinition, "lookupHomeOnStartup", "true");
assertPropertyValue(beanDefinition, "resourceRef", "true");
assertPropertyValue(beanDefinition, "jndiEnvironment", "foo=bar");
assertPropertyValue(beanDefinition, "homeInterface", "org.springframework.beans.ITestBean");
assertPropertyValue(beanDefinition, "refreshHomeOnConnectFailure", "true");
assertPropertyValue(beanDefinition, "cacheSessionBean", "true");
}
private void assertPropertyValue(BeanDefinition beanDefinition, String propertyName, Object expectedValue) {
assertEquals("Property '" + propertyName + "' incorrect",
expectedValue, beanDefinition.getPropertyValues().getPropertyValue(propertyName).getValue());
}
}

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
default-lazy-init="true">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<props>
<prop key="myDs">jdbc/MyDataSource</prop>
</props>
</property>
</bean>
<!-- JNDI Lookup Tests -->
<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource"/>
<jee:jndi-lookup id="complex"
jndi-name="${myDs}"
cache="true"
resource-ref="true"
lookup-on-startup="true"
expose-access-context="true"
expected-type="com.myapp.DefaultFoo"
proxy-interface="com.myapp.Foo"
default-value="myValue"/>
<jee:jndi-lookup id="withEnvironment" jndi-name="jdbc/MyDataSource" default-ref="myBean">
<jee:environment>foo=bar</jee:environment>
</jee:jndi-lookup>
<jee:jndi-lookup id="withReferencedEnvironment" jndi-name="jdbc/MyDataSource" environment-ref="myEnvironment"/>
<util:properties id="myEnvironment">
<prop key="foo">bar</prop>
</util:properties>
<!-- Local EJB Tests -->
<jee:local-slsb id="simpleLocalEjb" jndi-name="ejb/MyLocalBean"
business-interface="org.springframework.beans.ITestBean"/>
<jee:local-slsb id="complexLocalEjb"
jndi-name="ejb/MyLocalBean"
business-interface="org.springframework.beans.ITestBean"
cache-home="true"
lookup-home-on-startup="true"
resource-ref="true">
<jee:environment>foo=bar</jee:environment>
</jee:local-slsb>
<!-- Remote EJB Tests -->
<jee:remote-slsb id="simpleRemoteEjb" jndi-name="ejb/MyRemoteBean"
business-interface="org.springframework.beans.ITestBean"/>
<jee:remote-slsb id="complexRemoteEjb"
jndi-name="ejb/MyRemoteBean"
business-interface="org.springframework.beans.ITestBean"
cache-home="true"
lookup-home-on-startup="true"
resource-ref="true"
home-interface="org.springframework.beans.ITestBean"
refresh-home-on-connect-failure="true"
cache-session-bean="true">
<jee:environment>foo=bar</jee:environment>
</jee:remote-slsb>
</beans>

View File

@@ -0,0 +1,226 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ejb.support;
import static org.easymock.EasyMock.*;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenContext;
import javax.ejb.SessionContext;
import javax.jms.Message;
import javax.naming.NamingException;
import junit.framework.TestCase;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.access.BootstrapException;
import org.springframework.beans.factory.support.StaticListableBeanFactory;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @since 21.05.2003
*/
public class EjbSupportTests extends TestCase {
public void testSfsb() throws CreateException {
SessionContext sc = createMock(SessionContext.class);
replay(sc);
final BeanFactory bf = new StaticListableBeanFactory();
BeanFactoryLocator bfl = new BeanFactoryLocator() {
public BeanFactoryReference useBeanFactory(String factoryKey)
throws FatalBeanException {
return new BeanFactoryReference() {
public BeanFactory getFactory() {
return bf;
}
public void release() throws FatalBeanException {
// nothing to do in default implementation
}
};
}
};
// Basically the test is what needed to be implemented here!
@SuppressWarnings("serial")
class MySfsb extends AbstractStatefulSessionBean {
public void ejbCreate() throws CreateException {
loadBeanFactory();
assertTrue(getBeanFactory() == bf);
}
public void ejbActivate() throws EJBException, RemoteException {
throw new UnsupportedOperationException("ejbActivate");
}
public void ejbPassivate() throws EJBException, RemoteException {
throw new UnsupportedOperationException("ejbPassivate");
}
}
MySfsb sfsb = new MySfsb();
sfsb.setBeanFactoryLocator(bfl);
sfsb.setSessionContext(sc);
sfsb.ejbCreate();
assertTrue(sc == sfsb.getSessionContext());
}
/**
* Check there's a helpful message if no JNDI key is present.
*/
public void testHelpfulNamingLookupMessage() throws NamingException, CreateException {
SimpleNamingContextBuilder.emptyActivatedContextBuilder();
SessionContext sc = createMock(SessionContext.class);
replay(sc);
// Leave with default XmlBeanFactoryLoader
// Basically the test is what needed to be implemented here!
@SuppressWarnings("serial")
AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() {
public void onEjbCreate() {
}
};
slsb.setSessionContext(sc);
try {
slsb.ejbCreate();
fail();
}
catch (BeansException ex) {
assertTrue(ex.getMessage().indexOf("environment") != -1);
assertTrue(ex.getMessage().indexOf("ejb/BeanFactoryPath") != -1);
}
}
public void testSlsb() throws Exception {
SessionContext sc = createMock(SessionContext.class);
replay(sc);
final BeanFactory bf = new StaticListableBeanFactory();
BeanFactoryLocator bfl = new BeanFactoryLocator() {
public BeanFactoryReference useBeanFactory(String factoryKey) throws FatalBeanException {
return new BeanFactoryReference() {
public BeanFactory getFactory() {
return bf;
}
public void release() throws FatalBeanException {
// nothing to do in default implementation
}
};
}
};
@SuppressWarnings("serial")
AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() {
protected void onEjbCreate() throws CreateException {
assertTrue(getBeanFactory() == bf);
}
};
// Must call this method before ejbCreate()
slsb.setBeanFactoryLocator(bfl);
slsb.setSessionContext(sc);
assertTrue(sc == slsb.getSessionContext());
slsb.ejbCreate();
try {
slsb.ejbActivate();
fail("Shouldn't allow activation of SLSB");
}
catch (IllegalStateException ex) {
// Ok
}
try {
slsb.ejbPassivate();
fail("Shouldn't allow passivation of SLSB");
}
catch (IllegalStateException ex) {
// Ok
}
}
public void testJmsMdb() throws Exception {
MessageDrivenContext sc = createMock(MessageDrivenContext.class);
replay(sc);
final BeanFactory bf = new StaticListableBeanFactory();
BeanFactoryLocator bfl = new BeanFactoryLocator() {
public BeanFactoryReference useBeanFactory(String factoryKey) throws FatalBeanException {
return new BeanFactoryReference() {
public BeanFactory getFactory() {
return bf;
}
public void release() throws FatalBeanException {
// nothing to do in default implementation
}
};
}
};
@SuppressWarnings("serial")
AbstractJmsMessageDrivenBean mdb = new AbstractJmsMessageDrivenBean() {
protected void onEjbCreate() {
assertTrue(getBeanFactory() == bf);
}
public void onMessage(Message msg) {
throw new UnsupportedOperationException("onMessage");
}
};
// Must call this method before ejbCreate()
mdb.setBeanFactoryLocator(bfl);
mdb.setMessageDrivenContext(sc);
assertTrue(sc == mdb.getMessageDrivenContext());
mdb.ejbCreate();
}
public void testCannotLoadBeanFactory() throws Exception {
SessionContext sc = createMock(SessionContext.class);
replay(sc);
BeanFactoryLocator bfl = new BeanFactoryLocator() {
public BeanFactoryReference useBeanFactory(String factoryKey) throws FatalBeanException {
throw new BootstrapException("", null);
}};
@SuppressWarnings("serial")
AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() {
protected void onEjbCreate() throws CreateException {
}
};
// Must call this method before ejbCreate()
slsb.setBeanFactoryLocator(bfl);
slsb.setSessionContext(sc);
try {
slsb.ejbCreate();
fail();
}
catch (BeansException ex) {
// Ok
}
}
}