Tải bản đầy đủ - 0 (trang)
11 Testing the Session Façade Example with JUnit

11 Testing the Session Façade Example with JUnit

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

TimeSheetGroup.jar#TimeSheetActionProcessorFacade





ejb/ReportActionProcessorFacade

Session

com.wsbook.casestudy.ejb.ReportActionProcessorFacadeH

com.wsbook.casestudy.ejb.ReportActionProcessorFacade

TimeSheetGroup.jar#ReportActionProcessorFacade



/**

*ThisisanAllTestsclassthatrunsallofthetestsinthe*

com.wsbook.casestudy.tests.junit.ejbpackage.Theuseof

anAllTests*classineachpackageisacommonpractice

forregressiontesting.

*/

publicclass

AllEJBTests
class="docEmphStrong">extendsTestCase{



/**

*ConstructorforAllEJBTests.

*@paramarg0

*/

public

AllEJBTests(Stringarg0){

super(arg0);}

publicstatic

Testsuite(){

TestSuitesuite=
class="docEmphStrong">newTestSuite();

suite.addTest(
class="docEmphStrong">new

TestSuite(TimesheetActionProcessorTests.
class="docEmphStrong">class));

suite.addTest(
class="docEmphStrong">new

TestSuite(ReportActionProcessorTests.
class="docEmphStrong">class));
class="docEmphStrong">returnsuite;}



}

publicvoid

testAddFindDeleteProject(){

//testadding,findingandremovingaProjectfromthe

modelTimeSheetActionProcessorprocessor=

(TimeSheetActionProcessor)ActionProcessorFactory

.getActionProcessor("TimeSheet");

try{

Projectpurple=
class="docEmphStrong">newProject();

purple.setNumber("99");

purple.setName("ProjectPurple");

processor.addProject(purple);

Projectclone=processor.findProjectByKey("99");

assertEquals("Namesdon'tMatchforProject",

purple.getName(),

clone.getName());

processor.removeProject("99");



try{

processor.findProjectByKey("99");

fail("ShouldhaveraisedNoSuchProjectException");}

catch(Exception

e){

//Eatit--itshouldfail

}

}catch

(MappingExceptione){

fail("MappingExceptioncaught"+e);}

}




xmlns:xmi="http://www.omg.org/XMI"

xmlns:resources.mail="http:/




src="images/ccc.gif"alt="graphics/ccc.gif"

/>/www.ibm.com/websphere/appserver/schemas/5.0/resources.mail



xmlns:resources.jms="http:
height="9"align="left"src="images/ccc.gif"

alt="graphics/ccc.gif"

/>//www.ibm.com/websphere/appserver/schemas/5.0/resources.jms

xmlns:resources.jdbc="http
height="9"align="left"src="images/ccc.gif"

alt="graphics/ccc.gif"

/>://www.ibm.com/websphere/appserver/schemas/5.0/resources.jdb

xmlns:resources="http:/




src="images/ccc.gif"alt="graphics/ccc.gif"

/>/www.ibm.com/websphere/appserver/schemas/5.0/resources.xmi"


name="DefaultMailProvider"


src="images/ccc.gif"alt="graphics/ccc.gif"/>

description="IBMJavaMailImplementation">


protocol="smtp"type="TRANSPORT"/>


protocol="pop3"type="STORE"/>
xmi:id="ProtocolProvider_3"protocol="imap"

type="STORE"/>


name="MQJMSProvider"




src="images/ccc.gif"alt="graphics/ccc.gif"/>

description="Default-cannotbechanged"/>


name="WebSphereJMSProvider"


src="images/ccc.gif"alt="graphics/ccc.gif"/>

description="Default-cannotbechanged"/>


xmi:id="JDBCProvider_1"name="DefaultDB2JDBC

Provider"




src="images/ccc.gif"alt="graphics/ccc.gif"/>

description=""

implementationClassName="COM.ibm.db2.jdbc.DB2ConnectionP

C:\EJBTwoExperiments\db2java.zip


xmi:id="WAS40DataSource_1"


