Skip to content

Commit 04c01ea

Browse files
committed
Fix for fmtn#22 - JS transformer not working with copy/move during all conditions
1 parent c5c4f1e commit 04c01ea

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

src/main/java/co/nordlander/a/A.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ protected void executeCommandLine(CommandLine cmdLine) throws Exception{
242242
}
243243

244244
protected void executeMove(CommandLine cmdLine) throws JMSException,
245-
UnsupportedEncodingException, IOException {
245+
UnsupportedEncodingException, ScriptException, IOException {
246246

247247
// Should be able to support some kind of Move operation even though the session is not transacted.
248248
boolean hasTransactionalSession = tsess != null;
@@ -266,7 +266,7 @@ protected void executeMove(CommandLine cmdLine) throws JMSException,
266266
if (msg == null) {
267267
break;
268268
} else {
269-
mp.send(msg);
269+
sendWithOptionalTransformer(cmdLine, msg, mp);
270270
if( hasTransactionalSession ){
271271
moveSession.commit();
272272
}
@@ -303,17 +303,12 @@ protected void executeCopy(CommandLine cmdLine) throws JMSException, ScriptExcep
303303
String haystack = ((TextMessage) msg).getText();
304304
String needle = cmdLine.getOptionValue(CMD_FIND);
305305
if (haystack != null && haystack.contains(needle)) {
306-
if( cmdLine.hasOption(CMD_TRANSFORM_SCRIPT) ) {
307-
mp.send(transformMessage(msg,cmdLine.getOptionValue(CMD_TRANSFORM_SCRIPT)));
308-
} else {
309-
mp.send(msg);
310-
}
311-
306+
sendWithOptionalTransformer(cmdLine, msg, mp);
312307
++j;
313308
}
314309
}
315310
} else {
316-
mp.send(msg);
311+
sendWithOptionalTransformer(cmdLine, msg, mp);
317312
++j;
318313
}
319314
++i;
@@ -323,6 +318,15 @@ protected void executeCopy(CommandLine cmdLine) throws JMSException, ScriptExcep
323318
" to ", cmdLine.getArgs()[0]);
324319
}
325320

321+
protected void sendWithOptionalTransformer(CommandLine cmdLine, Message msg, MessageProducer mp) throws JMSException, ScriptException, IOException {
322+
if( cmdLine.hasOption(CMD_TRANSFORM_SCRIPT) ) {
323+
mp.send(transformMessage(msg, cmdLine.getOptionValue(CMD_TRANSFORM_SCRIPT)));
324+
} else {
325+
mp.send(msg);
326+
}
327+
328+
}
329+
326330
protected void connect(String url, String user, String password,
327331
Protocol protocol, String jndi, boolean noTransactionSupport) throws Exception {
328332
if (StringUtils.isBlank(jndi)) {

src/test/java/co/nordlander/a/BaseTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ public void testCopyQueue() throws Exception{
269269
assertNull(msg);
270270
}
271271

272+
/**
273+
* Test that transforming messages with copy works.
274+
* @throws Exception
275+
*/
276+
@Test
277+
public void testCopyQueueWithTransformer() throws Exception {
278+
final String script = "\"msg.stringProperties.put('changeme','new');\"";
279+
final String cmdLine = getConnectCommand() + "-" + CMD_COPY_QUEUE + " SOURCE.QUEUE -" + CMD_TRANSFORM_SCRIPT + " " + script + " TARGET.QUEUE";
280+
281+
MessageProducer mp = session.createProducer(sourceQueue);
282+
mp.send(testMessage);
283+
a.run(cmdLine.split(" "));
284+
// Verify messages are moved to target queue
285+
MessageConsumer mc = session.createConsumer(targetQueue);
286+
TextMessage msg = (TextMessage)mc.receive(TEST_TIMEOUT);
287+
assertNotNull(msg);
288+
assertEquals("new", msg.getStringProperty("changeme"));
289+
}
290+
272291
/**
273292
* Test that all messages are moved from one queue to the other.
274293
* @throws Exception
@@ -293,6 +312,26 @@ public void testMoveQueue() throws Exception{
293312
assertNotNull(msg);
294313
msg = (TextMessage)mc.receive(TEST_TIMEOUT);
295314
assertNull(msg);
315+
316+
}
317+
318+
/**
319+
* Test that all messages are moved from one queue to the other.
320+
* @throws Exception
321+
*/
322+
@Test
323+
public void testMoveQueueWithTransformer() throws Exception{
324+
final String script = "\"msg.stringProperties.put('changeme','new');\"";
325+
final String cmdLine = getConnectCommand() + "-" + CMD_MOVE_QUEUE + " SOURCE.QUEUE -" + CMD_TRANSFORM_SCRIPT + " " + script + " TARGET.QUEUE";
326+
MessageProducer mp = session.createProducer(sourceQueue);
327+
mp.send(testMessage);
328+
a.run(cmdLine.split(" "));
329+
// Verify messages are moved to target queue with changed property
330+
MessageConsumer mc = session.createConsumer(targetQueue);
331+
TextMessage msg = (TextMessage)mc.receive(TEST_TIMEOUT);
332+
assertNotNull(msg);
333+
assertEquals("new", msg.getStringProperty("changeme"));
334+
296335
}
297336

298337
/**

0 commit comments

Comments
 (0)