Web 服務的測試模型與代碼摘錄[2] 軟件測試
if(action.equalsIgnoreCase("BUY"))
System.out.println("BUYING quantity: "+ quantity + " of symbol:" + symbol);
// Invoke method to execute trade here.
else if(action.equalsIgnoreCase("SELL"))
System.out.println("SELLING quantity: "+ quantity + " of symbol:" + symbol);
// Invoke method to execute trade here.
else
{
System.out.println("INVALID action: "+ action);
throw new SOAPFaultException(new QName( "http://StockTrade/execute", "ServerFailed" ),
"Invalid Action:" + action,
null,
detail);
}
return true;
}
代碼摘錄:Stock Trade Web Services
該段摘錄的代碼是Stock Trade Web Services的“execute()”方法的實現代碼。該方法首先驗證輸入參數的有效性,驗證成功才執行功能。舉例說明,如果參數action是空值,它就會拋出一個SoapFaultException異常,用faultstring參數(第二個參數)說明造成異常的原因。為了舉例說明,在對參數 symbol進行相似的驗證之后,Web Services給出了處理機。在實際的情況下,商業邏輯應該在此位置中實現:
try{
// Setup the global JAXM message factory
System.setProperty("javax.xml.soap.MessageFactory",
"weblogic.webservice.core.soap.MessageFactoryImpl");
// Setup the global JAX-RPC service factory
System.setProperty( "javax.xml.rpc.ServiceFactory",
"weblogic.webservice.core.rpc.ServiceFactoryImpl");
StockTrade_Impl ws = new StockTrade_Impl();
StockTradePort port = ws.getStockTradePort();
boolean returnVal = port.execute(action, symbol, quantity);
System.out.println("The webservice got back the following result:" + returnVal);
}catch(Exception e) {
}
文章來源于領測軟件測試網 http://www.k11sc111.com/