View Javadoc

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