1   /*
2    * Copyright 2005-2012 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.jms;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.net.URI;
21  import javax.jms.BytesMessage;
22  import javax.jms.JMSException;
23  import javax.jms.Message;
24  import javax.jms.Session;
25  import javax.jms.TextMessage;
26  import javax.xml.soap.MessageFactory;
27  import javax.xml.soap.SOAPConstants;
28  
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.jms.core.JmsTemplate;
31  import org.springframework.jms.core.MessageCreator;
32  import org.springframework.jms.core.MessagePostProcessor;
33  import org.springframework.test.context.ContextConfiguration;
34  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35  import org.springframework.ws.soap.SoapMessage;
36  import org.springframework.ws.soap.SoapVersion;
37  import org.springframework.ws.soap.saaj.SaajSoapMessage;
38  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
39  import org.springframework.ws.transport.WebServiceConnection;
40  
41  import org.junit.Before;
42  import org.junit.Test;
43  import org.junit.runner.RunWith;
44  
45  import static org.junit.Assert.*;
46  
47  @RunWith(SpringJUnit4ClassRunner.class)
48  @ContextConfiguration("jms-sender-applicationContext.xml")
49  public class JmsMessageSenderIntegrationTest {
50  
51      @Autowired
52      private JmsMessageSender messageSender;
53  
54      @Autowired
55      private JmsTemplate jmsTemplate;
56  
57      private MessageFactory messageFactory;
58  
59      private static final String SOAP_ACTION = "\"http://springframework.org/DoIt\"";
60  
61      @Before
62      public void createMessageFactory() throws Exception {
63          messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
64      }
65  
66      @Test
67      public void testSendAndReceiveQueueBytesMessageTemporaryQueue() throws Exception {
68          WebServiceConnection connection = null;
69          try {
70              URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT");
71              connection = messageSender.createConnection(uri);
72              SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
73              soapRequest.setSoapAction(SOAP_ACTION);
74              connection.send(soapRequest);
75  
76              BytesMessage request = (BytesMessage) jmsTemplate.receive();
77              assertNotNull("No message received", request);
78              assertTrue("No message content received", request.readByte() != -1);
79              ByteArrayOutputStream bos = new ByteArrayOutputStream();
80              messageFactory.createMessage().writeTo(bos);
81              final byte[] buf = bos.toByteArray();
82              jmsTemplate.send(request.getJMSReplyTo(), new MessageCreator() {
83  
84                  public Message createMessage(Session session) throws JMSException {
85                      BytesMessage response = session.createBytesMessage();
86                      response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
87                      response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE,
88                              SoapVersion.SOAP_11.getContentType());
89                      response.writeBytes(buf);
90                      return response;
91                  }
92              });
93              SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
94              assertNotNull("No response received", response);
95              assertEquals("Invalid SOAPAction", SOAP_ACTION, response.getSoapAction());
96              assertFalse("Message is fault", response.hasFault());
97          }
98          finally {
99              if (connection != null) {
100                 connection.close();
101             }
102         }
103     }
104 
105     @Test
106     public void testSendAndReceiveQueueBytesMessagePermanentQueue() throws Exception {
107         WebServiceConnection connection = null;
108         try {
109             String responseQueueName = "SenderResponseQueue";
110             URI uri = new URI(
111                     "jms:SenderRequestQueue?replyToName=" + responseQueueName + "&deliveryMode=NON_PERSISTENT");
112             connection = messageSender.createConnection(uri);
113             SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
114             soapRequest.setSoapAction(SOAP_ACTION);
115             connection.send(soapRequest);
116 
117             final BytesMessage request = (BytesMessage) jmsTemplate.receive();
118             assertNotNull("No message received", request);
119             assertTrue("No message content received", request.readByte() != -1);
120             ByteArrayOutputStream bos = new ByteArrayOutputStream();
121             messageFactory.createMessage().writeTo(bos);
122             final byte[] buf = bos.toByteArray();
123             jmsTemplate.send(responseQueueName, new MessageCreator() {
124 
125                 public Message createMessage(Session session) throws JMSException {
126                     BytesMessage response = session.createBytesMessage();
127                     response.setJMSCorrelationID(request.getJMSMessageID());
128                     response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
129                     response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE,
130                             SoapVersion.SOAP_11.getContentType());
131                     response.writeBytes(buf);
132                     return response;
133                 }
134             });
135             SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
136             assertNotNull("No response received", response);
137             assertEquals("Invalid SOAPAction", SOAP_ACTION, response.getSoapAction());
138             assertFalse("Message is fault", response.hasFault());
139         }
140         finally {
141             if (connection != null) {
142                 connection.close();
143             }
144         }
145     }
146 
147     @Test
148     public void testSendAndReceiveQueueTextMessage() throws Exception {
149         WebServiceConnection connection = null;
150         try {
151             URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT&messageType=TEXT_MESSAGE");
152             connection = messageSender.createConnection(uri);
153             SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
154             soapRequest.setSoapAction(SOAP_ACTION);
155             connection.send(soapRequest);
156 
157             TextMessage request = (TextMessage) jmsTemplate.receive();
158             assertNotNull("No message received", request);
159             assertNotNull("No message content received", request.getText());
160             ByteArrayOutputStream bos = new ByteArrayOutputStream();
161             messageFactory.createMessage().writeTo(bos);
162             final String text = new String(bos.toByteArray(), "UTF-8");
163             jmsTemplate.send(request.getJMSReplyTo(), new MessageCreator() {
164 
165                 public Message createMessage(Session session) throws JMSException {
166                     TextMessage response = session.createTextMessage();
167                     response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
168                     response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE,
169                             SoapVersion.SOAP_11.getContentType());
170                     response.setText(text);
171                     return response;
172                 }
173             });
174             SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
175             assertNotNull("No response received", response);
176             assertEquals("Invalid SOAPAction", SOAP_ACTION, response.getSoapAction());
177             assertFalse("Message is fault", response.hasFault());
178         }
179         finally {
180             if (connection != null) {
181                 connection.close();
182             }
183         }
184     }
185 
186     @Test
187     public void testSendNoResponse() throws Exception {
188         WebServiceConnection connection = null;
189         try {
190             URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT");
191             connection = messageSender.createConnection(uri);
192             SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
193             soapRequest.setSoapAction(SOAP_ACTION);
194             connection.send(soapRequest);
195 
196             BytesMessage request = (BytesMessage) jmsTemplate.receive();
197             assertNotNull("No message received", request);
198             ByteArrayOutputStream bos = new ByteArrayOutputStream();
199             messageFactory.createMessage().writeTo(bos);
200             SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
201             assertNull("Response received", response);
202         }
203         finally {
204             if (connection != null) {
205                 connection.close();
206             }
207         }
208     }
209 
210     @Test
211     public void testPostProcessor() throws Exception {
212         MessagePostProcessor processor = new MessagePostProcessor() {
213             public Message postProcessMessage(Message message) throws JMSException {
214                 message.setBooleanProperty("processed", true);
215                 return message;
216             }
217         };
218         JmsSenderConnection connection = null;
219         try {
220             URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT");
221             connection = (JmsSenderConnection) messageSender.createConnection(uri);
222             connection.setPostProcessor(processor);
223             SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
224             connection.send(soapRequest);
225 
226             BytesMessage request = (BytesMessage) jmsTemplate.receive();
227             assertNotNull("No message received", request);
228             assertTrue("Message not processed", request.getBooleanProperty("processed"));
229         }
230         finally {
231             if (connection != null) {
232                 connection.close();
233             }
234         }
235 
236     }
237 }