src="images/ccc.gif"alt="graphics/ccc.gif"/>

name="jdbc/WSBOOK"jndiName="jdbc/WSBOOK"

description=""databaseName="SAMPLE">






name="user"value="my_userid"/>
xmi:id="J2EEResourceProperty_2"name="password"


src="images/ccc.gif"alt="graphics/ccc.gif"/>

value="my_password"/>







First,you'llneedtoedittheelementofthe

filetopointtoacopyofdb2java.zip,whichistheDB2

JDBCdriverjarfile.Also,you'llneedtoeditthevalue

attributesforuseridandpasswordtobeavaliduserID

andpasswordonyourmachine.Notethatyouareusing

anunencryptedpasswordinthiscase;onlythe

ApplicationClientResourceConfigurationtoolcan

encryptpasswords.Whileyoucanusetheunencrypted

onesinWSADyoushouldplanonrunningtheACRCT

afteryoudeployyourEARifyouneedtorunthematthe

commandline.



30.12RunningtheTestClient

Onceyou'veeditedtheclient-resourcefile,you'realmostreadytorunthe

example.First,you'llneedtocreateanapplicationclientconfiguration.In

WSAD,selectRun>RunfromthemenubartoopentheLaunch

Configurationseditor.SelectWebSphereV5ApplicationClientinthe

LaunchConfigurationsTreeview.PresstheNewbuttonatthebottomof

thepagetocreateaconfiguration.

Changethename(atthetopofthepage)toTimeAppTestClient.Setthe

enterpriseapplicationtobewasbookbyselectingwasbookfromthepulldown.ThenswitchtotheArgumentstagandprovideaparametertothe

JUnitTestRunner.After-CCverbose=trueaddtheline

com.wsbook.casestudy.tests.junit.ejb.AllEJBTests.Thiswillensurethat

theAllEJBTestssubclassofTestCasewillrunwhenyourunthe

applicationclient.

There'sonemoreproblemtoresolve.Theissuehereisthatnomatter

whatyouputintheclasspathsectionoftheclient-resourcefileforDB2,

WSADstillwon'tfindyourdriver.(Ifyouwanttorunyourapplication

clientatthecommandlineagainstWebSphereorWSAD,thisclasspath

doesmatter!)Instead,tomakethisworkforWSADyouwillneedtoadd

thedb2java.zipJARfilewiththeDataSourceimplementationtothe

runtimeclasspathofyourapplicationclientintheLaunchConfigurations

Editor.Todothis,opentheLaunchConfigurationsEditor(witheitherRun

>RunorDebug>Debug)andselecttheTimeAppTestClient.Turnto

theClasspathtabandusetheAddExternalJarsbuttontoadd

db2java.zip(whereveritisstoredonyourmachine,usually

/SQLLIB/java/db2java.zip)whereisoften

C:\ProgramFilesinWindows.

Nowthatyou'veeditedyourclient-resourcefile,you'refinallyreadytorun

thetestcaseagainstourcasestudy.First,switchtoaserverperspective

andmakesurethattheWAS5.0serveryoucreatedinanearlierchapter

isrunning.Ifnot,startit.Then,switchtotheJ2EEperspectiveanduse

Run-->Runtobringupthelaunchconfigurationseditor.Selectthe



TimeAppTestClientandpresstheRunbutton.Ifeverythinggoesas

expected,youshouldseethefollowingoutputattheendofthe

WebSphereconsole(yourtimewillvary,ofcourse):

WSCL0900I:Initializingandstartingcomponents.



WSCL0910I:Initializingcomponent:com.ibm.ws.activity.Activity

WSCL0911I:Componentinitializedsuccessfully.

WSCL0901I:Componentinitializationcompletedsuccessfully.



WSCL0035I:InitializationoftheJ2EEApplicationClientEnviro



WSCL0014I:InvokingtheApplicationClientclassjunit.textui.T

........

Time:9.363



OK(8tests)



IfyouseeanythingattheendotherthanOK(8tests)youneedtogo

backoveryourconfigurationstepsanddeterminewhatwentwrong.



