Home > Articles > Programming

  • Print
  • + Share This
Like this article? We recommend

Example Code

To better illustrate how the Cactus testing works, I have included three sample Enterprise Java Beans. These beans do take advantage of XDoclet, so if you want to build them yourself you need to use XDoclet to produce the interfaces and homes.

The first bean is a very simple Entity Bean that stores a name value pair and a reference to a collection of children:

package com.dzrealms.example.entity;

import com.dzrealms.example.util.ExampleUtil;

import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import java.util.Collection;

/**
  * @ejb.bean jndi-name="example/remote/ExampleBean"
  *           local-jndi-name="example/local/ExampleBean"
  *           cmp-version="2.x"
  *           primkey-field="ID"
  *           schema="Example"
  *
  * @ejb.interface remote-class="com.dzrealms.example.entity.ExampleRemote"
  *                local-class="com.dzrealms.example.entity.ExampleLocal"
  *
  * @ejb.home remote-class="com.dzrealms.example.entity.ExampleHomeRemote"
  *           local-class="com.dzrealms.example.entity.ExampleHomeLocal"
  *
  * @ejb.finder 
  *   signature="com.dzrealms.example.entity.ExampleHomeLocal
  *     findByName(java.lang.String u)"
  *   query="select object(u) from Example u where u.name = ?1"
  */
public abstract class ExampleBean implements EntityBean {

   /**
    * @ejb.create-method
    * @return Primary Key
    * @throws CreateException
    */
   public String ejbCreate() throws CreateException {
     setID(ExampleUtil.generateGUID(this));
     return null;
   }

   public void ejbPostCreate() {

   }

   /**
    * @ejb.interface-method
    * @ejb.persistence
    * @return Primary Key
    */
   public abstract String getID();

   /**
    * @param i Set the primary key
    */
   public abstract void setID(String i);

   /**
    * @ejb.interface-method
    * @ejb.persistence
    * @return the name of this bean
    */
   public abstract String getName();

   /**
    * @param s Set the  name of this bean
    */
   public abstract void setName(String s);

   /**
    * @ejb.interface-method
    * @ejb.persistence
    * @return the value of this bean
    */
   public abstract String getValue();

   /**
    * @ejb.interface-method
    * @param s Set the value of this bean
    */
   public abstract void setValue(String s);

   /**
    * @ejb.interface-method
    *
    * @ejb.relation name="Parent-to-Children"
    *               role-name="Parent-has-many-Children"
    */
   public abstract Collection getChildren();

   /**
    * @ejb.interface-method
    */
   public abstract void setChildren(Collection c);
}

The second bean is another very simple Entity Bean. It just stores a test variable and a reference to its parent bean.

package com.dzrealms.example.entity;

import javax.ejb.CreateException;
import javax.ejb.EntityBean;

/**
  * @ejb.bean jndi-name="example/remote/ExampleChildBean"
  *           local-jndi-name="example/local/ExampleChildBean"
  *           cmp-version="2.x"
  *           primkey-field="ID"
  *           schema="ExampleChild"
  *
  * @ejb.persistence table-name="exampleChild"
  *
  * @ejb.interface 
  *   remote-class="com.dzrealms.example.entity.ExampleChildRemote"
  *   local-class="com.dzrealms.example.entity.ExampleChildLocal"
  *
  * @ejb.home 
  *   remote-class="com.dzrealms.example.entity.ExampleChildHomeRemote"
  *   local-class="com.dzrealms.example.entity.ExampleChildHomeLocal"
  *
  * @jboss.entity-command name="mysql-get-generated-keys"
  *
  * @jboss.unknown-pk class="java.lang.Integer"
  *                   auto-increment="true"
  */
public abstract class ExampleChildBean implements EntityBean {

   /**
    * @ejb.create-method
    * @return Primary Key
    * @throws javax.ejb.CreateException
    */
   public Integer ejbCreate() throws CreateException {
     return null;
   }

   public void ejbPostCreate() {

   }

   /**
    * @ejb.interface-method
    * @ejb.persistence
    * @return Primary Key
    */
   public abstract Integer getID();

   /**
    * @param i Set the primary key
    */
   public abstract void setID(Integer i);

   /**
    * @ejb.interface-method
    * @ejb.persistence
    * @return the test variable
    */
   public abstract String getTestVariable();

   /**
    * @ejb.interface-method
    * @param s sets the test variable
    */
   public abstract void setTestVariable(String s);

   /**
    * @ejb.interface-method
    *
    * @ejb.relation name="Parent-to-Children"
    *               role-name="Child-has-one-parent"
    *               target-multiple="true"
    *
    * @jboss.relation fk-column="parentID"
    *                 related-pk-field="ID"
    */
   public abstract ExampleLocal getParent();

   /**
    * @ejb.interface-method
    */
   public abstract void setParent(ExampleLocal el);
}

The final bean is a stateful session bean. It is also very simple and straightforward, but more than sufficient for illustrating the testing framework.

package com.dzrealms.example.session;

import com.dzrealms.example.entity.ExampleHomeLocal;
import com.dzrealms.example.entity.ExampleLocal;
import com.dzrealms.example.util.ExampleUtil;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.naming.NamingException;

/**
  * @ejb.bean jndi-name="example/remote/ExampleSession"
  *           local-jndi-name="example/local/ExampleSession"
  *
  * @ejb.interface
  *   remote-class="com.dzrealms.example.entity.ExampleSessionRemote"
  *   local-class="com.dzrealms.example.entity.ExampleSessionLocal"
  *
  * @ejb.home
  *   remote-class="com.dzrealms.example.entity.ExampleSessionRemoteHome"
  *   local-class="com.dzrealms.example.entity.ExampleSessionLocalHome"
  */
public abstract class ExampleSessionBean implements SessionBean {
   private ExampleHomeLocal exampleHome;

   /**
    * @ejb.create-method
    * @throws CreateException
    */
   public void ejbCreate() throws CreateException {
     try {
       exampleHome = ExampleUtil.getLocalHome();
     }  catch (NamingException e) {
       throw new CreateException(e.getMessage());
     }
   }

   /**
    * @ejb.interface-method
    */
   public ExampleLocal retrieveParentByName(String name) 
     throws FinderException {
     return exampleHome.findByName(name);
   }
}
  • + Share This
  • 🔖 Save To Your Account