Web Services的测试模型与代码摘录

来源:互联网 发布:淘宝店面装修在哪 编辑:程序博客网 时间:2024/06/05 18:42
测试Web Services的有效性、性能、可伸缩性、可靠性以及安全性时所面临的主要挑战是 Web Services的分布性。

  为了使完整的Web Services能够实现预期的功能,就要求客户端和服务都要满足一系列的要求。接口必须在其WSDL文档中正确描述出来,消息必须遵守传输协议规范(如HTTP1.1)和消息协议(如SOAP 1.1)。同时消息必须遵守描述该服务的WSDL文档中的契约,要求同时考虑到消息的内容和传输层的绑定。加上综合的安全条款、互操作性问题、UDDI注册要求以及一定负载下的性能需求,就很容易发现为什么网络测试不是无足轻重的事情。

  Web Services组件可由多个利益相关者来共同构建和部署。因此,测试这些组件过程中会发现确定代码质量、可用性等都有很大的难度。Web Services的标准是简单的,数据驱动的,并且共享一个公共的基于XML的基础。传统的测试工具可能不足以有效地测试这些标准。而且GUI自动化工具也不足以有效地测试Web Services的接口点和消息格式。

  功能测试

  该测试的目标相当直观易懂:确保服务器能够对给定的请求发送正确的响应。然而,由于Web Services的复杂性,该任务原非想象的那么简单。对于大多数的Web Services而言,它不可能精确预见客户端会发来什么类型的请求。枚举所有可能的请求并不切实可行,因为可能输入的空间要么是没有边界,要么就是无穷大。因此,验证服务器是否能处理大范围的请求类型和参数是极其重要的。

   public boolean execute(String action, String symbol, int quantity)

   throws javax.xml.soap.SOAPException{

   Detail detail = null;

   detail = SOAPFactory.newInstance().createDetail();

   detail.addChildElement( "Stock Trade" ).addTextNode( "failed" );

   System.out.println("execute() in webservices.stock.trade webservice has been invoked

   with following arguments:: action:" + action +

   " symbol:" + symbol + " quantity:" + quantity);

   if(action == null) {

   throw new SOAPFaultException(new QName( "http://StockTrade/execute", "ServerFailed" ),

   "action parameter is null.",

   null,

   detail);

   }

   if(symbol == null) {

   throw new SOAPFaultException(new QName( "http://StockTrade/execute", "ServerFailed" ),

   "symbol parameter is null.",

   null,

   detail);

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

   }