5 Writing an EJB-enabled JTR-Runner

If you have already grasped all the basic ideas and APIs behind writing a simple JSE JTR-Runner, it is not going to be hard learning how-to write an EJB-enabled JTR-Runner.


Basically you still have to inherit from jtr.runners.AbstractRunner, and leverage on both a properly edited jtr.xml configuration file and the jtr.enterprise.ServiceLocator helper class.


Let’s see a step by step introduction guided by the jtrTestBenchClient sample application.


The MultiplierSessionTest EJB-Runner


package jtr.test.jee.client;


import jtr.enterprise.ServiceLocator;

import jtr.runners.AbstractRunner;

import jtr.test.IOutcome;

import jtr.test.jee.MultiplierSessionRemote;


/**

*

* @author frusso

*/

public class MultiplierSessionTest extends AbstractRunner {

   

    /** Creates a new instance of MultiplierSessionTest */

    public MultiplierSessionTest() {

    }

   

    public void test() throws Throwable {

        MultiplierSessionRemote msr =

          ServiceLocator.<MultiplierSessionRemote>getEJBObject(ejbName,

                                                               MultiplierSessionRemote.class,

                                                               getEnterprise());

        System.out.println("Received message is "+msr.multiply(x,y));

    }


    public void receiveFailureNotification(Throwable throwable, String string) {

    }

   

    public void cleanupResources() {

    }

   

    public void enrichOutcome(IOutcome outcome) {

        outcome.setUserObject("Running against "+getEnterprise().getUniqueName()+

                              "\nEJB JNDI name is: "+ejbName);

    }

   

    private String ejbName;

    private double x,y;

}


As we can see form the source code above, the MultiplierSessionTest runner implements all of the methods discussed in the previous section. The only important thing to highlight is the way the runner can look-up every EJB it requires by means of the jtr.enterprise.ServiceLocator helper class.


The jtr.enterprise.ServiceLocator class

The ServiceLocator class lets you look-up EJBs that conforms to both the EJB 2.x and the EJB 3 specs. The method


public static <T> getEJBObject(String ejbName, Class remote, EnterpriseConfig enterprise)


requires as actual parameters:

  1. the JNDI name of the remote object that must be looked-up

  2. for EJB 3 EJBs the remote interface the retrieved object must adhere to, for EJB 2.x components the home class required for creating the EJB

  3. the enterprise configuration to be used

The type parameter T must be equal to the expected remote interface.


The EnterpriseConfig instance is the object-oriented representation of a jtr.xml file enterprise element, describing the properties required by the JTR-runtime to correctly perform the look-up operation.


Amongst all the methods inherited from the jtr.runners.AbstractRunner class, the getEnterprise() method allows you to obtain the configured enterprise configuration available to your runner.


Completing the example with the jtr.xml file


<?xml version = '1.0' encoding = 'UTF-8'?>


<!-- JTRunner is free software; you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation; either version 2, or (at your option)

    any later version.

-->


<test xmlns="http://jtrunner.sourceforge.net"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://jtrunner.sourceforge.net file:./config/jtr.xsd"

    runs="10">


    <!-- JTR components factories -->

    <factories>

        <factory key="IAssignmentPolicyFactory"

                 fqn="jtr.assigner.impl.DefaultAssignmentPolicyFactory" />

        <factory key="IOutcomeFactory" fqn="jtr.test.impl.DefaultOutcomeFactory" />

        <factory key="IWsHelperFactory" fqn="jtr.ws.jaxws.JaxWsHelperFactory" />

        <factory key="ITestCompletionListener"

                 fqn="jtr.test.impl.DefaultTestCompletionListener" />

        <factory key="ITestResultDisplayer" fqn="jtr.test.impl.DefaultTestResultDisplayer" />

        <factory key="ITestResultsExporter" fqn="jtr.test.results.exporters.impl.ExcelExporter" />

        <factory key="ITestScriptingEngine" fqn="jtr.script.impl.BshScriptEngine" />

        <factory key="ITestStatFunctions" fqn="jtr.lang.functions.impl.DefaultStatFunctionsFactory" />

    </factories>


    <!-- Runners -->

    <runner runs="4" enterprise="jboss">

        <runner-fqn>jtr.test.jee.client.MultiplierSessionTest</runner-fqn>

        <instance-count>2</instance-count>

        <parameters-assignment-policy>

            indexed

        </parameters-assignment-policy>

        <parameters>

            <param name="sleepTime" value="10" />

            <param name="ejbName" value="jtrTestBench/MultiplierSessionBean/remote" />

            <param name="x" value="10.4444" />

            <param name="y" value="3.455" />

        </parameters>

        <!--parameters>

            <param name="enterprise" value="glassfish" />

            <param name="sleepTime" value="10" />

            <param name="ejbName" value="jtr.test.jee.MultiplierSessionRemote"/>

            <param name="x" value="10.4444" />

            <param name="y" value="3.455" />

        </parameters-->

    </runner>


    <!-- Enterprise Configuration -->

    <enterprise uniqueName="jboss">

        <property name="java.naming.provider.url" value="jnp://localhost:1099/"/>

        <property name="java.naming.factory.initial" value="org.jnp.interfaces.NamingContextFactory"/>

        <property name="java.naming.factory.url.pkgs" value="org.jboss.naming.client"/>

    </enterprise>

   

    <enterprise uniqueName="glassfish">

        <property name="org.omg.CORBA.ORBInitialHost" value="localhost"/>

        <property name="org.omg.CORBA.ORBInitialPort" value="3700"/>

        <property name="java.naming.factory.initial"

                  value="com.sun.enterprise.naming.SerialInitContextFactory"/>

        <property name="java.naming.factory.url.pkgs" value="com.sun.enterprise.naming"/>

        <property name="java.naming.factory.state"

                  value="com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"/>

    </enterprise>

</test>


With the above configuration file we have introduced a new runner category, the MultiplierSessionTest, stating that there will be 2 instances each of which will run for 4 times per epoch. The first instance is going to be parameterized with the first parameter-set, while the second parameter-set will be injected into the second instance.


We also have to note that the first instance is tied to the enterprise configuration called jboss, while the second one overrides this default category-level configuration, binding itself to the glassfish enterprise configuration.


Thus, at the bottom of the file, we find the two enterprise configurations required by the two instances: jboss and glassfish.



Next step: writing a JMS-enabled JTR-Runner