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) {
}