1
2
3
4
5
6
7
8
9
10 package org.mule.transport.jms.integration;
11
12 import org.mule.api.MuleMessage;
13 import org.mule.module.client.MuleClient;
14 import org.mule.tck.FunctionalTestCase;
15
16 import javax.jms.Connection;
17 import javax.jms.ConnectionFactory;
18 import javax.jms.JMSException;
19 import javax.jms.Message;
20 import javax.jms.MessageConsumer;
21 import javax.jms.MessageProducer;
22 import javax.jms.Session;
23 import javax.jms.TextMessage;
24 import javax.transaction.HeuristicMixedException;
25 import javax.transaction.HeuristicRollbackException;
26 import javax.transaction.RollbackException;
27 import javax.transaction.SystemException;
28
29 import org.apache.activemq.ActiveMQConnectionFactory;
30 import org.apache.activemq.command.ActiveMQQueue;
31
32
33
34
35 public abstract class AbstractJmsFunctionalTestCase extends FunctionalTestCase
36 {
37 public static final String DEFAULT_BROKER_URL = "vm://localhost?broker.persistent=false&broker.useJmx=false";
38 public static final String DEFAULT_INPUT_MESSAGE = "INPUT MESSAGE";
39 public static final String DEFAULT_OUTPUT_MESSAGE = "OUTPUT MESSAGE";
40 public static final String DEFAULT_INPUT_MQ_QUEUE_NAME = "in";
41 public static final String DEFAULT_INPUT_MULE_QUEUE_NAME = "jms://" + DEFAULT_INPUT_MQ_QUEUE_NAME;
42 public static final String DEFAULT_OUTPUT_MQ_QUEUE_NAME = "out";
43 public static final String DEFAULT_OUTPUT_MULE_QUEUE_NAME = "jms://" + DEFAULT_OUTPUT_MQ_QUEUE_NAME;
44 public static final long TIMEOUT = 5000;
45 public static final long SMALL_TIMEOUT = 1000;
46 public static final long LOCK_WAIT = 1000;
47 public static final String CONNECTOR_NAME = "MuleMQConnector";
48 private MuleClient client;
49
50 protected void dispatchMessage() throws Exception
51 {
52 client.dispatch(DEFAULT_INPUT_MULE_QUEUE_NAME, DEFAULT_INPUT_MESSAGE, null);
53 }
54
55 protected void dispatchMessage(Object payload) throws Exception
56 {
57 client.dispatch(DEFAULT_INPUT_MULE_QUEUE_NAME, payload, null);
58 }
59
60 protected MuleMessage receiveMessage() throws Exception
61 {
62 MuleMessage result = client.request(DEFAULT_OUTPUT_MULE_QUEUE_NAME, TIMEOUT);
63 assertNotNull(result);
64 assertNotNull(result.getPayload());
65 assertNull(result.getExceptionPayload());
66 assertEquals(DEFAULT_OUTPUT_MESSAGE, result.getPayload());
67 return result;
68
69 }
70
71 public void runAsynchronousDispatching() throws Exception
72 {
73 dispatchMessage();
74 receiveMessage();
75 MuleMessage result = client.request(DEFAULT_OUTPUT_MULE_QUEUE_NAME, SMALL_TIMEOUT);
76 assertNull(result);
77 }
78
79 protected void doSetUp() throws Exception
80 {
81 super.doSetUp();
82 client = new MuleClient();
83 }
84
85 protected void doTearDown() throws Exception
86 {
87 super.doTearDown();
88 client.dispose();
89 }
90
91 protected MuleClient getClient()
92 {
93 return client;
94 }
95
96 public void send(Scenario scenario) throws Exception
97 {
98 Connection connection = null;
99 try
100 {
101 ConnectionFactory factory = new ActiveMQConnectionFactory(scenario.getBrokerUrl());
102 connection = factory.createConnection();
103 connection.start();
104 Session session = null;
105 try
106 {
107 session = connection.createSession(scenario.isTransacted(), scenario.getAcknowledge());
108 ActiveMQQueue destination = new ActiveMQQueue(scenario.getInputQueue());
109 MessageProducer producer = null;
110 try
111 {
112 producer = session.createProducer(destination);
113 scenario.send(session, producer);
114 }
115 finally
116 {
117 if (producer != null)
118 {
119 producer.close();
120 }
121 }
122 }
123 finally
124 {
125 if (session != null)
126 {
127 session.close();
128 }
129 }
130 }
131 finally
132 {
133 if (connection != null)
134 {
135 connection.close();
136 }
137 }
138 }
139
140 public Message receive(Scenario scenario) throws Exception
141 {
142 Connection connection = null;
143 try
144 {
145 ConnectionFactory factory = new ActiveMQConnectionFactory(scenario.getBrokerUrl());
146 connection = factory.createConnection();
147 connection.start();
148 Session session = null;
149 try
150 {
151 session = connection.createSession(scenario.isTransacted(), scenario.getAcknowledge());
152 ActiveMQQueue destination = new ActiveMQQueue(scenario.getOutputQueue());
153 MessageConsumer consumer = null;
154 try
155 {
156 consumer = session.createConsumer(destination);
157 return scenario.receive(session, consumer);
158 }
159 finally
160 {
161 if (consumer != null)
162 {
163 consumer.close();
164 }
165 }
166 }
167 finally
168 {
169 if (session != null)
170 {
171 session.close();
172 }
173 }
174 }
175 finally
176 {
177 if (connection != null)
178 {
179 connection.close();
180 }
181 }
182 }
183
184
185
186
187
188 interface Scenario
189 {
190 String getBrokerUrl();
191
192 String getInputQueue();
193
194 void setInputQueue(String inputQueue);
195
196 String getOutputQueue();
197
198 void setOutputQueue(String outputQueue);
199
200 int getAcknowledge();
201
202 void send(Session session, MessageProducer producer) throws JMSException, SystemException, HeuristicMixedException, HeuristicRollbackException, RollbackException;
203
204 Message receive(Session session, MessageConsumer consumer) throws JMSException, SystemException, HeuristicMixedException, HeuristicRollbackException, RollbackException;
205
206 boolean isTransacted();
207 }
208
209 abstract class AbstractScenario implements Scenario
210 {
211 private String inputQueue = DEFAULT_INPUT_MQ_QUEUE_NAME;
212 private String outputQueue = DEFAULT_OUTPUT_MQ_QUEUE_NAME;
213
214 public String getBrokerUrl()
215 {
216 return DEFAULT_BROKER_URL;
217 }
218
219 public String getInputQueue()
220 {
221 return inputQueue;
222 }
223
224 public String getOutputQueue()
225 {
226 return outputQueue;
227 }
228
229 public void setInputQueue(String inputQueue)
230 {
231 this.inputQueue = inputQueue;
232 }
233
234 public void setOutputQueue(String outputQueue)
235 {
236 this.outputQueue = outputQueue;
237 }
238
239 public int getAcknowledge()
240 {
241 return Session.AUTO_ACKNOWLEDGE;
242 }
243
244 public void send(Session session, MessageProducer producer) throws JMSException
245 {
246 producer.send(session.createTextMessage(DEFAULT_INPUT_MESSAGE));
247 applyTransaction(session);
248 }
249
250 public Message receive(Session session, MessageConsumer consumer) throws JMSException
251 {
252 Message message = consumer.receive(TIMEOUT);
253 assertNotNull(message);
254 assertTrue(TextMessage.class.isAssignableFrom(message.getClass()));
255 assertEquals(DEFAULT_OUTPUT_MESSAGE, ((TextMessage) message).getText());
256 applyTransaction(session);
257 return message;
258 }
259
260 abstract protected void applyTransaction(Session session) throws JMSException;
261 }
262
263 protected Scenario scenarioNoTx = new NonTransactedScenario();
264 class NonTransactedScenario extends AbstractScenario
265 {
266 public boolean isTransacted()
267 {
268 return false;
269 }
270
271 protected void applyTransaction(Session session) throws JMSException
272 {
273
274 }
275 }
276
277 protected Scenario scenarioCommit = new ScenarioCommit();
278 class ScenarioCommit extends AbstractScenario
279 {
280 public boolean isTransacted()
281 {
282 return true;
283 }
284
285 protected void applyTransaction(Session session) throws JMSException
286 {
287 session.commit();
288 }
289 }
290
291 protected Scenario scenarioRollback = new ScenarioRollback();
292 class ScenarioRollback extends AbstractScenario
293 {
294 public boolean isTransacted()
295 {
296 return true;
297 }
298
299 protected void applyTransaction(Session session) throws JMSException
300 {
301 session.rollback();
302 }
303 }
304
305 protected Scenario scenarioNotReceive = new ScenarioNotReceive();
306 class ScenarioNotReceive extends NonTransactedScenario
307 {
308
309 public Message receive(Session session, MessageConsumer consumer) throws JMSException
310 {
311 Message message = consumer.receive(SMALL_TIMEOUT);
312 assertNull(message);
313 return message;
314 }
315 }
316 }