1   /*
2       This file is part of quExec.
3   
4       quExec is free software; you can redistribute it and/or modify
5       it under the terms of the GNU Lesser General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8   
9       quExec is distributed in the hope that it will be useful,
10      but WITHOUT ANY WARRANTY; without even the implied warranty of
11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12      GNU Lesser General Public License for more details.
13  
14      You should have received a copy of the GNU Lesser General Public License
15      along with quExec; if not, write to the Free Software
16      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18  
19  package net.sourceforge.quexec.jms;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.util.Collections;
24  import java.util.SortedSet;
25  import java.util.TreeSet;
26  import java.util.concurrent.ConcurrentSkipListSet;
27  
28  import javax.annotation.Resource;
29  import javax.jms.Destination;
30  import javax.jms.JMSException;
31  import javax.jms.Message;
32  import javax.jms.Queue;
33  import javax.jms.Session;
34  
35  import net.sourceforge.quexec.testutil.JmsTestUtils;
36  
37  import org.junit.Test;
38  import org.springframework.jms.core.JmsTemplate;
39  import org.springframework.jms.core.MessageCreator;
40  import org.springframework.jms.listener.DefaultMessageListenerContainer;
41  import org.springframework.jms.listener.adapter.MessageListenerAdapter;
42  import org.springframework.test.context.ContextConfiguration;
43  
44  
45  /**
46   * Test the correct usage of the Spring-based JMS infrastructure code.
47   * 
48   * @author schickin
49   *
50   */
51  @ContextConfiguration
52  public class JmsEnvironmentTest extends AbstractJmsBasedTest {
53  	
54  //	private static final Log log = LogFactory.getLog(JmsEnvironmentTest.class);
55  
56  	private final String msg = "Hello!";
57  	
58  	@Resource
59  	JmsTemplate jmsTempl;
60  	
61  	@Resource
62  	DefaultMessageListenerContainer jmsCont;
63  
64  	@Test
65  	public void sendAndReceiveViaJmsTemplate() {
66  		Destination dest = JmsTestUtils.getDynamicQueue(jmsTempl);
67  
68  		sendViaCommandSender(dest, msg);
69  		Object result = jmsTempl.receiveAndConvert(dest);
70  		assertEquals(msg, result);
71  		
72  		final int numMessages = 10;
73  		for (int i = 0; i < numMessages; i++) {
74  			sendViaCommandSender(dest, msg + i);
75  		}
76  		for (int i = 0; i < numMessages; i++) {
77  			assertEquals(msg + i, jmsTempl.receiveAndConvert(dest));
78  		}
79  	}
80  
81  	
82  	/**
83  	 * Helper class for collecting test messages which are received in parallel
84  	 */
85  	private static class TextMessageRecorder {
86  		private final SortedSet<String> receivedMsgs =
87  			new ConcurrentSkipListSet<String>();
88  		
89  		public void handleMessage(String msg) {
90  			receivedMsgs.add(msg);
91  		}
92  		
93  		public SortedSet<String> getReceivedMsgs() {
94  			return Collections.unmodifiableSortedSet(receivedMsgs);
95  		}
96  	}
97  
98  	@Test
99  	public void sendAndReceiveInJmsContainer() throws InterruptedException {
100 		Queue dest = JmsTestUtils.getDynamicQueue(jmsTempl);
101 
102 		final int numMessages = 10;
103 		SortedSet<String> sentMsgs = new TreeSet<String>(); 
104 		for (int i = 0; i < numMessages; i++) {
105 			sendViaCommandSender(dest, msg + i);
106 			sentMsgs.add(msg + i);
107 		}
108 
109 		TextMessageRecorder recorder = new TextMessageRecorder();
110 		jmsCont.setMessageListener(new MessageListenerAdapter(recorder));
111 		
112 		jmsCont.setDestination(dest);
113 		jmsCont.initialize();
114 		jmsCont.start();
115 
116 		JmsTestUtils.waitUntilQueueEmpty(jmsTempl, dest);
117 		
118 		jmsCont.stop();
119 		jmsCont.shutdown();
120 		
121 		assertEquals(sentMsgs, recorder.getReceivedMsgs());
122 	}
123 
124 	private void sendViaCommandSender(Destination dest, final String textMsg) {
125 		jmsTempl.send(dest, new MessageCreator() {
126 			public Message createMessage(Session session) throws JMSException {
127 				Message m = session.createTextMessage(textMsg);
128 				return m;
129 			}
130 		});
131 		
132 	}
133 }