TS 70 JavaEE Sanity

Java EE Sanity Test Specification

Author: Martin Schovanek, Jiri Skrivanek
Version: NetBeans 7.0
Last update: 8-th April 2011
Introduction: Purpose of this test specification is to try various technologies and go through whole development cycle. It can be used for Ant based and Maven projects. If there are differencies the test case is divided into two tracks.
Comments: Based on http://download.oracle.com/javaee/6/firstcup/doc/.

Contents


Test suite: Create Java EE 6 Application

Purpose: Create web applications with web services.
Setup: Install GlassFish server version 3.1 and register it in "Tools|Servers" in IDE.

  1. Creating Web Service project
Ant Project Maven Project
  1. Create a new Java Web project, project name: dukes-age, server: GlassFish 3, version: Java EE 6.
  2. Set the context path to: /DukesAgeService
  3. Click Finish.
  4. Delete index.jsp file.
  1. Create a new Maven Web Application project, project name: dukes-age.
  2. Right click project node and select Properties.
  3. Choose Run category.
  4. Select GlassFish server as target application server and set the context path to /DukesAgeService.
  5. Click OK.
  • EXPECTED RESULT: You should now see the project you created in Projects pane.
  1. Create DukesAgeResource class.
    1. Select "File > New File > Web Services > RESTful Web Services from Patterns".
    2. Click Next.
    3. Choose "Simple Root Resource" design pattern and click Next.
    4. Set package: firstcup.webservice, path: dukesAge; class name: DukesAgeResource, mime-type: text/plain.
    5. Click Finish.
    6. Confirm "REST Resource Configuration" dialog with default values ("NetBeans will generate...", Resources Path /resources).
    • EXPECTED RESULT: You should see the DukesAgeResource.java class opened in the Editor.
  2. Implement the DukesAgeResource class
    1. Remove the putText generated method.
    2. Add the following to the getText method body:
        // Create a new Calendar for Duke's birthday
        Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);
        // Create a new Calendar for today
        Calendar now = Calendar.getInstance();

        // Subtract today's year from Duke's birth year, 1995
        int dukesAge = now.get(Calendar.YEAR) - dukesBirthday.get(Calendar.YEAR);
        dukesBirthday.add(Calendar.YEAR, dukesAge);

        // If today's date is before May 23, subtract a year from Duke's age
        if (now.before(dukesBirthday)) {
            dukesAge--;
        }
        // Return a String representation of Duke's age
        return Integer.toString(dukesAge);
    1. Reformat the class (Alt+Shift+F), fix imports (Ctrl+Shift+I) and save it.
    • EXPECTED RESULT: There is not any visible error in editor.
  1. Configure, Build and deploy the Web Service Endpoint
    1. Right-click on dukes-age in the Projects tab and select Properties.
    2. Set "Run > Relative URL" to /resources/dukesAge.
    3. Click OK.
    4. Right click dukes-age and select Run.
    • EXPECTED RESULT: The dukes-age successfully deploys to the GlassFish server, web browser loads the URL of the DukesAgeResource path, and you will see the returned String representing Duke's age.
  2. Create the Web Application project
