1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.core.messaging;
16
17 import org.figure8.join.util.SpringTestCase;
18
19 import javax.jms.TextMessage;
20 import javax.jms.ObjectMessage;
21
22 import java.util.List;
23 import java.util.Properties;
24 /**
25 * JUnit test case for testing simple JMS message production and consumption.
26 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
27 * @version $Revision: 1.2 $
28 */
29 public class ProducerConsumerTest extends SpringTestCase{
30
31
32
33 /** Spring configuration files */
34 private String[] configLocations = new String[]{"classpath:/org/figure8/join/core/messaging/spring.xml"};
35
36
37
38
39 /** JMS messages producer */
40 protected JMSProducerBean producer = null;
41 /** JMS messages consumer (echoes messages) */
42 protected EchoJMSConsumerBean consumer = null;
43
44
45
46
47 /** Retrieve producer and consumer after having initialized context. */
48 public void setUp(){
49 super.setUp();
50
51 producer = (JMSProducerBean)context.getBean("simpleProducer");
52
53 consumer = (EchoJMSConsumerBean)context.getBean("echoConsumer");
54 }
55
56
57
58
59 /** Test the production and the consumption of text messages. */
60 public void testProductionAndConsumptionOfTextMessages(){
61
62 assertNotNull("Found a valid producer", producer);
63 assertNotNull("Found a valid consumer", consumer);
64 consumer.flushMessages();
65
66 for (int i=0; i<10; i++){
67 String text = "Hello World ! (" + i + ")";
68 producer.sendTextMessage(text);
69 }
70
71 consumer.waitForMessagesToArrive(10);
72 List messages = consumer.flushMessages();
73 assertEquals("Message count is correct", 10, messages.size());
74
75 for (int i=0; i<messages.size(); i++){
76 try{
77 TextMessage msg = (TextMessage)messages.get(i);
78 assertEquals("Message content is correct", "Hello World ! (" + i + ")", msg.getText());
79 }
80 catch (Exception e){
81 fail("Exception while checking message content");
82 }
83 }
84 }
85
86 /** Test the production and the consumption of text messages with properties. */
87 public void testProductionAndConsumptionOfTextMessagesWithProperties(){
88
89 assertNotNull("Found a valid producer", producer);
90 assertNotNull("Found a valid consumer", consumer);
91 consumer.flushMessages();
92
93 Properties props = new Properties();
94 for (int i=0; i<10; i++){
95 String text = "Hello World ! (" + i + ")";
96 props.setProperty("counter", String.valueOf(i));
97 producer.sendTextMessage(text, props);
98 }
99
100 consumer.waitForMessagesToArrive(10);
101 List messages = consumer.flushMessages();
102 assertEquals("Message count is correct", 10, messages.size());
103
104 for (int i=0; i<messages.size(); i++){
105 try{
106 TextMessage msg = (TextMessage)messages.get(i);
107 assertEquals("Message content is correct", "Hello World ! (" + i + ")", msg.getText());
108 assertEquals("Message property is correct", String.valueOf(i), msg.getStringProperty("counter"));
109 }
110 catch (Exception e){
111 fail("Exception while checking message content");
112 }
113 }
114 }
115
116 /** Test the production and consumption of object messages */
117 public void testProductionAndConsumptionOfObjectMessages(){
118
119 assertNotNull("Found a valid producer", producer);
120 assertNotNull("Found a valid consumer", consumer);
121 consumer.flushMessages();
122
123 for (int i=0; i<10; i++){
124 String text = "Hello World ! (" + i + ")";
125 producer.sendObjectMessage(text);
126 }
127
128 consumer.waitForMessagesToArrive(10);
129 List messages = consumer.flushMessages();
130 assertEquals("Message count is correct", 10, messages.size());
131
132 for (int i=0; i<messages.size(); i++){
133 try{
134 ObjectMessage msg = (ObjectMessage)messages.get(i);
135 assertEquals("Message content is correct", "Hello World ! (" + i + ")", msg.getObject());
136 }
137 catch (Exception e){
138 fail("Exception while checking message content");
139 }
140 }
141 }
142
143 /** Test the production and the consumption of object messages with properties. */
144 public void testProductionAndConsumptionOfObjectMessagesWithProperties(){
145
146 assertNotNull("Found a valid producer", producer);
147 assertNotNull("Found a valid consumer", consumer);
148 consumer.flushMessages();
149
150 Properties props = new Properties();
151 for (int i=0; i<10; i++){
152 String text = "Hello World ! (" + i + ")";
153 props.setProperty("counter", String.valueOf(i));
154 producer.sendObjectMessage(text, props);
155 }
156
157 consumer.waitForMessagesToArrive(10);
158 List messages = consumer.flushMessages();
159 assertEquals("Message count is correct", 10, messages.size());
160
161 for (int i=0; i<messages.size(); i++){
162 try{
163 ObjectMessage msg = (ObjectMessage)messages.get(i);
164 assertEquals("Message content is correct", "Hello World ! (" + i + ")", msg.getObject());
165 assertEquals("Message property is correct", String.valueOf(i), msg.getStringProperty("counter"));
166 }
167 catch (Exception e){
168 fail("Exception while checking message content");
169 }
170 }
171 }
172
173
174
175
176 /** @return configLocations inner field */
177 public String[] getConfigLocations(){
178 return configLocations;
179 }
180 }