Java Tutorial - Java Scipt : Testing the Bean

Java Tutorial - Java Scipt :

Testing the Bean


Now that we have built and deployed our bean, we want to test it. This was the reason for providing home and remote interfaces for the entity bean. To test the bean, we will create a very small command line-based client. An EJB client in general needs to:

1. Get an InitialContext
2. Use the context to lookup the home interface
3. Get a remote interface from the home interface
4. Execute the methods on the remote interface

The code for the test client follows:

import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import javax.ejb.DuplicateKeyException;
import com.oldfriends.entity.UserRemoteHome;
import com.oldfriends.entity.UserRemote;
import com.oldfriends.user.UserDTO;
import java.lang.ClassCastException;
import javax.ejb.EJBHome;
import javax.naming.NamingException;
public class TestUserEJB
{
private EJBHome getHomeInterface(
String jndiName, Class theHomeClass )
throws NamingException, ClassCastException
{
Context myctx = null;
EJBHome home = null;
try{
myctx = new InitialContext();
Object obj = myctx.lookup(jndiName);
home = (EJBHome)PortableRemoteObject.narrow( obj, theHomeClass );
}
catch( Exception e )
{
e.printStackTrace();
}
return home;
}
public void userEntityTest()
{
try
{
System.out.println( “Getting Home Interface”);
UserRemoteHome home = (UserRemoteHome)
getHomeInterface( “ejb/UserEJB”,
com.oldfriends.entity.UserRemoteHome.class);
System.out.println( “\nRetrieving Records”);
System.out.println( “\nFindByPrimaryKey”);
UserRemote ur =
home.findByPrimaryKey(“0000000-000000-00000000-0-00000000-1”);
System.out.println( “Name:”+
ur.getFirstName() + “ “ +
ur.getLastName()+” “+ur.getUserid());
System.out.println( “\nFindByEmail”);
ur = home.findByEmail(“janedoe@someaddr.org”);
System.out.println( “Name:”+
ur.getFirstName() + “ “ +
ur.getLastName()+” “+ur.getUserid());
System.out.println(“\nFindByLoginId”);
ur = home.findByLoginId(“jsmith”);
System.out.println( “Name:”+
ur.getFirstName() + “ “ +
ur.getLastName()+” “+ur.getUserid());
System.out.println( “\nCreating Record”);
ur = home.create(
“first”,
“last”,
“x”,
“”,
2003,
“login”,
“email”,
“password”);
System.out.println( “\nNew user=”+ur.getUserid());
System.out.print( “\nDone “);
}
catch (Exception ex)
{
System.err.println(“Caught an unexpected exception!”);
ex.printStackTrace();
}
finally
{
System.out.println( “Finally”);
}
}
public static void main(String[] args)
{
UserClient uc = new UserClient();
uc.userEntityTest();
}
}

The private getHomeInterface method is used to get the InitialContext and perform the lookup and type conversion for retrieving the home interface from JNDI. The rest of the program performs various lookups on the test data
we inserted when we created the database and then adds a new record to the table. You can use Ant to run the program. The following Ant task configures the classpath and runs the program:

<target depends=”compile” name=”run-test”>
<java classname=”TestUserEJB” dir=”.” fork=”true”>
<classpath>
<pathelement location=”.”/>
<path refid=”oldfriends.classpath”/>
</classpath>
</java>
</target>

To execute the program using Ant, enter the following command on the command line:
ant run-test The output should look something like the text shown below:
Buildfile: build.xml init: compile: run-test:
[java] Getting Home Interface
[java] Retrieving Records
[java] FindByPrimaryKey
[java] Name:John Doe 0000000-000000-00000000-0-00000000-1
[java] FindByEmail
[java] Name:Jane Doe 0000000-000000-00000000-0-00000000-2
[java] FindByLoginId
[java] Name:Jack Smith 0000000-000000-00000000-0-00000000-3
[java] Creating Record
[java] New user=5c4o042-zbp0i6-ddzhbn2w-1-ddzip0ay-6
[java] Done Finally
BUILD SUCCESSFUL
Total time: 3 seconds
Notice that the new user has a GUID automatically generated for the primary key. Now that we know we have a working entity bean, we are ready to wrap it with a session bean. The session bean is what servlets and other clients will access to retrieve data from the database.