Ant Project Maven Project
  1. Create a new Java Web project, project name: firstcup, server: GlassFish 3, version Java EE 6, context path: /firstcup"
  2. Under Frameworks select JavaServer Faces and set Servlet URL Pattern to: /firstcupWeb/*.
  3. Click Finish.
  4. Delete generated pages.
  1. Create a new Maven Web Application project, project name: firstcup.
  2. Delete generated web pages.
  3. Set the project as main project.
  4. Right click project node and select Properties.
  5. Choose Run category.
  6. Select GlassFish server as target application server.
  7. Choose Frameworks category.
  8. Click Add Button and select "JavaServer Faces" in the "Add Framework" dialog.
  9. In tab Configuration set Servlet URL Pattern to: /firstcupWeb/*.
  10. Click OK.
  • EXPECTED RESULT: You should now see the project you created in Projects pane.
  1. Create the DukesBirthdayBean Enterprise Bean
    1. Select "File > New File > Enterprise JavaBeans > Session Bean".
    2. Set EJB name: DukesBirthdayBean, package: firstcup.ejb, session type: Stateless
    3. Directly after class declaration, paste in following code:
    private static final Logger LOGGER =
            Logger.getLogger("firstcup.ejb.DukesBirthdayBean");

    public int getAgeDifference(Date date) {
        int ageDifference;

        Calendar theirBirthday = new GregorianCalendar();
        Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);

        // Set the Calendar object to the passed in Date
        theirBirthday.setTime(date);

        // Subtract the user's age from Duke's age
        ageDifference = dukesBirthday.get(Calendar.YEAR)
                - theirBirthday.get(Calendar.YEAR);
        LOGGER.log(Level.INFO, "Raw ageDifference is: {0}", ageDifference);
        // Check to see if Duke's birthday occurs before the user's. If so, 
        // subtract one from the age difference
        if (dukesBirthday.before(theirBirthday) && (ageDifference > 0)) {
            ageDifference--;
        }
        LOGGER.log(Level.INFO, "Final ageDifference is: {0}", ageDifference);

        return ageDifference;
    }
  • Reformat the code and Fix Imports (choose: java.util.logging.Logger, java.util.Date).
  • Save the file.
  • EXPECTED RESULT: There is not any visible error.
  1. Create a Resource bundle
Ant Project Maven Project
  1. Select "File > New File > Other > Properties File".
  2. Set file name: WebMessages, folder: src/java/firstcup/web.
  3. Click Finish
  1. Select "firstcup Web App|Other Sources|src/main/resources"
  2. From main menu select "File > New File > Other > Properties File".
  3. Set file name: WebMessages, folder: src/main/resources/firstcup/web.
  4. Click Finish
  • Copy the following messages to the file:
Welcome=Hi. My name is Duke. Let us find out who is older -- You or I. 
DukeIs=Duke is
YearsOldToday=years old today.
Instructions=Enter your birthday and click submit.
YourBD=Your birthday
Pattern=MM/dd/yyyy
DateError=Please enter the date in the form MM/dd/yyyy.
YouAre=You are 
Year=year
Years=years
Older=older than Duke!
Younger=younger than Duke!
SameAge= the same age as Duke!
Submit=Submit
Back=Back
  • Save the file.
  • To add the Spanish translation right-click WebMessages.properties node and choose "Add|Locale".
  • In the "New Locale" dialog type "es" in "Language Code" field and click OK.
  • Copy the WebMessages into WebMessages_es and add 'SP:' prefix to each message.
  • EXPECTED RESULT: You see two new properties files in the project.
  1. Configure the Resource Bundles
    1. Open "File > New File > JavaServer Faces > JSF Faces Configuration"
    2. Click Finish and in editor click XML to see raw code.
    3. Add an <application> tag with the following:
    <application>
        <resource-bundle>
            <base-name>firstcup.web.WebMessages</base-name>
            <var>bundle</var>
        </resource-bundle>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>es</supported-locale>
        </locale-config>
    </application>
    1. Save the file.
    • EXPECTED RESULT: You specified the default and supported locale.
  1. Create the DukesBDay Managed Bean
    1. Select the firstcup project node.
    2. Choose "File > New File > JavaServer Faces > JSF Managed Bean".
    3. Set Class Name: DukesBDay, Package: firstcup.web, Scope: session.
    4. Click Finish.
    5. Directly after the class declaration add the DukesBirthdayBean reference, private variables and default constructor:
    @EJB
    private DukesBirthdayBean dukesBirthday;
    private int age;
    private Date yourBD;
    private int ageDiff;
    private int absAgeDiff;
    private static final Logger LOGGER = Logger.getLogger("firstcup.web.DukesBDay");

    public DukesBDay() {
        age = -1;
        yourBD = null;
        ageDiff = -1;
        absAgeDiff = -1;
    }
    1. Reformat the class.
    2. Fix imports, choose java.util.Date and java.util.logging.Logger classes.
    3. Right-click in the editor window.
    4. Select "Refactor > Encapsulate Fields" from the popup window.
    5. In the "Encapsulate Fields" dialog, select the "Create Getter" and "Create Setter" check boxes for age, yourBD, ageDiff and absAgeDiff.
    6. Click Refactor.
    7. Add the following code to the getAge method:
        // Use the java.net.* APIs to access the Duke's Age RESTful web service
        HttpURLConnection connection = null;
        BufferedReader rd = null;
        StringBuilder sb = null;
        String line = null;
        URL serverAddress = null;

        try {
            serverAddress = new URL(
                    "http://localhost:8080/DukesAgeService/resources/dukesAge");
            connection = (HttpURLConnection) serverAddress.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setReadTimeout(10000);

            // Make the connection to Duke's Age
            connection.connect();

            // Read in the response
            rd = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            sb = new StringBuilder();
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }

            // Convert the response to an int
            age = Integer.parseInt(sb.toString());
        } catch (MalformedURLException e) {
            LOGGER.log(Level.WARNING, "A MalformedURLException occurred.", e);
        } catch (ProtocolException e) {
            LOGGER.log(Level.WARNING, "A ProtocolException occurred.", e);
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "An IOException occurred", e);
        }
        return age;
    1. Reformat the class.
    2. Fix Imports, choose the java.net.HttpURLConnection, java.net.ProtocolException, java.net.URL.
    3. Inside the getAgeDiff method, directly before the return statement, add the following code:
        setAgeDiff(dukesBirthday.getAgeDifference(yourBD));
        setAbsAgeDiff(Math.abs(ageDiff));
    1. Reformat the class.
    2. Save it.
    • EXPECTED RESULT: The DukesBDay have bean created.
  1. Modify the web.xml
    1. Open "firstcup > Web Pages > WEB-INF > web.xml".
    2. Go to pages pane and modify Welcome Files to: firstcupWeb/greeting.xhtml
    3. Save the file.
    • EXPECTED RESULT: The web.xml have been successfully modified.
  2. Create the inputDate Composite Component
    1. Select "firstcup > Web Pages" node.
    2. Select "File > New File > JavaServe Faces > JSF Composite Component" and click Next.
    3. Enter inputDate under File Name, resources/components under Folder and click Finish.
    4. In the Newly created inputDate.xhtml replace the content with the following:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:cc="http://java.sun.com/jsf/composite"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <!-- INTERFACE -->
    <cc:interface>
        <cc:attribute name="date" />
    </cc:interface>

    <!-- IMPLEMENTATION -->
    <cc:implementation>
        <h:inputText value="#{cc.attrs.date}">
            <f:convertDateTime pattern="MM/dd/yyyy" />
        </h:inputText>
    </cc:implementation>

</html>
    1. Reformat the page and save it.
    • EXPECTED RESULT: The inputDate.xhtml component have been created.
  1. Create the greeting.xhtml page
    1. Select the "firstcup > Web Pages" node.
    2. Choose "File > New File > JavaServer Faces > JSF Page".
    3. Enter greeting under File Name and click Finish.
    4. Replace the greeting.xhtml file with the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:fc="http://java.sun.com/jsf/composite/components">
    <head>
        <title>Firstcup Greeting Page</title>
    </head>
    <body>
        Hello
        <h:form>
            <h2>
                <h:outputText value="#{bundle.Welcome}"/>
            </h2>
            <h:outputText value="#{bundle.DukeIs} "/>
            <h:outputText value="#{dukesBDay.age} #{bundle.YearsOldToday}"/>
            <p/>
            <h:outputText value="#{bundle.Instructions}"/>
            <p/>
            <h:outputText value="#{bundle.YourBD} "/>
            <fc:inputDate id="userBirthday" date="#{dukesBDay.yourBD}" />
            <h:outputText value=" #{bundle.Pattern}"/>
            <p/>
            <h:commandButton value="#{bundle.Submit}" action="response"/>
            <p/>
            <h:message for="userBirthday" style="color:red"/>
        </h:form>
    </body>
</html>
    1. Reformat it and save it.
    2. Create a new JSF Page called: response.
    3. Replace the content of response.xhtml with the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:h="http://java.sun.com/jsf/html">
    <head>
        <title>Response Page</title>
    </head>
    <body>
        <h:form>
            <h:outputText value="#{bundle.YouAre} "/>
            <h:outputText value="#{bundle.SameAge}"
                          rendered="#{dukesBDay.ageDiff == 0}"/>
            <h:outputText value="#{dukesBDay.absAgeDiff}"
                          rendered="#{dukesBDay.ageDiff lt 0}"/>
            <h:outputText value=" #{bundle.Year} "
                          rendered="#{dukesBDay.ageDiff == -1}"/>
            <h:outputText value=" #{bundle.Years} "
                          rendered="#{dukesBDay.ageDiff lt -1}"/>
            <h:outputText value="#{bundle.Younger}"
                          rendered="#{dukesBDay.ageDiff lt 0}"/>
            <h:outputText value="#{dukesBDay.absAgeDiff}"
                          rendered="#{dukesBDay.ageDiff gt 0}"/>
            <h:outputText value=" #{bundle.Year} "
                          rendered="#{dukesBDay.ageDiff == 1}"/>
            <h:outputText value=" #{bundle.Years} "
                          rendered="#{dukesBDay.ageDiff gt 1}"/>
            <h:outputText value="#{bundle.Older}" rendered="#{dukesBDay.ageDiff gt 0}"/>    <p/>
            <h:commandButton id="back" value="#{bundle.Back}" action="greeting"/>
        </h:form>
    </body>
</html>
    1. Reformat it and save it.
    • EXPECTED RESULT: The greeting.xhtml and response.xhtml pages have been created.
  1. Run the firstcup application
    1. Right-click the firstcup node in the Projects tab and choose Run.
    2. Enter 01/01/2008 into the Your Birthday text field and click Submit.
    • EXPECTED RESULT: response.xhtml page should display following message:
You are 13 years younger than Duke!


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo