STS 74 JavaEE

Java EE Sanity Test Specification for NetBeans 7.4

Author: Štěpán Žebra
Version: 1.0
Last update: 29.7.2013
Introduction: Purpose of this test specification is to try various technologies and go through the whole development cycle. It can be used for Ant based and Maven projects. If there are differences the test case is divided into two tracks.
Comments: This document is heavily based on TS_72_JavaEE_Sanity which is now obsolete. Test scenarios are based on http://docs.oracle.com/javaee/7/firstcup/doc/home.htm.

Contents


Test suite: Create Java EE 7 Application

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

  1. Create Web Service project
Ant Project Maven Project
  1. Create a new "Java Web / Web Application" project:
    • "Project Name": dukes-age
    • "Server": GlassFish 4
    • "Java EE Version": Java EE 7 Web
  2. After the project is created, delete the index.html file.
  1. Create a new "Maven / Web Application" project:
    • "Project Name": dukes-age
    • "Server": GlassFish 4
    • "Java EE Version": Java EE 7 Web
  2. After the project is created, delete the index.html file.
  • EXPECTED RESULT: Newly created project is visible in the "Projects" view.
  1. Create Web Service class
    1. Select the project root node in "Projects" view and push the New File... toolbar button.
    2. Select "Web Services / RESTful Web Services from Patterns" and push Next >.
    3. Choose "Simple Root Resource" design pattern and push Next >.
    4. Set:
      • "Resource Package": firstcup.webservice
      • "Path": dukesAge
      • "Class Name": DukesAgeResource
      • "MIME Type": text/plain
    5. Push the Finish button.
    • EXPECTED RESULT: DukesAgeResource.java and ApplicationConfig.java classes are created and opened in the editor.
  2. Implement Web Service class
    1. In the DukesAgeResource.java, remove the putText generated method.
    2. Replace the body of the getText generated method with:
      // 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);
    3. Reformat the class (Alt+Shift+F), fix imports (Ctrl+Shift+I) and save it.
    • EXPECTED RESULT: There isn't any visible error in the editor.
  3. Configure, Build and Deploy the Web Service Endpoint
    1. In "Projects" view and invoke pop-up menu on the "dukes-age" root node and select Properties.
    2. Select the "Run" category in the "Project Properties" manager.
    3. Set "Context Path" to: /DukesAgeService.
    4. Set "Relative URL" to: /webresources/dukesAge.
    5. Push the OK button.
    6. Run the "dukes-age" project.
    • EXPECTED RESULT: The dukes-age successfully deploys to the GlassFish server, web browser loads the URL of the DukesAgeResource path, a String representing Duke's age is returned.
  4. Create the Web Application project
