01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.systest.servlet;
19:
20: import java.io.IOException;
21: import java.net.MalformedURLException;
22:
23: import org.xml.sax.SAXException;
24:
25: import com.meterware.httpunit.HttpException;
26: import com.meterware.httpunit.HttpNotFoundException;
27: import com.meterware.httpunit.HttpUnitOptions;
28: import com.meterware.httpunit.WebRequest;
29: import com.meterware.servletunit.ServletRunner;
30: import com.meterware.servletunit.ServletUnitClient;
31:
32: import org.apache.cxf.test.AbstractCXFTest;
33: import org.junit.Before;
34:
35: public abstract class AbstractServletTest extends AbstractCXFTest {
36: public static final String CONTEXT = "/mycontext";
37: public static final String CONTEXT_URL = "http://localhost/mycontext";
38: protected ServletRunner sr;
39:
40: @Before
41: public void setUp() throws Exception {
42: sr = new ServletRunner(getResourceAsStream(getConfiguration()),
43: CONTEXT);
44:
45: try {
46: sr.newClient().getResponse(CONTEXT_URL + "/services");
47: } catch (HttpNotFoundException e) {
48: // ignore, we just want to boot up the servlet
49: }
50:
51: HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
52: }
53:
54: /**
55: * @return The web.xml to use for testing.
56: */
57: protected String getConfiguration() {
58: return "/org/apache/cxf/systest/servlet/web.xml";
59: }
60:
61: protected ServletUnitClient newClient() {
62: return sr.newClient();
63: }
64:
65: /**
66: * Here we expect an errorCode other than 200, and look for it checking for
67: * text is omitted as it doesnt work. It would never work on java1.3, but
68: * one may have expected java1.4+ to have access to the error stream in
69: * responses. Clearly not.
70: *
71: * @param request
72: * @param errorCode
73: * @param errorText optional text string to search for
74: * @throws MalformedURLException
75: * @throws IOException
76: * @throws SAXException
77: */
78: protected void expectErrorCode(WebRequest request, int errorCode,
79: String errorText) throws MalformedURLException,
80: IOException, SAXException {
81: String failureText = "Expected error " + errorCode + " from "
82: + request.getURL();
83:
84: try {
85: newClient().getResponse(request);
86: fail(errorText + " -got success instead");
87: } catch (HttpException e) {
88: assertEquals(failureText, errorCode, e.getResponseCode());
89: /*
90: * checking for text omitted as it doesnt work. if(errorText!=null) {
91: * assertTrue( "Failed to find "+errorText+" in "+
92: * e.getResponseMessage(), e.getMessage().indexOf(errorText)>=0); }
93: */
94: }
95: }
96: }
|