001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.transport.http;
018:
019: import java.net.URL;
020: import javax.servlet.ServletException;
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023:
024: import org.custommonkey.xmlunit.XMLTestCase;
025: import org.easymock.MockControl;
026: import org.springframework.mock.web.MockHttpServletRequest;
027: import org.springframework.mock.web.MockHttpServletResponse;
028: import org.springframework.ws.wsdl.WsdlDefinition;
029: import org.springframework.xml.transform.StringSource;
030: import org.w3c.dom.Document;
031:
032: public class WsdlDefinitionHandlerAdapterTest extends XMLTestCase {
033:
034: private WsdlDefinitionHandlerAdapter adapter;
035:
036: private MockControl definitionControl;
037:
038: private WsdlDefinition definitionMock;
039:
040: private MockHttpServletRequest request;
041:
042: private MockHttpServletResponse response;
043:
044: protected void setUp() throws Exception {
045: adapter = new WsdlDefinitionHandlerAdapter();
046: definitionControl = MockControl
047: .createControl(WsdlDefinition.class);
048: definitionMock = (WsdlDefinition) definitionControl.getMock();
049: adapter.afterPropertiesSet();
050: request = new MockHttpServletRequest();
051: response = new MockHttpServletResponse();
052: }
053:
054: public void testHandleGet() throws Exception {
055: request.setMethod("GET");
056: String definition = "<definition xmlns='http://schemas.xmlsoap.org/wsdl/'/>";
057: definitionControl.expectAndReturn(definitionMock.getSource(),
058: new StringSource(definition));
059: definitionControl.replay();
060: adapter.handle(request, response, definitionMock);
061: assertXMLEqual(definition, response.getContentAsString());
062: definitionControl.verify();
063: }
064:
065: public void testHandleNonGet() throws Exception {
066: request.setMethod("POST");
067: definitionControl.replay();
068: try {
069: adapter.handle(request, response, definitionMock);
070: fail("ServletException expected");
071: } catch (ServletException ex) {
072: // expected
073: }
074: definitionControl.verify();
075: }
076:
077: public void testTransformLocations() throws Exception {
078: adapter.setTransformLocations(true);
079: request.setMethod("GET");
080: request.setScheme("http");
081: request.setServerName("example.com");
082: request.setServerPort(8080);
083: request.setContextPath("/context");
084: request.setServletPath("/service.wsdl");
085: request.setPathInfo(null);
086: request.setRequestURI("/context/service.wsdl");
087: definitionControl.replay();
088:
089: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
090: .newInstance();
091: documentBuilderFactory.setNamespaceAware(true);
092: DocumentBuilder documentBuilder = documentBuilderFactory
093: .newDocumentBuilder();
094: Document result = documentBuilder.parse(getClass()
095: .getResourceAsStream("wsdl11-input.wsdl"));
096: adapter.transformLocations(result, request);
097: Document expectedDocument = documentBuilder.parse(getClass()
098: .getResourceAsStream("wsdl11-expected.wsdl"));
099: assertXMLEqual("Invalid result", expectedDocument, result);
100:
101: definitionControl.verify();
102: }
103:
104: public void testTransformLocationFullUrl() throws Exception {
105: request.setScheme("http");
106: request.setServerName("example.com");
107: request.setServerPort(8080);
108: request.setContextPath("/context");
109: request.setPathInfo("/service.wsdl");
110: request.setRequestURI("/context/service.wsdl");
111: String oldLocation = "http://localhost:8080/context/service";
112:
113: String result = adapter.transformLocation(oldLocation, request);
114: assertNotNull("No result", result);
115: assertEquals("Invalid result", new URL(
116: "http://example.com:8080/context/service"), new URL(
117: result));
118: }
119:
120: public void testTransformLocationEmptyContextFullUrl()
121: throws Exception {
122: request.setScheme("http");
123: request.setServerName("example.com");
124: request.setServerPort(8080);
125: request.setContextPath("");
126: request.setRequestURI("/service.wsdl");
127: String oldLocation = "http://localhost:8080/service";
128:
129: String result = adapter.transformLocation(oldLocation, request);
130: assertNotNull("No result", result);
131: assertEquals("Invalid result", new URL(
132: "http://example.com:8080/service"), new URL(result));
133: }
134:
135: public void testTransformLocationRelativeUrl() throws Exception {
136: request.setScheme("http");
137: request.setServerName("example.com");
138: request.setServerPort(8080);
139: request.setContextPath("/context");
140: request.setPathInfo("/service.wsdl");
141: request.setRequestURI("/context/service.wsdl");
142: String oldLocation = "/service";
143:
144: String result = adapter.transformLocation(oldLocation, request);
145: assertNotNull("No result", result);
146: assertEquals("Invalid result", new URL(
147: "http://example.com:8080/context/service"), new URL(
148: result));
149: }
150:
151: public void testTransformLocationEmptyContextRelativeUrl()
152: throws Exception {
153: request.setScheme("http");
154: request.setServerName("example.com");
155: request.setServerPort(8080);
156: request.setContextPath("");
157: request.setRequestURI("/service.wsdl");
158: String oldLocation = "/service";
159:
160: String result = adapter.transformLocation(oldLocation, request);
161: assertNotNull("No result", result);
162: assertEquals("Invalid result", new URL(
163: "http://example.com:8080/service"), new URL(result));
164: }
165: }
|