Ant Project Maven Project
  1. Create a new "Java Web / Web Application" project:
    • "Project Name": firstcup
    • "Server": GlassFish 4
    • "Java EE Version": Java EE 7 Web
    • "Context Path": /firstcup
  2. In "Frameworks" step of the wizard, tick "JavaServer Faces" and in "Configuration" tab, set "JSF Servlet URL Pattern" to: /firstcupWeb/*.
  3. After the project is created, delete the index.xhtml file.
  1. Create a new "Maven / Web Application" project:
    • "Project Name": firstcup
    • "Server": GlassFish 4
    • "Java EE Version": Java EE 7 Web
    • "Context Path": /firstcup
  2. After the project is created, invoke pop-up menu on it's root node and select Properties.
  3. Select the "Frameworks" category in the "Project Properties" manager.
  4. Push the Add... button and choose "JavaServer Faces" framework.
  5. In "Configuration" tab, set "JSF Servlet URL Pattern" to: /firstcupWeb/* and push OK.
  6. Delete generated web pages.
  • EXPECTED RESULT: Newly created project is visible in the "Projects" view.
  1. Create the DukesBirthdayBean Enterprise Bean
    1. Invoke File | New File... main menu action. "New File" wizard shows up.
    2. Select "Enterprise JavaBeans" category and "Session Bean" file type and push Next >.
    3. Set:
      • "EJB name": DukesBirthdayBean
      • "Package": firstcup.ejb
      • "Session type": "Stateless" option
    4. Push the Finish button.
    5. Replace the body of the generated class with:
      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;
      }
    6. Reformat the class, fix imports (choose: java.util.logging.Logger, java.util.Date) and save it.
    • EXPECTED RESULT: There isn't any visible error in the editor.
  2. Create a Resource Bundle
Ant Project Maven Project
  1. Invoke File | New File... main menu action. "New File" wizard shows up.
  2. Select "Other" category and "Properties File" file type and push Next >.
  3. Set:
    • "File Name": WebMessages
    • "Folder": src/java/firstcup/web
  4. Push the Finish button.
  1. In the "Projects" view, select the "src/main/resources" node under the "Other Sources" node.
  2. Push the New File... toolbar button. "New File" wizard shows up.
  3. Select "Other" category and "Properties File" file type and push Next >.
  4. Set:
    • "File Name": WebMessages
    • "Folder": src/main/resources/firstcup/web
  5. Push the Finish button.
  • EXPECTED RESULT: The newly created "WebMessages.properties" file is visible in the "Projects" view.
  1. Add Translations
    1. Copy the following messages to the properties 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
    2. Save the properties file.
    3. To add a Spanish translation invoke pop-up menu on the "WebMessages.properties" node and select Add | Locale action.
    4. In the "New Locale" dialog, type es in "Language Code" field and push OK.
    5. Add SP: prefix to each message in "WebMessages_es.properties".
    • EXPECTED RESULT: Two properties files are visible in the "Projects" view.
  2. Configure the Resource Bundles
    1. Invoke File | New File... main menu action. "New File" wizard shows up.
    2. Select "JavaServer Faces" category and "JSF Faces Configuration" file type and push Next >.
    3. Keep the default values and push Finish.
    4. In the newly created file, 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>
    5. Save the file.
    • EXPECTED RESULT: You specified the default and supported locale.
  3. Create the DukesBDay Managed Bean
    1. Invoke File | New File... main menu action. "New File" wizard shows up.
    2. Select "JavaServer Faces" category and "JSF Managed Bean" file type and push Next >.
    3. Set:
      • "Class Name": DukesBDay
      • "Package": firstcup.web
      • "Scope": session
    4. Push the Finish button.
    5. Replace the body of the generated class with:
      @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;
      }
    6. Reformat the class, fix imports (choose: java.util.logging.Logger, java.util.Date) and save it.
    7. Invoke Refactor | Encapsulate Fields main menu action.
    8. In the "Encapsulate Fields" dialog, tick the "Create Getter" and "Create Setter" checkboxes for fields "age", "yourBD", "ageDiff" and "absAgeDiff".
    9. Keep other values default and push Refactor.
    10. Add the following code to the getAge() method, directly before the return statement:
      // Use the java.net.* APIs to access the Duke's Age RESTful web service
      HttpURLConnection connection;
      BufferedReader rd;
      StringBuilder sb;
      String line;
      URL serverAddress;
      
      try {
          serverAddress = new URL(
              "http://localhost:8080/DukesAgeService/webresources/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);
      }
    11. Reformat the class, fix imports (choose: java.net.HttpURLConnection, java.net.ProtocolException, java.net.URL) and save it.
    12. In the getAgeDiff() method, directly before the return statement, add the following code:
      setAgeDiff(dukesBirthday.getAgeDifference(yourBD));
      setAbsAgeDiff(Math.abs(ageDiff));
    13. Reformat the class and save it.
    • EXPECTED RESULT: The DukesBDay class have bean created. There are no code errors.
  4. Modify the web.xml
    1. Open the "web.xml" file under the "Web Pages > WEB-INF" node.
    2. Go to "Pages" tab and modify "Welcome Files" to: firstcupWeb/greeting.xhtml
    3. Invoke File | Save main menu action to save the file.
    • EXPECTED RESULT: The "web.xml" have been successfully modified.
  5. Create the inputDate Composite Component
    1. In the "Projects" view, select the "Web Pages" node.
    2. Push the New File... toolbar button. "New File" wizard shows up.
    3. Select "JavaServer Faces" category and "JSF Composite Component" file type and push Next >.
    4. Set:
      • "File Name": inputDate
      • "Folder": resources/components
    5. Push the Finish button.
    6. Replace the contents of "inputDate.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"
          xmlns:cc="http://xmlns.jcp.org/jsf/composite"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/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>
    7. Reformat the page and save it.
    • EXPECTED RESULT: The "inputDate.xhtml" component have been created.
  6. Create the greeting.xhtml page
    1. In the "Projects" view, select the "Web Pages" node.
    2. Push the New File... toolbar button. "New File" wizard shows up.
    3. Select "JavaServer Faces" category and "JSF Page" file type and push Next >.
    4. Set greeting "File Name" push Finish.
    5. Replace the contents of "greeting.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:f="http://xmlns.jcp.org/jsf/core"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:fc="http://xmlns.jcp.org/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>
    6. Reformat the page and save it.
    • EXPECTED RESULT: The "greeting.xhtml" page have been created.
  7. Create the response.xhtml page
    1. In the "Projects" view, select the "Web Pages" node.
    2. Push the New File... toolbar button. "New File" wizard shows up.
    3. Select "JavaServer Faces" category and "JSF Page" file type and push Next >.
    4. Set response "File Name" push Finish.
    5. Replace the contents 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://xmlns.jcp.org/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>
    6. Reformat the page and save it.
    • EXPECTED RESULT: The "response.xhtml" page have been created.
  8. Run the firstcup application
    1. Run the "firstcup" project.
    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