1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.http;
18
19 import java.io.ByteArrayInputStream;
20 import java.util.List;
21 import javax.servlet.ServletConfig;
22 import javax.servlet.ServletException;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25
26 import org.springframework.beans.BeansException;
27 import org.springframework.beans.MutablePropertyValues;
28 import org.springframework.core.io.ClassPathResource;
29 import org.springframework.mock.web.MockHttpServletRequest;
30 import org.springframework.mock.web.MockHttpServletResponse;
31 import org.springframework.mock.web.MockServletConfig;
32 import org.springframework.mock.web.MockServletContext;
33 import org.springframework.web.context.support.StaticWebApplicationContext;
34 import org.springframework.ws.server.MessageDispatcher;
35 import org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter;
36 import org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping;
37 import org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver;
38 import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
39
40 import org.custommonkey.xmlunit.XMLUnit;
41 import org.junit.Assert;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.w3c.dom.Document;
45
46 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
47
48 public class MessageDispatcherServletTest {
49
50 private ServletConfig config;
51
52 private MessageDispatcherServlet servlet;
53
54 @Before
55 public void setUp() throws Exception {
56 config = new MockServletConfig(new MockServletContext(), "spring-ws");
57 servlet = new MessageDispatcherServlet();
58 }
59
60 private void assertStrategies(Class<?> expectedClass, List<?> actual) {
61 Assert.assertEquals("Invalid amount of strategies", 1, actual.size());
62 Object strategy = actual.get(0);
63 Assert.assertTrue("Invalid strategy", expectedClass.isAssignableFrom(strategy.getClass()));
64 }
65
66 @Test
67 public void testDefaultStrategies() throws ServletException {
68 servlet.setContextClass(StaticWebApplicationContext.class);
69 servlet.init(config);
70 MessageDispatcher messageDispatcher = (MessageDispatcher) servlet.getMessageReceiver();
71 Assert.assertNotNull("No messageDispatcher created", messageDispatcher);
72 }
73
74 @Test
75 public void testDetectedStrategies() throws ServletException {
76 servlet.setContextClass(DetectWebApplicationContext.class);
77 servlet.init(config);
78 MessageDispatcher messageDispatcher = (MessageDispatcher) servlet.getMessageReceiver();
79 Assert.assertNotNull("No messageDispatcher created", messageDispatcher);
80 assertStrategies(PayloadRootQNameEndpointMapping.class, messageDispatcher.getEndpointMappings());
81 assertStrategies(PayloadEndpointAdapter.class, messageDispatcher.getEndpointAdapters());
82 assertStrategies(SimpleSoapExceptionResolver.class, messageDispatcher.getEndpointExceptionResolvers());
83 }
84
85 @Test
86 public void testDetectWsdlDefinitions() throws Exception {
87 servlet.setContextClass(WsdlDefinitionWebApplicationContext.class);
88 servlet.init(config);
89 MockHttpServletRequest request =
90 new MockHttpServletRequest(HttpTransportConstants.METHOD_GET, "/definition.wsdl");
91 MockHttpServletResponse response = new MockHttpServletResponse();
92 servlet.service(request, response);
93 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
94 documentBuilderFactory.setNamespaceAware(true);
95 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
96 Document result = documentBuilder.parse(new ByteArrayInputStream(response.getContentAsByteArray()));
97 Document expected = documentBuilder.parse(getClass().getResourceAsStream("wsdl11-input.wsdl"));
98 XMLUnit.setIgnoreWhitespace(true);
99 assertXMLEqual("Invalid WSDL written", expected, result);
100 }
101
102 private static class DetectWebApplicationContext extends StaticWebApplicationContext {
103
104 @Override
105 public void refresh() throws BeansException, IllegalStateException {
106 registerSingleton("payloadMapping", PayloadRootQNameEndpointMapping.class);
107 registerSingleton("payloadAdapter", PayloadEndpointAdapter.class);
108 registerSingleton("simpleExceptionResolver", SimpleSoapExceptionResolver.class);
109 super.refresh();
110 }
111 }
112
113 private static class WsdlDefinitionWebApplicationContext extends StaticWebApplicationContext {
114
115 @Override
116 public void refresh() throws BeansException, IllegalStateException {
117 MutablePropertyValues mpv = new MutablePropertyValues();
118 mpv.addPropertyValue("wsdl", new ClassPathResource("wsdl11-input.wsdl", getClass()));
119 registerSingleton("definition", SimpleWsdl11Definition.class, mpv);
120 super.refresh();
121 }
122 }
123 }