Tải bản đầy đủ - 0 (trang)
Chapter 21. Testing and Debugging EJBs in WSAD

Chapter 21. Testing and Debugging EJBs in WSAD

Tải bản đầy đủ - 0trang

publicCollection

findAll(){

Collectionemployees=
class="docEmphStrong">null;
class="docEmphStrong">try{



UnitOfWork.begin();{



EmployeeMappermapper=

(EmployeeMapper)MapperRegistry.getMapper(

Employee.
class="docEmphStrong">class);employees=

mapper.findAll();

}UnitOfWork.getCurrent().commit();

}catch

(MappingExceptione){

e.printStackTrace();

}



return

employees;}

publicEmployee

getEmployee(Stringid){

Employeeemployee=
class="docEmphStrong">null;
class="docEmphStrong">try{

UnitOfWork.begin();{

EmployeeMappermapper=

(EmployeeMapper)MapperRegistry.getMapper(

Employee.
class="docEmphStrong">class);employee=

(Employee)mapper.findByPrimaryKey(id);}

UnitOfWork.getCurrent().commit();

}catch

(MappingExceptione){

e.printStackTrace();

}



return

employee;}

publicvoid

update(Mapargs){

try{

UnitOfWork.begin();{

EmployeeMappermapper=

(EmployeeMapper)MapperRegistry.getMapper(

Employee.
class="docEmphStrong">class);Employeeemp

=(Employee)mapper.findByPrimaryKey(

getParameter(args,"id"));

emp.setName(getParameter(args,"name"));

emp.setAge(Integer.parseInt(

getParameter(args,"age")));

emp.getAddress().setStreet(

getParameter(args,"street"));



emp.getAddress().setCity(

getParameter(args,"city"));

emp.getAddress().setState(

getParameter(args,"state"));

emp.getAddress().setZip(

getParameter(args,"zip"));

}UnitOfWork.getCurrent().commit();

}catch

(MappingExceptione){

e.printStackTrace();

}

}

publicvoid

createNewEmployee(Mapargs){

try{

UnitOfWork.begin();{



EmployeeMappermapper=

(EmployeeMapper)MapperRegistry.getMapper(

Employee.
class="docEmphStrong">class);Employeeemp

=new

Employee(

getParameter(args,"id"));

emp.setName(getParameter(args,"name"));

emp.setAge(Integer.parseInt(

getParameter(args,"age")));

new

Address(emp);emp.getAddress().setStreet(

getParameter(args,"street"));

emp.getAddress().setCity(

getParameter(args,"city"));

emp.getAddress().setState(

getParameter(args,"state"));



emp.getAddress().setZip(

getParameter(args,"zip"));

}UnitOfWork.getCurrent().commit();

}catch

(MappingExceptione){

e.printStackTrace();

}

}

publicvoid

deleteAnEmployee(Stringid){

try{

UnitOfWork.begin();{

EmployeeMappermapper=

(EmployeeMapper)MapperRegistry.getMapper(

Employee.
class="docEmphStrong">class);Employeeemp



=(Employee)mapper.findByPrimaryKey(id);

emp.delete();

}UnitOfWork.getCurrent().commit();

}catch

(MappingExceptione){

e.printStackTrace();

}

}

publicString

getParameter(Mapmap,Stringid){

Stringvalue=
class="docEmphStrong">null;String[]values=

(String[])map.get(id);
class="docEmphStrong">if(values!=
class="docEmphStrong">null&&values.length

>0){

value=values[0];

}



returnvalue;}

package

com.wsbook.acme.management;
class="docEmphStrong">import

java.io.IOException;



publicclass

BrowseEmployees
class="docEmphStrong">extendsHttpServlet{

publicvoid

doGet(HttpServletRequestreq,HttpServletResponse

resp)throws

ServletException,IOException{

try{

InitialContextcontext

=newInitialContext();
class="docEmphStrong">EmployeeManagementLocalHome

emh=

(EmployeeManagementLocalHome)

context.lookup(
class="docEmphStrong">"java:comp/env/ejb




class="docEmphStrong">EmployeeManagementLocal");


class="docEmphStrong">EmployeeManagementLocal

em=emh.create();
class="docEmphStrong">Collectionemployees=

em.findAll();req.setAttribute("employees",

employees);RequestDispatcherrd=

getServletContext().getRequestDispatcher(

"/browseEmployees.jsp");

rd.forward(req,resp);

}catch

(Exceptione){

RequestDispatcherrd=

getServletContext().getRequestDispatcher(

"/remoteAccessError.html");

rd.forward(req,resp);

}

}



}

Thisisonlyslightlydifferentthantheimplementationin

Chapter13thatwentdirectlytothedomainmodel.The

servletlooksuptheEmployeeManagementLocalHomeand

usesittocreateaninstanceofthe

EmployeeManagementLocalinterfacewhichitthenuses

toaddthecollectionofemployeestotheservletrequest

sessiondata.TheJSPsfromChapter13hardlyhaveto

changeatallotherthanofcoursetousethenew

com.wsbook.acme.Employeedomainmodel.Theother

servletsandJSPshavesimilarchanges.

Nowthatthecodingisdoneforourenterprise

application,thenextthingtodoistotestthe

modifications.



21.2OverviewoftheTestingProcess

BeforeproceedingwithtestingyourEJBswe'llneedtocoversomeofthe

built-intestingtoolsinWebSphereStudioApplicationDeveloper(WSAD).

JustaswedevelopedEJBsbyfollowingastep-by-stepapproachinthe

previoussection,we'lluseasimilarapproachtodeploy,test,anddebug

EJBs.Inshort,thenecessarystepsare:

1. GeneratetheEJBDeploymentclassesintheEJBprojectsused

intheEAR

CreateandconfigureaServerinstanceandaddtheEARproject

PublishandstarttheServerInstance

TestanEJBwiththeUniversalTestClientandtheclientWebmodule

DebugtheEJBcode(ifnecessary)usingtheWSADdebugger

PublishtheenterpriseapplicationtoanexternalWebSphereserver

Debugthepublishedapplicationusingthedistributeddebugger

SostartupWSADandworkthrougheachofthesesteps.



21.2.1Step1:GeneratetheEJBDeployment

ClassesforanEJBProject

Beforewebeginthisstep,we'llneedtoreviewsomebasicEJBconcepts

inorderforyoutounderstandwhat'shappening.RememberthatanEJB

isasoftwarecomponent.Intheprevioussections,we'vebeen

concentratingondevelopingtheuser-developedpartofentityand



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 21. Testing and Debugging EJBs in WSAD

Tải bản đầy đủ ngay(0 tr)

×