1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 }