TS 68 JavaEE Sanity
Revision as of 12:54, 19 April 2010 by Mschovanek (Talk | contribs)
"Java EE Sanity" Test Specification for "NetBeans 6.8"
Author: "Martin Schovanek"
Version: "beta"
Last update: "08/05/2009"
Introduction: "Java EE Validtion Test Specification"
Comments: "Based on http://java.sun.com/javaee/6/docs/firstcup/doc/"
Contents |
Test suite: "Create Java EE 6 Application"
Purpose: "purpose description"
Setup: "setup description"
- "Creating Web Service project"
- "Create a new Java Web project, project name: dukes-age, server: GF v3, version: Java EE 6"
- "Set the context paht to: /DukeAgeService ."
- "Click Finish."
- "Delete index.jsp file."
- EXPECTED RESULT: "You should now see the project you created in Projects pane."
- "Create DukeAgeResource class."
- "Select File > New File > Web Services > RESTful Web Services from Patterns"
- "Clic Next"
- "Choose 'Simple Root Resource' design pattern and click Next"
- "Set package: firstcup.webservice, path: dukesAge; class name: DukesAgeResource, mime-type: text/plain"
- "Click Finish"
- EXPECTED RESULT: "You should see the DukeAgeResource.java class opened in the Editor."
- "Implement the DukeAgeResource class"
- "Highlight the putText generated method and their JavaDoc and delete it."
- "Highlight the current getText method body and replace it with following implementation:"
// 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);
- "Reformat the class, fix imports and save it"
- EXPECTED RESULT: "there is not any visible error"
- "Configure, Build and deploy the Web Servicce Endpoint"
- "Right-click on dukes-age in the Projects tab and select Properties > Libraries."
- "Uncheck the boxe under Compile-time Libraries for 'retlib_gfv3ee6' library."
- "Set 'Run > Relative URL' to /resources/dukesAge."
- "Click OK."
- "Right click dukes-age and select Run"
- EXPECTED RESULT: "The dukes-age sucessfully deploys to the GF v3 server, web browser load the URL of the DukesAgeResource path, and you'll see the returned String representing Duke's age."
- "Create the Web Application project"
- "Create a new Java Web project, project name: firstcup, server: GF v3, version Java EE 6, context path: /firstcup"
- "Under Framewroks select select Java Server Faces and set Servlet URL Patter to: /firstcupWeb/*"
- "Click Finish"
- "Delete the WelcomeJSF.jsp generated file."
- EXPECTED RESULT: "You should now see the project you created in Projects pane."
- "Create the DukesBirthdayBean Enterprise Bean"
- "Select File > New File > Java EE > Session Bean"
- "Set ejb name: DukesBirthdayBean, package: firstcup.ejb, session type: Stateless"
- "Directly after class declaration, paste in following code:"
private static Logger logger = Logger.getLogger("com.sun.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.info("Raw ageDifference is: " + 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.info("Final ageDifference is: " + 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"
- "Creata a Resource bundle"
- "Select File > New File > Other > Properties File"
- "Set file name: WebMessages, folder: src/java/firstcup/web"
- "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 copy the WebMessages into WebMessages_es and add 'SP:' prefix to each message"
- EXPECTED RESULT: "You see two new properties files in the project."
- "Configure the Resource Bundles"
- "Open 'Configuration Files > faces0config.xml'"
- "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>
- "Save the file."
- EXPECTED RESULT: "You specified the default and supported locale."
- "Create the DukesBDay Managed Bean"
- "Select the firstcup module node"
- "Choose File > New File > JavaServer Faces > JSF Managed Bean"
- "Set {Class Name: DukesBDay, Package: firstcup.web, Scope: session}"
- "Click Finish"
- "Directly after the class declaration add the DukesBirthdayBean reference: "
@EJB private DukesBirthdayBean dukesBirthday;
- "After the dukesBirthday variable declaration, add the following private variables: "
private int age; private Date yourBD; private int ageDiff; private int absAgeDiff; private static Logger logger = Logger.getLogger("firstcup.web.DukesBDay");
- "Initialize the variables by adding a default constructor: "
public DukesBDay() { age = -1; yourBD = null; ageDiff = -1; absAgeDiff = -1; }
- "Reformat the class."
- "Fix imports, choose java.util.Date and java.util.logging.Logger classes."
- "Right-click in the editor window."
- "Select Refactor > Encapsulate Fields from the popup window."
- "In the Encapsulate Fields dialog, select the Create Getter and Create Setter checkboxes for age, yourBD, ageDiff and absAgeDiff."
- "Click Refactor."
- "Add the following code to the getAge method: "
public int getAge() { // 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; }
- "Reformat the class."
- "Fix Imports, choose the java.net.HttpURLConnection, java.net.ProtocolException, java.net.URL"
- "Inside the getAgeDiff method, directly before the return statement, add the foloowing code: "
ageDiff = dukesBirthday.getAgeDifference(yourBD); setAbsAgeDiff(Math.abs(ageDiff));
- "Reformat the class."
- "Save it."
- EXPECTED RESULT: "The DukesBDay have bean created."
- "Modify the web.xml"
- "Open firstcup > Configuration FIles > web.xml"
- "Expand Context Parameters and delete them all."
- "Modify Welcome Files to: firstcupWeb/greeting.xhml"
- "Save the file."
- EXPECTED RESULT: "The web.xml have been sucessfully modified"
- "Create the inputDate Composite Component"
- "Select firstcup > Web Pages node"
- "Choose: File > New File > Other > Folder"
- "Enter: resurces/components uder Folder Name and click Finsh."
- "Select the resources.components folder"
- "Select File > New File > JavaServe Faces > JSF Page and click Next"
- "Enter inputDate under File Name and click Finish"
- "In the Newly creatd inputDate.xhtml replace the <html> tag with the following: "
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" xmlns:composite="http://java.sun.com/jsf/composite" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <body> <composite:interface> <composite:attribute name="date" required="true" /> </composite:interface> <composite:implementation> <h:inputText value="#{cc.attrs.date}"> <f:convertDateTime pattern="MM/dd/yyyy" /> </h:inputText> </composite:implementation> </body> </html>
- "Reformat the page and save it."
- EXPECTED RESULT: "The inputDate.xhtml component have been created."
- "Create the greeting.xhtml page"
- "Select the firstcup > Web Pages node."
- "Choose File > New File > JavaServer Faces > JSF Page"
- "Enter greeting under File Name and click Finish."
- "Repeat the previous steps and create a new JSF Page called: response"
- "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> <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>}
- "Reformat it and save it."
- "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:f="http://java.sun.com/jsf/core" 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>
- "Reformat it and save it."
- EXPECTED RESULT: "the greeting.xhtml and response.xhtml pages have been created."
- "Run the firstcup application"
- "Right-click the firstcup node in the Projects tab and choose Run."
- "Enter 01/01/2008 into the Your Birthday text fileld and click Submit."
- EXPECTED RESULT: "response.xhtml page should display following message: "
You are 13 years younger than Duke!