1   package org.codehaus.xfire.transport.http;
2   
3   import java.io.IOException;
4   import java.net.MalformedURLException;
5   import org.codehaus.xfire.AbstractXFireTest;
6   import org.codehaus.xfire.XFire;
7   import org.codehaus.xfire.XFireFactory;
8   import org.xml.sax.SAXException;
9   import com.meterware.httpunit.HttpException;
10  import com.meterware.httpunit.HttpUnitOptions;
11  import com.meterware.httpunit.WebConversation;
12  import com.meterware.httpunit.WebRequest;
13  import com.meterware.servletunit.ServletRunner;
14  import com.meterware.servletunit.ServletUnitClient;
15  
16  /***
17   * A generic test-case for testing servlets.
18   * 
19   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
20   * @since May 4, 2003
21   */
22  public class AbstractServletTest
23      extends AbstractXFireTest
24  {
25      private ServletRunner sr;
26      
27      private XFireFactory factory;
28      
29      private XFire xfire;
30      
31      public void setUp() throws Exception
32      {
33          super.setUp();
34          
35          factory = XFireFactory.newInstance();
36          xfire = factory.getXFire();
37          
38          HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
39      
40          sr = new ServletRunner( getResourceAsStream("/org/codehaus/xfire/transport/http/web.xml") );
41      }
42  
43      protected XFire getXFire()
44      {
45          return xfire;
46      }
47      
48      protected ServletUnitClient newClient() throws Exception
49      {
50          ServletUnitClient client = sr.newClient();
51  
52          return sr.newClient();
53      }
54      
55      /***
56       * Here we expect an errorCode other than 200, and look for it
57       * checking for text is omitted as it doesnt work. It would never work on
58       * java1.3, but one may have expected java1.4+ to have access to the
59       * error stream in responses. Clearly not.
60       * @param request
61       * @param errorCode
62       * @param errorText optional text string to search for
63       * @throws MalformedURLException
64       * @throws IOException
65       * @throws SAXException
66       */
67      protected void expectErrorCode(
68          WebRequest request,
69          int errorCode,
70          String errorText)
71          throws MalformedURLException, IOException, SAXException
72      {
73          WebConversation session = new WebConversation();
74          String failureText =
75              "Expected error " + errorCode + " from " + request.getURL();
76      
77          try
78          {
79              session.getResponse(request);
80              fail(errorText + " -got success instead");
81          }
82          catch (HttpException e)
83          {
84              assertEquals(failureText, errorCode, e.getResponseCode());
85              /* checking for text omitted as it doesnt work.
86              if(errorText!=null) {
87              	assertTrue(
88              			"Failed to find "+errorText+" in "+ e.getResponseMessage(),
89              			e.getMessage().indexOf(errorText)>=0);
90              }
91              */
92          }
93      }
94  
95  }