Java Tutorial - Java Scipt : Building the Test Client

Java Tutorial - Java Scipt :

Building the Test Client


We will add a new method to our test client to test the new session bean, as shown in the following code:
public void userSessionTest(
{
UserSessionRemote remote;
UserSessionHome home;
System.out.println(“userSessionTest:”);
try
{
System.out.println( “Getting Home Interface”);
home = (UserSessionHome)getHomeInterface(
“ejb/UserSession”,
com.oldfriends.session.UserSessionHome.class);
System.out.println( “Getting Remote Interface”);
remote = home.create();
System.out.println(remote.toString());
System.out.println( “Calling getHello”);
System.out.println(“Hello:”+remote.getHello());
System.out.println( “Retrieving Records”);
System.out.println( “getUserByUserid”);
UserDTO udata =
remote.getUserByUserid(“0000000-000000-00000000-0-00000000-1”);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
System.out.println( “getUserByLogin”);
udata = remote.getUserByLogin(“jsmith”);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
System.out.println( “Update User”);
udata.setFirstName(“NotJack”);
remote.updateUser(udata);
udata = remote.getUserByLogin(“jsmith”);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
System.out.println( “Add User”);
udata.setFirstName(“One”);
udata.setLastName(“LastName”);
udata.setMi(“Z”);
udata.setMaidenName(“”);
udata.setLoginId(“lgid”);
udata.setPassword(“somepwd”);
udata.setEmail(“email@foo.bar”);
udata = remote.addUser(udata);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
remote.remove();
}
catch( Exception ex )
{
System.err.println(
“userSessionTest: Caught an unexpected exception!”);
ex.printStackTrace();
}
finally
{
remote = null;
home = null;
System.out.println( “userSessionTest: Finally”);
}
}
The new userSessionTest method is similar to the userEntityTest method covered earlier, except that it uses the UserDTO object to move user data between the client the EJB container. We demonstrate the session bean methods for retrieving users, updating users, and adding a user to the system. We also need to make a change to the main method so that our new method is executed. The new main method follows:
public static void main(String[] args)
{
UserClient uc = new UserClient();
uc.userEntityTest();
uc.userSessionTest();
}
We can execute this from Ant without any further changes. Use the following command to run this by using Ant:
ant run-test The output will be similar to the output previously observed for the entity  bean under the Testing the Bean section.
.