Sun Microsystems Logo

 

 
 

Sun[tm] ONE Studio 4, Enterprise Edition for Java[tm] with Application Server 7 Tutorial
Testing the Session Bean
PREV PREV NEXT

 

The IDE includes a facility to automatically generate a test environment for the Enterprise JavaBeans[tm]. This feature creates a web-based test client application and packages it along with the enterprise bean. You can use this test application to create an instance of the enterprise bean and to interact with it. In this section you will use this test facility to exercise the GreeterDB bean's create() and getGreeting() methods.

  1. Create Test Client
  2. Deploy the Test Client
  3. Run the Test Client


1. Create the Test Client

When you create a test client, the IDE generates an EJB[tm] module, supporting elements, and a J2EE[tm] application module.

To create a test client for the GreeterDB session bean:

1. Right-click the GreeterDB (EJB) node and select Create New EJB Test Application.

The EJB Test Application wizard is displayed.

2. Accept all default values and click the OK button.

A progress monitor appears briefly and then goes away when the process is complete.

Another window is displayed informing you that the web module that was created is also visible in the Project tab. It should go away automatically also. If not, click OK to close the window.

3. View the resulting test objects in the Explorer.

The IDE has created an EJB module named GreeterDB_EJBModule, a web module named GreeterDB_WebModule, and a J2EE application named GreeterDB_TestApp in the ejb subdirectory.

The EJB module contains the GreeterDB session bean. The web module contains a number of JavaServer Pages[tm] pages and Java class files that make up the test client. The J2EE application includes references to the EJB module and to the web module. In addition, the IDE mounts the web module.


Confusing list of icons? To avoid confusion amongst the various nodes representing your business application EJB and the automatically generated test application, you have the option during the creation of the test application to specify both the location of the test application and Java package names used in the test application.


Content of Test Application: The EJB module, web module, and J2EE application generated by the IDE are not intended to be modified. If you modify them you might not be able to redeploy the J2EE test application.


2. Deploy the Test Client

Before deploying the test application, ensure that the application server is running and it is set as the default server in the IDE.

To deploy the test application:

1. Right-click the GreeterDB_TestApp J2EE application node and select Deploy.

The progress of the deployment process is displayed in a dialog window.

See the new tabs in the IDE's Output window that display messages about the deployment.

2. Click the server1(localhost:port):server.log tab in the output window.

The server.log view displays any error conditions that prevent deployment. When the deployment was successful, the output window resembles the following:

3. On the IDE's toolbar, select the Editing tab.

The Explorer window should be displayed. If not, select View -> Explorer from the IDE's menu bar.

4. In the Explorer window, select the Runtime tab.

5. Expand the Server Registry, Installed Servers, Sun ONE Application Server 7, localhost:port, server1(localhost:port), and Deployed Applications nodes.

You should see the GreeterDB_TestApp node under the Deployed Applications node. If the GreeterDB_TestApp node is not listed, right-click on the Deployed Applications node and select Refresh List.

What happens during deployment? The IDE automatically assembles the EAR file and contacts the administrative server of the specified application server to carry out the deployment.

During the initial deployment of an application containing EJBs, the administrative server automatically generates the EJB's stubs and skeletons. Until the EJB interfaces are changed, subsequent redeployment of the application does not entail stub and skeleton generation. This smart redeployment feature greatly optimizes redeployment operations..


3. Run the Test Client

Since the test client is web-based, you need to open a web browser window to access the initial test page of the generated web application.

On the initial test client’s web page, you'll invoke the create() method of the GreeterDB bean’s home interface to create an instance of the bean. Then you'll execute the getGreeting()business method of the bean.

To test the GreeterDB bean:

1. Start a web browser and point it to the following URL:

http://localhost:port/GreeterDB_TestApp

Where port is the HTTP server port number specified during installation. The default HTTP server port number is 80, but it may be different based on the ports in use during installation.

Your browser displays the test client.

Forgot the HTTP Server's Port Number? If you do not remember the HTTP server port number of the application server instance, you can determine the appropriate value by looking at the properties of the application server instance.

1. Under the Runtime tab of Explorer, expand the Server Registry, Installed Servers, Sun ONE Application Server 7, localhost:port

2. Right click the server1(localhost:port) object and select Properties.

3. Read the HTTP server's port number from the Server Instance Port property.


From this page, you can invoke the methods of the GreeterDBHome remote interface.

Speed of first access: When you visit a JavaServer Pages[tm] (JSP[tm]) page for the first time, the JSP file is compiled into servlet source code and the servlet source code is compiled into a servlet class file. This one time compilation step accounts for the delay in displaying the test client page. After the JSP page has been compiled as part of the first access, subsequent requests are processed very quickly and the resulting page appears almost immediately. Force a reload of the page to see how quickly the subsequent next visit is handled. To force a reload, press the shift key and click the reload button on your web browser.

2. Click the Invoke button next to the create method to create an instance of the GreeterDB bean.

The create method is under the heading "Invoke Methods on samples.jdbc.simple.ejb.GreeterDBHome."

The web browser displays the new GreeterDB instance as shown below:

Notice that the samples.jdbc.simple.ejb.GreeterDB [7] item is added in the instances list in the upper left hand portion of the page. The item represents the instance of the GreeterDB bean created by invoking the create method on the GreeterDBHome interface.

3. Click the samples.jdbc.simple.ejb.GreeterDB [7] instance link.

This action selects the GreeterDB instance as the EJB module to test. The web browser displays the methods that can be invoked, including the getGreeting() business method.

4. Click the Invoke button next to the getGreeting method.

The getGreeting() method is called, and its return value is listed in the Results area:

In the example above, the web browser above shows the word "evening" as the return value. Because the getGreeting() method generates a greeting based on the current time, you might see a different greeting value.

5. In the main menu bar of the IDE, select the Running tab. In the Output window, click the server1:(localhost:port): access tab.

A display of the HTTP requests and responses processed by the application server is shown:

If you encountered problems while running your EJB, you might normally want to exercise the debugging facilities at this stage. However, for the purposes of this tutorial, we'll defer debugging exercises until we develop and deploy the combined web and EJB application.

Now that you've successfully tested your session bean, proceed to Packaging the Enterprise Bean in an EJB Module to prepare to include the EJB in an enterprise application.

PREV PREV NEXT