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"

  1. "Create the J2EE Application"
    1. "In the IDE, choose File‚ÜíNew Project (Ctrl-Shift-N)."
    2. "From the Enterprise category, select Enterprise Application and click Next."
    3. "Name the project ConverterApp, specify a location for the project and click Next."
    4. "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."
  2. "Create the ConverterBean Enterprise bean"
    1. "In the Projects window, right-click the ConverterApp-ejb node and choose New > Session Bean."
    2. "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."
  3. "Add Business Methods"

??? remove the next 3 steps?

    1. "Expand the Enterprise Beans node, right-click the ConverterBean node and choose: Add > Business Method."
    2. "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."
    3. "Repeat the steps 1 and 2 to add a business method called: yenToEuro that returns BigDecimal and has one parameter named yen."
    4. "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");
    1. "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);
    }
}
  1. "Create the Web Client"
    1. "In the Projects window, right-click the ConverterApp-web node and choose: New > Servlet."
    2. "Name the servlet ConverterServlet and place it in a package called: converter. Click Next and then Finish."
    3. "In ConverterBean.java, add the following field declarations right below

the class declaration:"

@EJB
ConverterBean converter;
    1. "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();."
    2. "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);
}
}
    1. "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"
  1. "Runnig the Web Client"
    1. "In the Projects window, right-click the ConverterApp project node and choose Properties > Run."
    2. "Type /ConverterServlet in the Relative URL field and click OK."
    3. "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.

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