001: package org.springframework.ws.transport.http;
002:
003: import java.io.ByteArrayInputStream;
004: import java.util.List;
005: import javax.servlet.ServletConfig;
006: import javax.servlet.ServletException;
007: import javax.xml.parsers.DocumentBuilder;
008: import javax.xml.parsers.DocumentBuilderFactory;
009:
010: import org.custommonkey.xmlunit.XMLTestCase;
011: import org.custommonkey.xmlunit.XMLUnit;
012: import org.springframework.beans.BeansException;
013: import org.springframework.beans.MutablePropertyValues;
014: import org.springframework.core.io.ClassPathResource;
015: import org.springframework.mock.web.MockHttpServletRequest;
016: import org.springframework.mock.web.MockHttpServletResponse;
017: import org.springframework.mock.web.MockServletConfig;
018: import org.springframework.mock.web.MockServletContext;
019: import org.springframework.web.context.support.StaticWebApplicationContext;
020: import org.springframework.ws.server.MessageDispatcher;
021: import org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter;
022: import org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping;
023: import org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver;
024: import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
025: import org.w3c.dom.Document;
026:
027: public class MessageDispatcherServletTest extends XMLTestCase {
028:
029: private ServletConfig config;
030:
031: private MessageDispatcherServlet servlet;
032:
033: protected void setUp() throws Exception {
034: config = new MockServletConfig(new MockServletContext(),
035: "spring-ws");
036: servlet = new MessageDispatcherServlet();
037: }
038:
039: private void assertStrategies(Class expectedClass, List actual) {
040: assertEquals("Invalid amount of strategies", 1, actual.size());
041: Object strategy = actual.get(0);
042: assertTrue("Invalid strategy", expectedClass
043: .isAssignableFrom(strategy.getClass()));
044: }
045:
046: public void testDefaultStrategies() throws ServletException {
047: servlet.setContextClass(StaticWebApplicationContext.class);
048: servlet.init(config);
049: MessageDispatcher messageDispatcher = (MessageDispatcher) servlet
050: .getMessageReceiver();
051: assertNotNull("No messageDispatcher created", messageDispatcher);
052: }
053:
054: public void testDetectedStrategies() throws ServletException {
055: servlet.setContextClass(DetectWebApplicationContext.class);
056: servlet.init(config);
057: MessageDispatcher messageDispatcher = (MessageDispatcher) servlet
058: .getMessageReceiver();
059: assertNotNull("No messageDispatcher created", messageDispatcher);
060: assertStrategies(PayloadRootQNameEndpointMapping.class,
061: messageDispatcher.getEndpointMappings());
062: assertStrategies(PayloadEndpointAdapter.class,
063: messageDispatcher.getEndpointAdapters());
064: assertStrategies(SimpleSoapExceptionResolver.class,
065: messageDispatcher.getEndpointExceptionResolvers());
066: }
067:
068: public void testDetectWsdlDefinitions() throws Exception {
069: servlet
070: .setContextClass(WsdlDefinitionWebApplicationContext.class);
071: servlet.init(config);
072: MockHttpServletRequest request = new MockHttpServletRequest(
073: "GET", "/definition.wsdl");
074: MockHttpServletResponse response = new MockHttpServletResponse();
075: servlet.service(request, response);
076: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
077: .newInstance();
078: documentBuilderFactory.setNamespaceAware(true);
079: DocumentBuilder documentBuilder = documentBuilderFactory
080: .newDocumentBuilder();
081: Document result = documentBuilder
082: .parse(new ByteArrayInputStream(response
083: .getContentAsByteArray()));
084: Document expected = documentBuilder.parse(getClass()
085: .getResourceAsStream("wsdl11-input.wsdl"));
086: XMLUnit.setIgnoreWhitespace(true);
087: assertXMLEqual("Invalid WSDL written", expected, result);
088: }
089:
090: private static class DetectWebApplicationContext extends
091: StaticWebApplicationContext {
092:
093: public void refresh() throws BeansException,
094: IllegalStateException {
095: registerSingleton("payloadMapping",
096: PayloadRootQNameEndpointMapping.class);
097: registerSingleton("payloadAdapter",
098: PayloadEndpointAdapter.class);
099: registerSingleton("simpleExceptionResolver",
100: SimpleSoapExceptionResolver.class);
101: super .refresh();
102: }
103: }
104:
105: private static class WsdlDefinitionWebApplicationContext extends
106: StaticWebApplicationContext {
107:
108: public void refresh() throws BeansException,
109: IllegalStateException {
110: MutablePropertyValues mpv = new MutablePropertyValues();
111: mpv.addPropertyValue("wsdl", new ClassPathResource(
112: "wsdl11-input.wsdl", getClass()));
113: registerSingleton("definition",
114: SimpleWsdl11Definition.class, mpv);
115: super.refresh();
116: }
117: }
118: }
|