001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.systest.servlet;
019:
020: import org.w3c.dom.Document;
021:
022: import com.meterware.httpunit.GetMethodWebRequest;
023: import com.meterware.httpunit.PostMethodWebRequest;
024: import com.meterware.httpunit.WebLink;
025: import com.meterware.httpunit.WebRequest;
026: import com.meterware.httpunit.WebResponse;
027: import com.meterware.servletunit.ServletUnitClient;
028:
029: import org.apache.cxf.Bus;
030: import org.apache.cxf.BusException;
031: import org.apache.cxf.BusFactory;
032: import org.apache.cxf.helpers.DOMUtils;
033: import org.junit.Before;
034: import org.junit.Test;
035:
036: public class CXFServletTest extends AbstractServletTest {
037:
038: @Before
039: public void setUp() throws Exception {
040: BusFactory.setDefaultBus(null);
041: BusFactory.setThreadDefaultBus(null);
042: super .setUp();
043: }
044:
045: @Override
046: protected Bus createBus() throws BusException {
047: return null;
048: }
049:
050: @Test
051: public void testPostInvokeServices() throws Exception {
052:
053: invoke("UTF-8");
054: invoke("iso-8859-1");
055: }
056:
057: private void invoke(String encoding) throws Exception {
058: WebRequest req = new PostMethodWebRequest(CONTEXT_URL
059: + "/services/greeter", getClass().getResourceAsStream(
060: "GreeterMessage.xml"), "text/xml; charset=" + encoding);
061:
062: ServletUnitClient client = newClient();
063: WebResponse response = client.getResponse(req);
064: client.setExceptionsThrownOnErrorStatus(false);
065:
066: assertEquals("text/xml", response.getContentType());
067: assertEquals(encoding, response.getCharacterSet());
068:
069: Document doc = DOMUtils.readXml(response.getInputStream());
070: assertNotNull(doc);
071:
072: addNamespace("h",
073: "http://apache.org/hello_world_soap_http/types");
074:
075: assertValid("/s:Envelope/s:Body", doc);
076: assertValid("//h:sayHiResponse", doc);
077: }
078:
079: @Test
080: public void testGetServiceList() throws Exception {
081: ServletUnitClient client = newClient();
082: client.setExceptionsThrownOnErrorStatus(false);
083:
084: WebResponse res = client.getResponse(CONTEXT_URL + "/services");
085:
086: WebLink[] links = res.getLinks();
087: assertEquals("There should get two link for service", 2,
088: links.length);
089: assertEquals(CONTEXT_URL + "/services/greeter?wsdl", links[0]
090: .getURLString());
091: assertEquals("text/html", res.getContentType());
092:
093: res = client.getResponse(CONTEXT_URL + "/services");
094: links = res.getLinks();
095: assertEquals("There should get two link for service", 2,
096: links.length);
097: assertEquals(CONTEXT_URL + "/services/greeter?wsdl", links[0]
098: .getURLString());
099: assertEquals("text/html", res.getContentType());
100:
101: // Ensure that the Bus is available for people doing an Endpoint.publish() or similar.
102: assertNotNull(BusFactory.getDefaultBus(false));
103: }
104:
105: @Test
106: public void testGetWSDL() throws Exception {
107: ServletUnitClient client = newClient();
108: client.setExceptionsThrownOnErrorStatus(true);
109:
110: WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL
111: + "/services/greeter?wsdl");
112:
113: WebResponse res = client.getResponse(req);
114: assertEquals(200, res.getResponseCode());
115: assertEquals("text/xml", res.getContentType());
116: assertTrue("the wsdl should contain the opertion greetMe", res
117: .getText()
118: .contains("<wsdl:operation name=\"greetMe\">"));
119: assertTrue("the soap address should changed", res.getText()
120: .contains(
121: "<soap:address location=\"" + CONTEXT_URL
122: + "/services/greeter\""));
123:
124: }
125:
126: @Test
127: public void testGetWSDLWithXMLBinding() throws Exception {
128: ServletUnitClient client = newClient();
129: client.setExceptionsThrownOnErrorStatus(true);
130:
131: WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL
132: + "/services/greeter2?wsdl");
133:
134: WebResponse res = client.getResponse(req);
135: assertEquals(200, res.getResponseCode());
136: assertEquals("text/xml", res.getContentType());
137: assertTrue("the wsdl should contain the opertion greetMe", res
138: .getText()
139: .contains("<wsdl:operation name=\"greetMe\">"));
140: assertTrue("the http address should changed", res.getText()
141: .contains(CONTEXT_URL + "/services/greeter2\""));
142:
143: }
144:
145: @Test
146: public void testInvalidServiceUrl() throws Exception {
147: ServletUnitClient client = newClient();
148: client.setExceptionsThrownOnErrorStatus(false);
149:
150: WebResponse res = client.getResponse(CONTEXT_URL
151: + "/services/NoSuchService");
152: assertEquals(404, res.getResponseCode());
153: assertEquals("text/html", res.getContentType());
154: }
155:
156: @Test
157: public void testServiceWsdlNotFound() throws Exception {
158: WebRequest req = new GetMethodWebRequest(CONTEXT_URL
159: + "/services/NoSuchService?wsdl");
160:
161: expectErrorCode(req, 404,
162: "Response code 404 required for invalid WSDL url.");
163: }
164:
165: @Test
166: public void testGetImportedXSD() throws Exception {
167: ServletUnitClient client = newClient();
168: client.setExceptionsThrownOnErrorStatus(true);
169:
170: WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL
171: + "/services/greeter?wsdl");
172: WebResponse res = client.getResponse(req);
173: assertEquals(200, res.getResponseCode());
174: String text = res.getText();
175: assertEquals("text/xml", res.getContentType());
176: assertTrue(text.contains(CONTEXT_URL
177: + "/services/greeter?wsdl=test_import.xsd"));
178:
179: req = new GetMethodQueryWebRequest(CONTEXT_URL
180: + "/services/greeter?wsdl=test_import.xsd");
181: res = client.getResponse(req);
182: assertEquals(200, res.getResponseCode());
183: text = res.getText();
184:
185: assertEquals("text/xml", res.getContentType());
186: assertTrue(
187: "the xsd should contain the completType SimpleStruct",
188: text.contains("<complexType name=\"SimpleStruct\">"));
189: }
190: }
|