1   /*
2    * Copyright 2007 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.jms.core.JmsTemplate;
30  import org.springframework.jms.core.MessageCreator;
31  import org.springframework.jms.core.MessagePostProcessor;
32  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
33  import org.springframework.ws.soap.SoapMessage;
34  import org.springframework.ws.soap.SoapVersion;
35  import org.springframework.ws.soap.saaj.SaajSoapMessage;
36  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
37  import org.springframework.ws.transport.WebServiceConnection;
38  
39  public class JmsMessageSenderIntegrationTest extends AbstractDependencyInjectionSpringContextTests {
40  
41      private JmsMessageSender messageSender;
42  
43      private JmsTemplate jmsTemplate;
44  
45      private MessageFactory messageFactory;
46  
47      private static final String SOAP_ACTION = "\"http://springframework.org/DoIt\"";
48  
49      protected void onSetUp() throws Exception {
50          messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
51      }
52  
53      protected String[] getConfigLocations() {
54          return new String[]{"classpath:org/springframework/ws/transport/jms/jms-sender-applicationContext.xml"};
55      }
56  
57      public void setJmsTemplate(JmsTemplate jmsTemplate) {
58          this.jmsTemplate = jmsTemplate;
59      }
60  
61      public void setMessageSender(JmsMessageSender messageSender) {
62          this.messageSender = messageSender;
63      }
64  
65      public void testSendAndReceiveQueueBytesMessage() throws Exception {
66          WebServiceConnection connection = null;
67          try {
68              URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT");
69              connection = messageSender.createConnection(uri);
70              SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
71              soapRequest.setSoapAction(SOAP_ACTION);
72              connection.send(soapRequest);
73  
74              BytesMessage request = (BytesMessage) jmsTemplate.receive();
75              assertNotNull("No message received", request);
76              assertTrue("No message content received", request.readByte() != -1);
77              ByteArrayOutputStream bos = new ByteArrayOutputStream();
78              messageFactory.createMessage().writeTo(bos);
79              final byte[] buf = bos.toByteArray();
80              jmsTemplate.send(request.getJMSReplyTo(), new MessageCreator() {
81  
82                  public Message createMessage(Session session) throws JMSException {
83                      BytesMessage response = session.createBytesMessage();
84                      response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
85                      response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE,
86                              SoapVersion.SOAP_11.getContentType());
87                      response.writeBytes(buf);
88                      return response;
89                  }
90              });
91              SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
92              assertNotNull("No response received", response);
93              assertEquals("Invalid SOAPAction", SOAP_ACTION, response.getSoapAction());
94              assertFalse("Message is fault", response.hasFault());
95          }
96          finally {
97              if (connection != null) {
98                  connection.close();
99              }
100         }
101     }
102 
103     public void testSendAndReceiveQueueTextMessage() throws Exception {
104         WebServiceConnection connection = null;
105         try {
106             URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT&messageType=TEXT_MESSAGE");
107             connection = messageSender.createConnection(uri);
108             SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
109             soapRequest.setSoapAction(SOAP_ACTION);
110             connection.send(soapRequest);
111 
112             TextMessage request = (TextMessage) jmsTemplate.receive();
113             assertNotNull("No message received", request);
114             assertNotNull("No message content received", request.getText());
115             ByteArrayOutputStream bos = new ByteArrayOutputStream();
116             messageFactory.createMessage().writeTo(bos);
117             final String text = new String(bos.toByteArray(), "UTF-8");
118             jmsTemplate.send(request.getJMSReplyTo(), new MessageCreator() {
119 
120                 public Message createMessage(Session session) throws JMSException {
121                     TextMessage response = session.createTextMessage();
122                     response.setStringProperty(JmsTransportConstants.PROPERTY_SOAP_ACTION, SOAP_ACTION);
123                     response.setStringProperty(JmsTransportConstants.PROPERTY_CONTENT_TYPE,
124                             SoapVersion.SOAP_11.getContentType());
125                     response.setText(text);
126                     return response;
127                 }
128             });
129             SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
130             assertNotNull("No response received", response);
131             assertEquals("Invalid SOAPAction", SOAP_ACTION, response.getSoapAction());
132             assertFalse("Message is fault", response.hasFault());
133         }
134         finally {
135             if (connection != null) {
136                 connection.close();
137             }
138         }
139     }
140 
141     public void testSendNoResponse() throws Exception {
142         WebServiceConnection connection = null;
143         try {
144             URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT");
145             connection = messageSender.createConnection(uri);
146             SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
147             soapRequest.setSoapAction(SOAP_ACTION);
148             connection.send(soapRequest);
149 
150             BytesMessage request = (BytesMessage) jmsTemplate.receive();
151             assertNotNull("No message received", request);
152             ByteArrayOutputStream bos = new ByteArrayOutputStream();
153             messageFactory.createMessage().writeTo(bos);
154             SoapMessage response = (SoapMessage) connection.receive(new SaajSoapMessageFactory(messageFactory));
155             assertNull("Response received", response);
156         }
157         finally {
158             if (connection != null) {
159                 connection.close();
160             }
161         }
162     }
163 
164     public void testPostProcessor() throws Exception {
165         MessagePostProcessor processor = new MessagePostProcessor() {
166             public Message postProcessMessage(Message message) throws JMSException {
167                 message.setBooleanProperty("processed", true);
168                 return message;
169             }
170         };
171         JmsSenderConnection connection = null;
172         try {
173             URI uri = new URI("jms:SenderRequestQueue?deliveryMode=NON_PERSISTENT");
174             connection = (JmsSenderConnection) messageSender.createConnection(uri);
175             connection.setPostProcessor(processor);
176             SoapMessage soapRequest = new SaajSoapMessage(messageFactory.createMessage());
177             connection.send(soapRequest);
178 
179             BytesMessage request = (BytesMessage) jmsTemplate.receive();
180             assertNotNull("No message received", request);
181             assertTrue("Message not processed", request.getBooleanProperty("processed"));
182         }
183         finally {
184             if (connection != null) {
185                 connection.close();
186             }
187         }
188 
189     }
190 }