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

Difference between revisions of "WTP JEE5 Test Scenarios"

(SIMPLE WEB APPLICATION USE CASE)
(SIMPLE SESSION EJB 3.0/JPA 1.0)
Line 83: Line 83:
 
     @Entity
 
     @Entity
 
     public class HelloWorld implements Serializable{
 
     public class HelloWorld implements Serializable{
private static final long serialVersionUID = 1L;
+
        private static final long serialVersionUID = 1L;
@Id
+
        @Id
private String id;
+
        private String id;
private String message;
+
        private String message;
private String language;
+
        private String language;
public HelloWorld() {
+
        public HelloWorld() {
}
+
        }
 
         public String getId() {
 
         public String getId() {
        ...
+
          ...
 
* Open your persistence.xml (create one if it does not exists in….) file, and make sure that it looks like:
 
* Open your persistence.xml (create one if it does not exists in….) file, and make sure that it looks like:
 
         <persistence version="1.0"
 
         <persistence version="1.0"

Revision as of 10:29, 30 March 2007

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.

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

  • 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.

  • Create a new Dynamic Web Project named HelloWorldWeb, select Tomcat 6 as the target runtime
  WtpJEE5UC1DynamicWebProj.gif
  • 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
  • CURRENT I-BUILD NO WEB:XML CREATED. CAN ADD ONE MANUALLY
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"
     metadata-complete="true">

     <display-name>MyJEE5Web</display-name>
     <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
    </web-app>
  • 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>
  • Right click test.jsp, Choose Run As: Run on Server, select Tomcat 6 as the server
  • Tomcat 6 starts and results are correctly displayed (using current code in CVS, 3/28)

COMBINED SCENARIO

The second scenario is a test of 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.

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

  • 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.
  • Create a new EJB 3.0 Project, with JPA 1.0 facet named HelloWorldEJB, select Glassfish as the target runtime.
  • Make sure that HelloWorldEJB is added to HelloWorldEAR.
  • Use the data tools to create a connection to the new Derby database named myderbydb.
  • 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`)
   );
  • Add a few rows of data such as
   1 Hello World! en
   2 Merhaba Dünya tr
  • 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() {
         ...
  • 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>


  • Refer to Application Server manuals to create a new DataSource to the Derby database with the JNDI name “jdbc/MyDataSource”.
  • 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 HelloWorl h where h.language = :lang ");
            query.setParameter("lang",language);
            List<HelloWorld> list = query.getResultList();
            if (list.size() == 0)
              return list.get(0);
            return null;
          }
        }
  • Add the interfaces as usual:
          public interface HelloBeanFacade {
            public HelloWorld hello(String language);
          }
  • You are done with creating an EJB 3.0 Module. Next you will create a client Web application.

JSF WEB Client for EJB

  • 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.
  • 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.
  • Go to module dependencies and add a dependency to HelloWorldEJB from HelloWorldWeb
  • 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 ...
              }
           }
        }
  • 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>
          ... ...
  • 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>
  • 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