1   /*
2    * Copyright 2005-2011 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.File;
20  import javax.xml.namespace.QName;
21  import javax.xml.soap.MessageFactory;
22  import javax.xml.soap.SOAPConnection;
23  import javax.xml.soap.SOAPConnectionFactory;
24  import javax.xml.soap.SOAPElement;
25  import javax.xml.soap.SOAPException;
26  import javax.xml.soap.SOAPMessage;
27  
28  import org.springframework.ws.transport.support.EchoPayloadEndpoint;
29  import org.springframework.ws.transport.support.FreePortScanner;
30  
31  import org.junit.AfterClass;
32  import org.junit.Before;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.mortbay.jetty.Server;
36  import org.mortbay.jetty.servlet.Context;
37  import org.mortbay.jetty.servlet.ServletHolder;
38  
39  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
40  
41  /**
42   * @author Arjen Poutsma
43   */
44  public class MessageDispatcherServletIntegrationTest {
45  
46      private static Server jettyServer;
47  
48      private static String url;
49  
50      private MessageFactory messageFactory;
51  
52      private SOAPConnectionFactory connectionFactory;
53  
54      @BeforeClass
55      public static void startJetty() throws Exception {
56          int port = FreePortScanner.getFreePort();
57          url = "http://localhost:" + port;
58          jettyServer = new Server(port);
59          Context jettyContext = new Context(jettyServer, "/");
60          String resourceBase =
61                  new File(MessageDispatcherServletIntegrationTest.class.getResource("WEB-INF").toURI()).getParent();
62          jettyContext.setResourceBase(resourceBase);
63          ServletHolder servletHolder = new ServletHolder(new MessageDispatcherServlet());
64          servletHolder.setName("sws");
65          jettyContext.addServlet(servletHolder, "/");
66          jettyServer.start();
67      }
68  
69      @Before
70      public void setUpSaaj() throws SOAPException {
71          messageFactory = MessageFactory.newInstance();
72          connectionFactory = SOAPConnectionFactory.newInstance();
73      }
74  
75      @AfterClass
76      public static void stopJetty() throws Exception {
77          if (jettyServer.isRunning()) {
78              jettyServer.stop();
79          }
80      }
81  
82      @Test
83      public void echo() throws SOAPException {
84          SOAPMessage request = messageFactory.createMessage();
85          SOAPElement element = request.getSOAPBody().addChildElement(new QName(EchoPayloadEndpoint.NAMESPACE, EchoPayloadEndpoint.LOCAL_PART));
86          element.setTextContent("Hello World");
87  
88          SOAPConnection connection = connectionFactory.createConnection();
89  
90          SOAPMessage response = connection.call(request, url);
91  
92          assertXMLEqual(request.getSOAPPart(), response.getSOAPPart());
93      }
94  
95  
96  }