TS 68 EJB31 Validation
"EJB 3.1 Validation" Test Specification for "NetBeans 6.8"
Author: "Martin Schovanek"
Version: "1.0"
Last update: "Aug 24, 2009"
Introduction: "In this TS you will develop, deploy, and run a simple J2EE application named ConverterApp. The purpose of ConverterApp is to calculate currency conversions between yen and eurodollars. ConverterApp consists of an enterprise bean, which performs the calculations, and a web client."
Comments: ""
Contents |
Test suite: ConverterApp
Purpose: "purpose description"
Setup: "setup description"
- "Create the J2EE Application"
- "In the IDE, choose File‚ÜíNew Project (Ctrl-Shift-N)."
- "From the Enterprise category, select Enterprise Application and click Next."
- "Name the project ConverterApp, specify a location for the project and click Next."
- "Select the GFv3 server and set the Java EE Version to: Java EE 6 and click Finish."
- EXPECTED RESULT: "The wizard creates three projects: one for the enterprise application, one for the EJB module, and one for the web module."
- "Create the ConverterBean Enterprise bean"
- "In the Projects window, right-click the ConverterApp-ejb node and choose New > Session Bean."
- "In the EJB Name field, type: Converter. In the Package field, type: converter. Set the bean to be stateless and click Finish."
- EXPECTED RESULT: "The IDE creates ConverterBean.java class. The class is annotated by javax.ejb.Stateless."
- "Add Business Methods"
??? remove the next 3 steps?
- "Expand the Enterprise Beans node, right-click the ConverterBean node and choose: Add > Business Method."
- "In the dialog box, type: dollarToYen in the Name filed and java.math.BigDecimal in the Return Type field. In the parameters tab, click Add to add a BigDecimal parameter named dollars. Then click OK to add the business method."
- "Repeat the steps 1 and 2 to add a business method called: yenToEuro that returns BigDecimal and has one parameter named yen."
- "In ConverterBean.java, add the following field declarations right below the class declaration:"
BigDecimal yenRate = new BigDecimal("94.3188"); BigDecimal euroRate = new BigDecimal("0.0075");
- "In ConverterBean.java, implement the dollarToYen and yenToEuro methods as follows:"
public BigDecimal dollarToYen(BigDecimal dollars) { BigDecimal result = dollars.multiply(yenRate); return result.setScale(2, BigDecimal.ROUND_UP); } public BigDecimal yenToEuro(BigDecimal yen) { BigDecimal result = yen.multiply(euroRate); return result.setScale(2, BigDecimal.ROUND_UP); }
- {{result|EXPECTED RESULT: "The full source code should look like follows: "
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package converter; import java.math.BigDecimal; import javax.ejb.Stateless; /** * * @author tester */ @Stateless public class ConverterBean { BigDecimal yenRate = new BigDecimal("121.6000"); BigDecimal euroRate = new BigDecimal("0.0077"); public BigDecimal dollarToYen(BigDecimal dollars) { BigDecimal result = dollars.multiply(yenRate); return result.setScale(2, BigDecimal.ROUND_UP); } public BigDecimal yenToEuro(BigDecimal yen) { BigDecimal result = yen.multiply(euroRate); return result.setScale(2, BigDecimal.ROUND_UP); } }
- "Create the Web Client"
- "In the Projects window, right-click the ConverterApp-web node and choose: New > Servlet."
- "Name the servlet ConverterServlet and place it in a package called: converter. Click Next and then Finish."
- "In ConverterBean.java, add the following field declarations right below
the class declaration:"
@EJB ConverterBean converter;
- "In the Source Editor, go to the processRequest method and remove the comment symbols that comment out the text between PrintWriter out = response.getWriter(); and out.close();."
- "Add the following code in the body of the servlet, right before the: out.println("</body>");:"
out.println("<h1><b><center>Converter</center></b></h1>"); out.println("<hr>"); out.println("<p>Enter an amount to convert:</p>"); out.println("<form method=\"get\">"); out.println("<input type=\"text\" name=\"amount\" size=\"25\">"); out.println("<br>"); out.println("<p>"); out.println("<input type=\"submit\" value=\"Submit\">"); out.println("<input type=\"reset\" value=\"Reset\">"); out.println("</form>"); String amount = request.getParameter("amount"); if (amount != null && amount.length() > 0) { try { java.math.BigDecimal d = new java.math.BigDecimal(amount); out.println("<p>"); out.println("<p>"); out.println(amount + " Dollars are " + converter.dollarToYen(d) + " Yen."); out.println("<p>"); out.println(amount + " Yen are " + converter.yenToEuro(d) + " Euro."); } catch (Exception e) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Cannot execute ConverterBean:\n", e); } }
- "Right click the Editor pane and choose Format"
- EXPECTED RESULT: "The IDE creates the ConverterServlet.java class. The class is annotated by javax.servlet.anntotion.WebServlet"
- "Runnig the Web Client"
- "In the Projects window, right-click the ConverterApp project node and choose Properties > Run."
- "Type /ConverterServlet in the Relative URL field and click OK."
- "In the Projects window, right-click the ConverterApp node and choose Run Project."
- EXPECTED RESULT: "The IDE does all of the following:"
- "Starts the application server if it is not already started."
- "Builds the ConverterApp project and the projects for each of its modules. You can view the build ouputs in the Files window."
- "Deploys converterapp.ear to the application server."
- "Opens your default web browser at the following URL: http://localhost:8080/ConverterApp-war/ConverterServlet"
"After entering 100 in the input field and clicking Submit, you should see the output like:"
100 Dollars are 9431.88 Yen. 100 Yen are 0.75 Euro.