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          ServletUnitClient client = sr.newClient();
60  
61          return sr.newClient();
62      }
63      
64      /***
65       * Here we expect an errorCode other than 200, and look for it
66       * checking for text is omitted as it doesnt work. It would never work on
67       * java1.3, but one may have expected java1.4+ to have access to the
68       * error stream in responses. Clearly not.
69       * @param request
70       * @param errorCode
71       * @param errorText optional text string to search for
72       * @throws MalformedURLException
73       * @throws IOException
74       * @throws SAXException
75       */
76      protected void expectErrorCode(
77          WebRequest request,
78          int errorCode,
79          String errorText)
80          throws MalformedURLException, IOException, SAXException
81      {
82          String failureText =
83              "Expected error " + errorCode + " from " + request.getURL();
84      
85          try
86          {
87              WebResponse response = newClient().getResponse(request);
88              fail(errorText + " -got success instead");
89          }
90          catch (HttpException e)
91          {
92              assertEquals(failureText, errorCode, e.getResponseCode());
93              /* checking for text omitted as it doesnt work.
94              if(errorText!=null) {
95              	assertTrue(
96              			"Failed to find "+errorText+" in "+ e.getResponseMessage(),
97              			e.getMessage().indexOf(errorText)>=0);
98              }
99              */
100         }
101     }
102 
103 }