OpenEJB 1.0
I am currently working on a legacy J2EE project, which requires some rework to its EJB 2.0 implementation (e.g. session beans, entity beans). To do this, I investigated OpenEJB 1.0 - this open source library aims to establish a J2EE container inside a unit test. The aim of this is to facilitate the unit testing of EJBs. A thorough investigation revealed that OpenEJB 1.0 should only be used for lightweight session bean based only EJB implementations. I was able to successfully unit test session bean interfaces on session beans which did not call any other EJBs, or if so, only other session beans (e.g. session beans following the command pattern). Here are the steps I followed to unit test the session beans:
- copy the OpenEJB default 'logging.conf' and 'openejb.conf' to a properties directory in your project's unit test classpath; ensure that your log4j (through log4j.properties) configuration is outputting to the console (as the logging of OpenEJB is very useful at runtime to find JNDI references)
- configure OpenEJB in the constructor of the test case
public SBBeanTest(String testcaseName) throws Exception {
super(testcaseName);
System.setProperty
(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory");
System.setProperty("openejb.configuration", "conf/openejb.conf");
context = new InitialContext(System.getProperties());
} - configure the 'openejb.conf' xml file such that it is pointing to the base directory where the META-INF directory and ejb.xml of the session beans resides; specify the full path, if necessary (including drive, etc.)
<openejb>
<Container id="Default Stateless Container" ctype="STATELESS" />
<Deployments dir="\src\java" />
</openejb> - add a 'openejb-jar.xml' xml file to the META-INF directory where the ejb-jar.xml is located; this provides a mapping between the bean names (in the ejb.xml) and the JNDI names with which you are providing
ejb-jar.xml<ejb-jar>
openejb-jar.xml
<enterprise-beans>
<session>
<ejb-name>SB</ejb-name>
<home>foo.bar.SBHome</home>
<remote>foo.bar.SBRemote</remote>
<ejb-class<foo.bar.SB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar><openejb-jar>
<ejb-deployment ejb-name="SB"
deployment-id="ejb/foo/bar/SBHome"
container-id="Default Stateless Container"/>
</openejb-jar> - test that the session bean has been successfully looked up with OpenEJB
public void testSBNotNull() throws Exception {
Object ebObject = context.lookup("ejb/foo/bar/SBHome");
SBHome sessionbeanHome = (SBHome)
PortableRemoteObject.narrow(ebObject, SBPHome.class);
SB sessionbean = sbHome.create();
assertNotNull(sessionbean);
} - if configured correctly then the JUnit test will pass, with similar logger.debug output to this below
********************************************************************************
OpenEJB http://www.openejb.org
Startup: 30/08/06 10:39
Copyright 1999-2004 (C) OpenEJB Project, All Rights Reserved.
Version: 1.0
Build date: 20060226
Build time: 1701
********************************************************************************
WARN : No ApplicationServer was specified! The container system will only be accessible
by same-vm clients via the IntraVm Server.
DEBUG: Instantiating assembler class org.openejb.alt.assembler.classic.Assembler
WARN : Cannot find the configuration file [null], Trying conf/openejb.conf instead.
DEBUG: Containers : 4
DEBUG: Type Container ID
DEBUG: ENTITY Default BMP Container
DEBUG: ENTITY Default CMP Container
DEBUG: STATEFUL Default Stateful Container
DEBUG: STATELESS Default Stateless Container
DEBUG: Deployments : 1
DEBUG: OpenEJB.startup - Type Deployment ID
DEBUG: OpenEJB.startup - STATELESS ejb/foo/bar/SBHome
DEBUG: SecurityService : org.openejb.ri.sp.PseudoSecurityService
DEBUG: TransactionManager: org.openejb.core.TransactionManagerWrapper
INFO : OpenEJB ready.
No comments:
Post a Comment