30.13RulesforCreatingSessionFaỗades

Nowthatyou'velearnedwhatsessionfaỗadeinterfaceslooklike,and

whatobjectssitbehindthesessionfaỗades,thequestionyoumayhave

is"HowmanyofthesethingswillIhave?"Youdon'twanttohavetoo

manysessionfaỗades,otherwiseyoulosethebenefitsoftheFaỗade

pattern.However,asinglesessionfaỗadeforanentireapplicationmight

becomea"GodObject"[7]andcauseproblemsofitown.Herearesome

rulesfordesigningsessionfaỗadestoachievetherightlevelof

granularity.

[7][Brown98]p.73.



Lookforfunctionalsubsystemsinyourapplication.Subsystems

namedOrderManagement,Billing,andShippingmightbethe

sourceofthreepotentialFaỗadeobjectsinanapplication.

Gobacktoyourusecasesandlookforrelatedgroupsofusecases.

Agroupofrelatedusecases(likebuyastock,sellastock,geta

pricequote)mightsuggestacohesivesubsystemlikeStockTrading.

Thissinglecohesivesubsystemwillprobablysharemanyinner-layer

objectsandbeagoodcandidateforasessionfaỗade.[Sun2001]

discussesthisapproachinmoredepth.

Donotmakeeachindividualusecaseintoasessionfaỗade.This

resultsinasystemwithfartoolargeofadistributioncrosssection.

TheclientswillhavetomanagetoomanyEJBreferencesand

homesinthiscase.

Afteraninitialpass,lookattherelationshipsbetweenthesecondlayerobjectsinyourdesign.Ifyouseethattherearedisjointgroups

ofvalueobjects,factories,andactions,separatethefaỗadeintotwo

ormorefaỗadesbasedaroundtheactualgroupings.



30.14ShouldSessionFaỗadesReturnXML?

Oneofthebiggerquestionspeoplestrugglewithinregardtotheprevious

architectureisthequestionofwhattheparametersandreturntypesof

thefaỗademethodsshouldbe.Inparticular,there'sacommon

antipatternthathasfounditswayintoseveralprojectsthatshouldbe

squashedbeforeitinfectsmoreprojectsandcausesmoregrief.

Whatwehavebeenassuminginourprecedingdiscussionisthat

SessionFaỗademethodscouldtakeasargumentsandreturnsimple,

serializableJavaobjects(DTOsorvalueobjects).However,othershave

recommendedthatinsteadFaỗademethodscouldtakeasarguments

andreturnanXML(string)representationofthedatarequested.

Infact,acommonquestionthatwefindourselvesaddressingquiteoften

onmailinglistsandbulletinboardslikeJavaRanchis"Howdoyoupassa

DOMintoanEJB?"Theshortanswerisyoucan't.DOMobjectsarenot

serializable,and,asyou'velearned,allEJBargumentsmustbedeclared

asbeingserializable.Butthemoreinsidiousquestioniswhydoyouwant

todothat?WhatadvantagewouldsendingaDOM(orastring

representationofXML,whichisallyoucouldreallysend)toanEJB

possiblyconvey?

First,let'sbeginwithaconsiderationofDTOs.DTOshavetheadvantage

thattheycanbeveryefficientlyserialized.Inmostcases,thesizeofa

binaryvalueobjectrepresentationofadatasetwouldbesmallerthanthe

correspondingXMLrepresentationofthesamedata.Also,sincetheJava

serializationmechanismisahighlyoptimizedpartoftheJavabase

classesandJVM,theserializationprocessisusuallysignificantly(an

orderofmagnitudeormore)fasterthangeneratingandparsing

correspondingXML.What'smore,DTOsareparticularlyattractivesince

theJSPspecificationhasbeenexplicitlydesignedtomakedisplayingthe

partsofaJavaBeaneasyandefficient.Thisistrueevenifthedesired

outputoftheJSPisXML.

Tobefair,DTOshavesomedrawbacks.Thebiggestisthatitforcesyou



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

11 Testing the Session Façade Example with JUnit

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

×