Clover coverage report - Ivory - 1.0-alpha-5
Coverage timestamp: Sun Nov 9 2003 22:02:40 EST
file stats: LOC: 473   Methods: 19
NCLOC: 250   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
IvoryTestCase.java 16.7% 40.4% 52.6% 37.2%
coverage coverage
 1   
 package org.codehaus.ivory.plexus;
 2   
 
 3   
 /*
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products derived
 30   
  *    from this software without prior written permission. For written
 31   
  *    permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  */
 56   
 
 57   
 import java.io.IOException;
 58   
 import java.io.InputStream;
 59   
 import java.io.StringWriter;
 60   
 import java.net.MalformedURLException;
 61   
 
 62   
 import javax.servlet.ServletContext;
 63   
 
 64   
 import org.apache.avalon.framework.service.ServiceManager;
 65   
 import org.apache.axis.MessageContext;
 66   
 import org.apache.axis.server.AxisServer;
 67   
 import org.apache.axis.soap.SOAPConstants;
 68   
 import org.apache.axis.transport.local.LocalTransport;
 69   
 import org.apache.axis.utils.XMLUtils;
 70   
 import org.codehaus.plexus.PlexusTestCase;
 71   
 import org.codehaus.plexus.lifecycle.avalon.AvalonServiceManager;
 72   
 import org.codehaus.plexus.servlet.PlexusServlet;
 73   
 import org.codehaus.ivory.AxisService;
 74   
 import org.w3c.dom.Document;
 75   
 import org.xml.sax.SAXException;
 76   
 
 77   
 import com.meterware.httpunit.HttpException;
 78   
 import com.meterware.httpunit.HttpUnitOptions;
 79   
 import com.meterware.httpunit.WebConversation;
 80   
 import com.meterware.httpunit.WebRequest;
 81   
 import com.meterware.httpunit.WebResponse;
 82   
 import com.meterware.servletunit.InvocationContext;
 83   
 import com.meterware.servletunit.ServletRunner;
 84   
 import com.meterware.servletunit.ServletUnitClient;
 85   
 
 86   
 /**
 87   
  * A generic test-case for testing Ivory and other SOAP services for Plexus.
 88   
  * 
 89   
  * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
 90   
  * @since May 4, 2003
 91   
  */
 92   
 public class IvoryTestCase extends PlexusTestCase
 93   
 {
 94   
     private ServletRunner sr;
 95   
     
 96   
     private ServiceManager manager;
 97   
     
 98   
     private String services = "http://localhost/services/";
 99   
     
 100   
     private boolean verbose = false;
 101   
     
 102   
     public final static String VERBOSE_KEY = 
 103   
         "IvoryTestCase.verbose";
 104   
     
 105  6
     public IvoryTestCase(String name)
 106   
     {
 107  6
         super(name);
 108   
     }
 109   
     
 110  5
     public void setUp() throws Exception
 111   
     {
 112  5
         super.setUp();
 113  5
         setVerbose( Boolean.getBoolean( VERBOSE_KEY ) );
 114   
     
 115  5
         HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
 116   
     
 117  5
         manager =
 118   
             new AvalonServiceManager(getContainer().getComponentRepository());
 119   
     
 120  5
         InputStream is =
 121   
             getClass().getResourceAsStream(
 122   
                 "/org/codehaus/ivory/plexus/web.xml");
 123   
     
 124  5
         sr = new ServletRunner(is);
 125   
         
 126  5
         ServletUnitClient client = newClient();
 127   
     
 128   
         // There must be a better way to do this.
 129  5
         InvocationContext ic =
 130   
             client.newInvocation("http://localhost/servlet/AxisServlet");
 131  5
         ServletContext context =
 132   
             ic.getServlet().getServletConfig().getServletContext();
 133  5
         context.setAttribute(PlexusServlet.SERVICE_MANAGER_KEY, manager);
 134   
     }
 135   
     
 136  7
     protected ServletUnitClient newClient() throws Exception
 137   
     {
 138  7
         return sr.newClient();
 139   
     }
 140   
     
 141   
     /**
 142   
      * @return
 143   
      */
 144  2
     public boolean isVerbose()
 145   
     {
 146  2
         return verbose;
 147   
     }
 148   
     
 149   
     /**
 150   
      * @param b
 151   
      */
 152  5
     public void setVerbose(boolean b)
 153   
     {
 154  5
         verbose = b;
 155   
     }
 156   
     
 157   
     /**
 158   
      * Assert that the response contains a string.
 159   
      * @param response
 160   
      * @param searchfor
 161   
      * @throws IOException
 162   
      */
 163  6
     public void assertStringInBody(
 164   
         String body,
 165   
         String searchfor)
 166   
         throws IOException
 167   
     {
 168  6
         boolean found = body.indexOf(searchfor) >= 0;
 169  6
         if (!found)
 170   
         {
 171  0
             String message;
 172  0
             message = "failed to find [" + searchfor + "].\n";
 173   
             
 174  0
             if ( isVerbose() )
 175  0
                  message += "Body:\n" + body;
 176   
                  
 177  0
             fail(message);
 178   
         }
 179   
     }
 180   
     
 181   
     /**
 182   
      * Assert that the response contains a string.
 183   
      * @param response
 184   
      * @param searchfor
 185   
      * @throws IOException
 186   
      */
 187  0
     public void assertStringInBody(
 188   
         WebResponse response,
 189   
         String searchfor)
 190   
         throws IOException
 191   
     {
 192  0
         String body = response.getText();
 193  0
         boolean found = body.indexOf(searchfor) >= 0;
 194  0
         if (!found)
 195   
         {
 196  0
             String message;
 197  0
             message = "failed to find [" + searchfor + "]";
 198   
             
 199  0
             if ( isVerbose() )
 200  0
                  message += "Body:\n" + body;
 201   
                  
 202  0
             fail(message);
 203   
         }
 204   
     }
 205   
     
 206   
     /**
 207   
      * Assert that the response contains a string.
 208   
      * @param response
 209   
      * @param searchfor
 210   
      * @param url
 211   
      * @throws IOException
 212   
      */
 213  0
     public void assertStringInBody(
 214   
         WebResponse response,
 215   
         String searchfor,
 216   
         String url)
 217   
         throws IOException
 218   
     {
 219  0
         String body = response.getText();
 220  0
         boolean found = body.indexOf(searchfor) >= 0;
 221  0
         if (!found)
 222   
         {
 223  0
             String message;
 224  0
             message = "failed to find [" + searchfor + "] at " + url;
 225   
             
 226  0
             if ( isVerbose() )
 227  0
                  message += "Body:\n" + body;
 228   
                  
 229  0
             fail(message);
 230   
         }
 231   
     }
 232   
     
 233   
     /**
 234   
      * Assert that a named string is in the request body of the.
 235   
      * 
 236   
      * response to a request
 237   
      * @param request what we ask
 238   
      * @param searchfor string to look for
 239   
      * @throws IOException when the fetch fails
 240   
      * @throws org.xml.sax.SAXException
 241   
      */
 242  0
     protected void assertStringInBody(WebRequest request, String searchfor)
 243   
         throws IOException, org.xml.sax.SAXException
 244   
     {
 245  0
         WebResponse response = makeRequest(request);
 246  0
         assertStringInBody(response, searchfor, request.getURL().toString());
 247   
     }
 248   
     
 249   
     /**
 250   
      * Make a request in a new session.
 251   
      * @param request   request to make
 252   
      * @return the response
 253   
      * @throws IOException
 254   
      * @throws SAXException
 255   
      */
 256  0
     protected WebResponse makeRequest(WebRequest request)
 257   
         throws IOException, SAXException
 258   
     {
 259  0
         WebConversation session = new WebConversation();
 260  0
         WebResponse response = session.getResponse(request);
 261  0
         return response;
 262   
     }
 263   
     
 264   
     /**
 265   
      * Assert that a string is not in a response.
 266   
      * @param response
 267   
      * @param searchfor
 268   
      * @param url
 269   
      * @throws IOException
 270   
      */
 271  0
     protected void assertStringNotInBody(
 272   
         String body,
 273   
         String searchfor)
 274   
         throws IOException
 275   
     {
 276  0
         boolean found = body.indexOf(searchfor) >= 0;
 277  0
         if (found)
 278   
         {
 279  0
             String message;
 280  0
             message = "unexpectedly found [" + searchfor + "].";
 281   
             
 282  0
             if ( isVerbose() )
 283  0
                  message += "Body:\n" + body;
 284   
                  
 285  0
             fail(message);
 286   
         }
 287   
     }
 288   
     
 289   
     /**
 290   
      * Assert that a string is not in a response.
 291   
      * @param response
 292   
      * @param searchfor
 293   
      * @param url
 294   
      * @throws IOException
 295   
      */
 296  0
     protected void assertStringNotInBody(
 297   
         WebResponse response,
 298   
         String searchfor)
 299   
         throws IOException
 300   
     {
 301  0
         String body = response.getText();
 302  0
         boolean found = body.indexOf(searchfor) >= 0;
 303  0
         if (found)
 304   
         {
 305  0
             String message;
 306  0
             message = "unexpectedly found [" + searchfor + "].";
 307   
             
 308  0
             if ( isVerbose() )
 309  0
                  message += "Body:\n" + body;
 310   
                  
 311  0
             fail(message);
 312   
         }
 313   
     }
 314   
     
 315   
     /**
 316   
      * Assert that a string is not in a response.
 317   
      * @param response
 318   
      * @param searchfor
 319   
      * @param url
 320   
      * @throws IOException
 321   
      */
 322  0
     protected void assertStringNotInBody(
 323   
         WebResponse response,
 324   
         String searchfor,
 325   
         String url)
 326   
         throws IOException
 327   
     {
 328  0
         String body = response.getText();
 329  0
         boolean found = body.indexOf(searchfor) >= 0;
 330  0
         if (found)
 331   
         {
 332  0
             String message;
 333  0
             message = "unexpectedly found [" + searchfor + "] at " + url;
 334   
             
 335  0
             if ( isVerbose() )
 336  0
                  message += "Body:\n" + body;
 337   
                  
 338  0
             fail(message);
 339   
         }
 340   
     
 341   
     }
 342   
     
 343   
     /**
 344   
      * Assert that a string is not in the response to a request.
 345   
      * @param request
 346   
      * @param searchfor
 347   
      * @throws IOException
 348   
      * @throws org.xml.sax.SAXException
 349   
      */
 350  0
     protected void assertStringNotInBody(WebRequest request, String searchfor)
 351   
         throws IOException, org.xml.sax.SAXException
 352   
     {
 353  0
         WebConversation session = new WebConversation();
 354  0
         WebResponse response = session.getResponse(request);
 355  0
         assertStringNotInBody(response, searchfor, request.getURL().toString());
 356   
     }
 357   
     
 358  3
     protected void assertIsXml(String response)
 359   
     {
 360  3
         if(  response.indexOf("<?xml") != 0 )
 361   
         {
 362  0
             fail( "Invalid XML:\n" + response );
 363   
         } 
 364   
     }
 365   
     
 366   
     /**
 367   
      * Here we expect an errorCode other than 200, and look for it
 368   
      * checking for text is omitted as it doesnt work. It would never work on
 369   
      * java1.3, but one may have expected java1.4+ to have access to the
 370   
      * error stream in responses. Clearly not.
 371   
      * @param request
 372   
      * @param errorCode
 373   
      * @param errorText optional text string to search for
 374   
      * @throws MalformedURLException
 375   
      * @throws IOException
 376   
      * @throws SAXException
 377   
      */
 378  0
     protected void expectErrorCode(
 379   
         WebRequest request,
 380   
         int errorCode,
 381   
         String errorText)
 382   
         throws MalformedURLException, IOException, SAXException
 383   
     {
 384  0
         WebConversation session = new WebConversation();
 385  0
         String failureText =
 386   
             "Expected error " + errorCode + " from " + request.getURL();
 387   
     
 388  0
         try
 389   
         {
 390  0
             session.getResponse(request);
 391  0
             fail(errorText + " -got success instead");
 392   
         }
 393   
         catch (HttpException e)
 394   
         {
 395  0
             assertEquals(failureText, errorCode, e.getResponseCode());
 396   
             /* checking for text omitted as it doesnt work.
 397   
             if(errorText!=null) {
 398   
                 assertTrue(
 399   
                         "Failed to find "+errorText+" in "+ e.getResponseMessage(),
 400   
                         e.getMessage().indexOf(errorText)>=0);
 401   
             }
 402   
             */
 403   
         }
 404   
     }
 405   
     
 406   
     /**
 407   
      * Verifies that the service generates WSDL.
 408   
      * 
 409   
      * @param service
 410   
      * @param method
 411   
      */
 412  3
     public void assertValidWSDL( String serviceName, String method )
 413   
         throws Exception
 414   
     {
 415  3
         assertValidWSDL( serviceName, new String[]{ method } );
 416   
     }
 417   
     
 418  3
     public String getWSDL( String serviceName ) throws Exception
 419   
     {
 420  3
         AxisService service = ( AxisService ) getComponent( AxisService.ROLE );
 421  3
         AxisServer server = service.getAxisServer();
 422   
     
 423  3
         LocalTransport transport = new LocalTransport(server);
 424   
     
 425  3
         MessageContext msgContext = new MessageContext(server);
 426  3
         msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
 427  3
         msgContext.setEncodingStyle(SOAPConstants.SOAP12_CONSTANTS.getEncodingURI());
 428   
     
 429  3
         msgContext.setTargetService( serviceName );
 430   
         
 431   
         // During a real invocation this is set by the handler, however we
 432   
         // need to set it hear to get the wsdl generation working.
 433  3
         msgContext.setProperty( MessageContext.TRANS_URL, 
 434   
                                 services + serviceName );
 435  3
         server.generateWSDL( msgContext );        
 436   
         
 437   
         // another one of those undocumented "features"
 438  3
         Document doc = (Document) msgContext.getProperty( "WSDL" );
 439   
         
 440  3
         StringWriter writer = new StringWriter();
 441  3
         XMLUtils.DocumentToWriter(doc, writer);
 442   
         
 443  3
         return writer.toString();
 444   
     }
 445   
     
 446   
     /**
 447   
      * Verifies that the service generates WSDL.
 448   
      * 
 449   
      * @param service
 450   
      * @param methods
 451   
      */
 452  3
     public void assertValidWSDL( String serviceName, String methods[] )
 453   
         throws Exception
 454   
     {
 455  3
         String response = getWSDL( serviceName );
 456   
     
 457  2
         if ( isVerbose() )
 458   
         {
 459  2
             System.out.println( "WSDL for " + serviceName + ":" );
 460  2
             System.out.println( response );
 461   
         }
 462   
         
 463  2
         assertIsXml( response );
 464   
         
 465  2
         for ( int i = 0; i < methods.length; i++ )
 466   
         {
 467  2
             assertStringInBody( response, "<wsdl:operation name=\"" + methods[i] + "\">" );
 468  2
             assertStringInBody( response, "<wsdl:input name=\"" + methods[i] + "Request\">" );
 469  2
             assertStringInBody( response, "<wsdl:output name=\"" + methods[i] + "Response\">" );
 470   
         }
 471   
     }
 472   
 }
 473