1 package org.codehaus.xfire.test;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5
6 import com.meterware.httpunit.HttpException;
7 import com.meterware.httpunit.HttpUnitOptions;
8 import com.meterware.httpunit.WebRequest;
9 import com.meterware.httpunit.WebResponse;
10 import com.meterware.servletunit.ServletRunner;
11 import com.meterware.servletunit.ServletUnitClient;
12 import org.codehaus.xfire.XFire;
13 import org.codehaus.xfire.XFireFactory;
14 import org.xml.sax.SAXException;
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 abstract 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()
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 String failureText =
74 "Expected error " + errorCode + " from " + request.getURL();
75
76 try
77 {
78 WebResponse response = newClient().getResponse(request);
79 fail(errorText + " -got success instead");
80 }
81 catch (HttpException e)
82 {
83 assertEquals(failureText, errorCode, e.getResponseCode());
84
85
86
87
88
89
90
91 }
92 }
93
94 }