Wednesday, September 06, 2006

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:

  1. 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 configuration is outputting to the console (as the logging of OpenEJB is very useful at runtime to find JNDI references)

  2. configure OpenEJB in the constructor of the test case
    public SBBeanTest(String testcaseName) throws Exception {
    (Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory");

    System.setProperty("openejb.configuration", "conf/openejb.conf");
    context = new InitialContext(System.getProperties());

  3. 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.)
    <Container id="Default Stateless Container" ctype="STATELESS" />
    <Deployments dir="\src\java" />

  4. 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-deployment ejb-name="SB"
    container-id="Default Stateless Container"/>

  5. 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();

  6. if configured correctly then the JUnit test will pass, with similar logger.debug output to this below
    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.
And there you have it - session beans unit tested!

No comments: