Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

WTP JEE5 Test Scenarios

WTP 2.0 JEE 5 Use Cases

These scenarios are created to test the basic functionality that we would expect from WTP 2.0 to support JEE5. Please note that NO wizards are used other than the ones needed to create the servers, projects and add the necessary facets to these projects. The idea is if we are provided with valid JEE5 module and components (such as EJBs, JPA, Servlets, JSPs, etc), WTP 2.0 should support it. This includes Web, EJB, and EAR and Run As> Run on Server support. Of course this involves basic models for deployment descriptors (sans Annotation injections). Annotation support will only include that provided by JDT.

If you have questions, comments, etc., about these use cases, send a note to the WTP Dev List or contact me, Naci Dai.

Test Plan

As we get close to releasing 2.0, we will report on the status of these use-cases with each I-Build. After an I-Build is declared (Typically on Thursdays), we will run the following use-cases using this build and report any problems via bugzilla. To facilitate easy reporting and searching of these bugs, we will use an indexing key called JEE5-UC in Status Whiteboard. You can use a search like JEE5-UC to list them. Use cases and steps are numbered to make it easy to refer to them.

Please try these use cases or similar ones yourselves and help us improve the quality of WTP tools for 2.0 release.

Prereqs

To try these use cases you will need the latest build of WTP, Apache Tomcat version 6, JDK 1.5 and up, JSF v1.1 Reference Implementation or Apache MyFaces1.1.5, TOPLink Essentials, and Apache Derby. Follow their instructions to install these software. Additionally, EJB/Ear use cases require Apache Geronimo, JBoss v5.0, Glassfish or a similar JEE 5 compatible application server. WTP comes with a server adapter for JBoss v5.0, you can obtain the latest Geronimo adapters for WTP 2.0 from Geronimo Server Adapters, for other servers you will need a WTP server adapter that supports JEE5 features. There is an example glassfish server adapter that me and John Lanuti have created during an eclipsecon 2007 tutorial.

UC-1(a) SIMPLE WEB APPLICATION USE CASE

This use case is the most basic Web application scenario that involves creating the Web module, targeting it to a JEE5 runtime, and testing it using Run As> Run on Server

  • Step 1 - Go to Installed Runtimes and add Tomcat 6 as a runtime.
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
    that has JEE 5  capability such as JBoss, IBM WAS, BEA WebLogic Server, 
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe 
    some delays in providing server adapters for all.

  • Step 2 - Create a new Dynamic Web Project named HelloWorldWeb, select Tomcat 6 as the target runtime
  WtpJEE5UC1DynamicWebProj.gif
  • Step 3 - Select Dynamic Web Module 2.5, and Java 5 as facets, click next to accept defaults. A new Web 2.5 project is created.
  WTPJEE5UC1Facets.gif
  • Step 4 - Click Next. In the next wizard page, you have the option to specify context root and content directories, and option to create a Deployment Descriptor.
UC1-GenerateDeploymentDescriptor.png

Make sure that Generate Deployment Descriptor is checked. WTP will create the web.xml file WebContents/Web-Inf folder. web.xml should look like the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"
     metadata-complete="true">
    </web-app>
  • Step 5 - Add a new JSP file called test.jsp with the following content:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       
      "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
       Hello World!
    </body>
    </html>
  • Step 6 - Right click test.jsp, Choose Run As: Run on Server, select Tomcat 6 as the server
  • Step 7 - Tomcat 6 starts and results are correctly displayed (using current code in CVS, 3/28)

UC-1(b) ADD A SERVLET TO SIMPLE WEB APPLICATION USE CASE

This use case adds a servlet to the simple Web application scenario (previos use-case)and testing it using Run As> Run on Server

  • Step 1 - Use File->new to choose the Servlet Wizard. Complete the wizard pages to create a Servlet class in a package named demo with the class named HelloWorldServlet. The servlet should be the subclass of HttpServlet. Click next to proceed to the next pages and give the name helloworld to the servlet, /helloworld as the URL mapping and select check the box to create the doGet method.

UC1b-CreateNewServlet.png

  • Step 2 - The wizard will create the Servlet class and add the required entries to the web.xml file.
   <?xml version="1.0" encoding="UTF-8"?>
   <web-app id="WebApp_ID" version="2.5"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
       <servlet>
           <servlet-name>helloworld</servlet-name>
           <servlet-class>demo.HelloWorldServlet</servlet-class>
       </servlet>
       <servlet-mapping>
           <servlet-name>helloworld</servlet-name>
           <url-pattern>/helloworld</url-pattern>
       </servlet-mapping>
   </web-app>
  • Step 2 - Edit the Servlet class so that the code looks like the following:
   public class HelloWorldServlet extends HttpServlet {
       @Override
       public void doGet(HttpServletRequest req, HttpServletResponse resp)
           throws ServletException, IOException {
           resp.getWriter().print("Hello World");	
       }
   }
  • Step 4 - Tomcat 6 starts and results are correctly displayed

UC-1(c) SIMPLE EJB3 APPLICATION USE CASE

This use case is the most basic EJB3 application scenario that involves creating the EJB3 module, targeting it to a JEE5 runtime, and testing it.

  • Step 1 - Go to Installed Runtimes and add JBoss v5.0 as a runtime.
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
    that has JEE 5  capability such as JBoss v5, IBM WAS, BEA WebLogic Server, 
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe 
    some delays in providing server adapters for all.
   UC1c-CreateNewRuntime-1.png
   UC1c-CreateNewRuntime-2.png
  • Step 2 - Create a new EJB Project named HelloWorldEJB, select JBoss v5.0 as the target runtime
  UC1c-NewEJB3Project.png
  • Step 3 - Select EJB Module 3.0, and Java 5 as facets, click next to accept defaults.
  UC1c-NewEJB3Facets.gif
  • Step 4 - Click Next. In the next wizard page, you have the option to specify source directories, and option to create a Deployment Descriptor.
UC1c-GenerateDeploymentDescriptor.png

Make sure that Generate Deployment Descriptor is unchecked. WTP will create the new EJB Project.

UC1c-NewEJBProject.png
  • Step 5 - Now we can add a new Stateless EJB. First start with the Interface. Create a new interfaces using the New Interface wizard. Place it in the demo package and name it HelloWorld.

UC1c-NewInterface.png

Edit the interface and make sure that the code looks like the following. When you save you will have an error because we did not create the HelloWorldMessage class yet.

   public interface HelloWorld {
       public HelloWorldMessage hello(String language);
   }

Next we will create the HelloWorldMessage class, which can say HelloWorld in many different languages!

  • Step 5 - Create a new class using the New Class wizard. Place it in the demo package and name it HelloWorldMessage. Edit the class and make sure that the code looks like the following. It will have three attributes named id, language and message, getter and setter methods, a toString method that prints the message and constructors. When you save your previos error messages should dissappear.
   public class HelloWorldMessage implements Serializable {
       private static final long serialVersionUID = 1L;
       //attributes
       private Long id;
       private String language;
       private String message;
       public HelloWorldMessage() {
           super();
       }
       public HelloWorldMessage(Long id, String language, String message) {
           super();
           this.id = id;
           this.language = language;
           this.message = message;
       }
       public Long getId()...
       public void setId(Long id)...
       public String getLanguage()...
       public void setLanguage(String language)...
       public String getMessage()...
       public void setMessage(String message)...
       @Override
       public String toString() {	
               return message;
       }
   }
  • Step 6 - It is time to create the Stateless EJB that implements the HelloWorld interface. Create a new class using the New Class wizard. Place it in the demo package and name it HelloWorldBean.

UC1c-NewStatelessBean.png

Edit the class and make sure that the code looks like the following. It will have two annotations @Stateless and @Remote and implements the hello message defined in the interface.

   @Stateless(name="HelloWorldStateless")
   @Remote(HelloWorld.class)
   public class HelloWorldBean implements HelloWorld {
       public HelloWorldMessage hello(String language) {
           // Return a hard coded message for now
           return new HelloWorldMessage(0L,"en","Hello World!");
       }
   }

That is it for the EJB3 development. You can now create a new Server for the JBoss and deploy our EJB.

  • Step 6 - Choose the HelloWorldEJB and choose Run On>Run On Server, and create new new server for JBoss v5.0.

UC1c-NewServer.png

  • Step 7 - Add the EJB3 project to the new server.

UC1c-AddProjectToServer.png

  • Step 8 - Click finish. WTP will deploy the EJB jar to JBoss and start it. When JBoss start you should see a message like the following that confirms that EJB has been deployed to the server succesfully.
   17:50:46,875 INFO  [Catalina] Server startup in 203 ms
   17:50:59,671 INFO  [MCKernelAbstraction] installing bean: jboss.j2ee:jar=HelloWorldEJB.jar,name=HelloWorldStateless,service=EJB3 with dependencies:
   17:51:02,890 INFO  [EJBContainer] STARTED EJB: demo.HelloWorldBean ejbName: HelloWorldStateless


  • Step 9 - In the next use case we will extend the simple Web application to be a client to the EJB.


UC-1(d) EJB3 WEB CLIENT USE CASE

This use case is a simple Web client to the previos EJB3 application scenario that involves creating a dependency to the EJB3 module, targeting it to a JEE5 runtime, and testing it.

   JBoss v5.0 BUG: If you are using JBoss v5.0 fo testing this use case.  There is a JBoss v5.0 bug that prevents WTP 
   from redeploying an EJB jar, ear or a war file to JBoss (see http://jira.jboss.com/jira/browse/JBAS-4310).  As a 
   workaround stop the JBoss v5.0 server before you start this use case.
  • Step 1 - Go to HelloWorldWeb project and open its properties window. Click on the J2EE dependencies item on the list to choose HelloWorldEJB as a module that our Web application will depend on. By doing this we can access the types defined in the EJB module from the Web application and make remote calls to the stateless EJB.
   UC1d-ModuleDependency.png
  • Step 2 - Edit the test.jsp JSP to make a call to our EJB. JNDI naming for remote and local EJB interfaces is not prtable among application servers so you will have to identify how it is implemented. For JBoss the suffix /remote is appended to the end of the JNDI name for the EJB. So we will use HelloWorldStateless/remote. Make sure your JSP looks like the following:
   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       
       "http://www.w3.org/TR/html4/loose.dtd">
   <%@page import="javax.naming.InitialContext"%>
   <%@page import="demo.HelloWorld"%>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Hello World</title>
   </head>
   <body>
   <%
       InitialContext ctx = new InitialContext();
       HelloWorld message =  (HelloWorld)ctx.lookup("HelloWorldStateless/remote");
   %>
   <%= message.hello("en")%>
   </body>
   </html>
  • Step 3 - Make sure that your Web project is also targeted to JBoss. To check this go to project properties window and check the JBoss v5.0 as one of the targeted runtimes.
  UC1d-TargetedServers.png
  • Step 3 - Select test.jsp file. Right click to choose Run On>Run On Server to test your code. WTP should deploy both Web and EJB modules to the JBoss v5.0 server, start it and display the Hello World message that comes from the EJB using the internal browser.
  File:UC1d-StartingServer.png

UC-2 Web Application with using JSF 1.1 and JPA 1.0

This use case is and adaptation of the Dali Tutorial. This use case is about building and deploying dynamic web application for an order-entry system using JPA 1.0 and JSF 1.1. The complete application is packaged as a web application.


EJB/EAR SCENARIOS

The second set of scenarios are for EJB 3.0 and EAR 5. Since EJBs are meaningless without a client, this use case involves creating an EJB 3.0 stateless session bean, that uses JPA 1.0 to map a simple class. A web application using JSF is the EJB client. Whole application is packaged as an EAR.

UC-3 SIMPLE SESSION EJB 3.0/JPA 1.0

This use case is the most creates a simple POJO mapped using JPA 1.0. A stateless session references the entity manager to query the DB. The EJB jar is packaged in an EAR module

  • Step 1 - Go to Installed Runtimes and add Glassfish as a runtime.
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
    that has JEE 5  capability such as JBoss, IBM WAS, BEA WebLogic Server, 
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe 
    some delays in providing server adapters for all.
  • Step 2 - Create a new EJB 3.0 Project, with JPA 1.0 facet named HelloWorldEJB, select Glassfish as the target runtime.
  • Step 3 - Make sure that HelloWorldEJB is added to HelloWorldEAR.
  • Step 4 - Use the data tools to create a connection to the new Derby database named myderbydb. (Note: Can the data tools steps be clarified for those unfamiliar w/ Dali and Derby? Or can we provide a link to a pre-configured Derby DB?)
  • Step 5 - Create a connection to it and create a new table using the following script:
   CREATE TABLE HELLOWORLD (
     `ID` varchar(10) NOT NULL,
     `MESSAGE` varchar(50) NOT NULL,
     `LANGUAGE` varchar(50) NOT NULL,
      PRIMARY KEY  (`ID`)
   );
  • Step 6 - Add a few rows of data such as
   1 Hello World! en
   2 Merhaba Dünya tr
  • Step 7 - Create a package called demo and add the following class which is JPA mapped
   package demo;
   import java.io.Serializable;
   @Entity
   public class HelloWorld implements Serializable{
       private static final long serialVersionUID = 1L;	
       @Id
       private String id;
       private String message;
       private String language;	
       public HelloWorld() {
       }
       public String getId() {
         ...

(Note: We had to add an @Table(schema="APP", name="HELLOWORLD") in order to get this working.)

  • Step 8 - Open your persistence.xml (create one if it does not exists in….) file, and make sure that it looks like:
       <persistence version="1.0"
        xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
          http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
        <persistence-unit name="HelloWorldPU">
         <jta-data-source>jdbc/MyDataSource</jta-data-source>
         <class>demo.HelloWorld</class>
        </persistence-unit>
       </persistence>


  • Step 9 - Refer to Application Server manuals to create a new DataSource to the Derby database with the JNDI name “jdbc/MyDataSource”.

(Note: We had to bring up the Admin Console for Glassfish by pointing to http://localhost:4848 and entering userid: admin password: adminadmin)

  • Step 10 - Create a package called demo.session and create a class named HelloWorldSession, and make sure that it looks like the following:
       @Stateless(name="HelloWorldStateless")
       public class HelloWorldSession 
          implements HelloBeanFacade
        {
          @PersistenceUnit(name = "HelloWorldPU")
          EntityManagerFactory emf;
          public HelloWorldSession() {
          }
          public HelloWorld hello(String language) {
            Query query = emf.createEntityManager().
              createQuery("select h from HelloWorld h where h.language = :lang ");
            query.setParameter("lang",language);
            List<HelloWorld> list = query.getResultList();
            if (list.size() == 0)
              return list.get(0);
            return null;
          }
        }
  • Step 11 - Add the interfaces as usual:
          public interface HelloBeanFacade {
            public HelloWorld hello(String language);
          }
  • Step 12 - You are done with creating an EJB 3.0 Module. Next you will create a client Web application.

JSF WEB Client for EJB

  • Step 12 - Create a new Dynamic Web Project (Web 2.5, Java 5, JSF 1.1) named HelloWorldWeb, select GlassFish as the target runtime. Make sure that HelloWorldWeb is also added to the HelloWorldEAR.
  • Step 13 - Select Dynamic Web Module 2.5, and Java 5, JSF 1.1 as facets, click next to accept defaults. A new Web 2.5 project is created.
  • Step 14 - Go to module dependencies and add a dependency to HelloWorldEJB from HelloWorldWeb
  • Step 15 - Add a JSF backing bean called HelloWorldBean as follows:
        public class HelloWorldBean 
            implements Serializable {
          String language= “en”;
          HelloBeanFacade helloBean;
          public String hello(){
              if(helloBean == null)
                init();
              return helloBean.hello(getLanguage);
          }
          public void  setLanguage(..)
          public String getLanguage(..)
          private void init() {
              try {
                //JSF BEANS DO NOT SUPPORT INJECTION WITH
                // ANNOTATIONS so use Naming Context
                InitialContext ctx = new InitialContext();
                helloBean = (HelloBeanFacade) 
                ctx.lookup("HelloWorldStateless");      
              } catch (Exception e) {
                // Handle exception ...
              }
           }
        }
  • Step 16 - Add the following managed bean to the faces-config.xml
        <faces-config>
          <managed-bean>
            <description>hello world</description>
            <managed-bean-name>helloBean</managed-bean-name>
            <managed-bean-class>
              demo.web.HelloWorldBean
            </managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
          </managed-bean>
          ... ...
  • Step 17 - Create a new file named hello.jsp. JSP file should look like:
        <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
        <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
        <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Hello World</title>
        <head>
        <body>
        <f:view>
          <h:form> 
                <h:outputText value="#{helloBean.hello}"/>
                <h:selectOneMenu onchange="submit()"  
                  value="#{helloBean.language}" immediate="true">
                     <f:selectItem itemLabel="en" itemValue="en"/>
                     <f:selectItem itemLabel="tr" itemValue="tr"/>
                </h:selectOneMenu>
           </h:form>
        </f:view>
        </body>
        </html>
  • Step 18 - Select hello.jsp, Run As > Run On Server, the ear should be packaged and deployed with EJB and WAR, page should display the message.

Back to the top