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.jms;
18  
19  import javax.annotation.Resource;
20  import javax.jms.BytesMessage;
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  import javax.jms.Queue;
24  import javax.jms.Session;
25  import javax.jms.TextMessage;
26  import javax.jms.Topic;
27  
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.jms.core.JmsTemplate;
30  import org.springframework.jms.core.MessageCreator;
31  import org.springframework.test.context.ContextConfiguration;
32  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33  
34  import org.junit.Test;
35  import org.junit.runner.RunWith;
36  
37  import static org.junit.Assert.assertNotNull;
38  
39  @RunWith(SpringJUnit4ClassRunner.class)
40  @ContextConfiguration("jms-receiver-applicationContext.xml")
41  public class WebServiceMessageListenerIntegrationTest {
42  
43      private static final String CONTENT =
44              "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" + "<SOAP-ENV:Body>\n" +
45                      "<m:GetLastTradePrice xmlns:m='http://www.springframework.org/spring-ws'>\n" +
46                      "<symbol>DIS</symbol>\n" + "</m:GetLastTradePrice>\n" + "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
47  
48      @Autowired
49      private JmsTemplate jmsTemplate;
50  
51      @Resource
52      private Queue responseQueue;
53  
54      @Resource
55      private Queue requestQueue;
56  
57      @Autowired
58      private Topic requestTopic;
59  
60  
61      @Test
62      public void testReceiveQueueBytesMessage() throws Exception {
63          final byte[] b = CONTENT.getBytes("UTF-8");
64          jmsTemplate.send(requestQueue, new MessageCreator() {
65              public Message createMessage(Session session) throws JMSException {
66                  BytesMessage request = session.createBytesMessage();
67                  request.setJMSReplyTo(responseQueue);
68                  request.writeBytes(b);
69                  return request;
70              }
71          });
72          BytesMessage response = (BytesMessage) jmsTemplate.receive(responseQueue);
73          assertNotNull("No response received", response);
74      }
75  
76      @Test
77      public void testReceiveQueueTextMessage() throws Exception {
78          jmsTemplate.send(requestQueue, new MessageCreator() {
79              public Message createMessage(Session session) throws JMSException {
80                  TextMessage request = session.createTextMessage(CONTENT);
81                  request.setJMSReplyTo(responseQueue);
82                  return request;
83              }
84          });
85          TextMessage response = (TextMessage) jmsTemplate.receive(responseQueue);
86          assertNotNull("No response received", response);
87      }
88  
89      @Test
90      public void testReceiveTopic() throws Exception {
91          final byte[] b = CONTENT.getBytes("UTF-8");
92          jmsTemplate.send(requestTopic, new MessageCreator() {
93              public Message createMessage(Session session) throws JMSException {
94                  BytesMessage request = session.createBytesMessage();
95                  request.writeBytes(b);
96                  return request;
97              }
98          });
99          Thread.sleep(100);
100     }
101 
102 }