1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.jms.integration;
12
13 import org.mule.api.config.MuleProperties;
14 import org.mule.message.ExceptionMessage;
15
16 import javax.jms.BytesMessage;
17 import javax.jms.JMSException;
18 import javax.jms.Message;
19 import javax.jms.MessageConsumer;
20 import javax.jms.ObjectMessage;
21 import javax.jms.Session;
22 import javax.jms.TextMessage;
23
24 import org.apache.commons.lang.SerializationUtils;
25 import org.junit.Test;
26
27
28
29
30 public class JmsExceptionStrategyTestCase extends AbstractJmsFunctionalTestCase
31 {
32 public static final String DEADLETTER_QUEUE_NAME = "dlq";
33
34 protected String getConfigResources()
35 {
36 return "integration/jms-exception-strategy.xml";
37 }
38
39 @Test
40 public void testTransactedRedeliveryToDLDestination() throws Exception
41 {
42 send(scenarioDeadLetter);
43
44 receive(scenarioNotReceive);
45
46 receive(scenarioDeadLetter);
47 }
48
49 @Test
50 public void testTransactedRedeliveryToDLDestinationRollback() throws Exception
51 {
52 send(scenarioDeadLetter);
53
54 receive(scenarioDeadLetterRollback);
55
56 receive(scenarioDeadLetter);
57
58 receive(scenarioDeadLetterNotReceive);
59 }
60
61 Scenario scenarioDeadLetter = new ScenarioDeadLetter();
62
63 class ScenarioDeadLetter extends ScenarioCommit
64 {
65
66 public String getOutputDestinationName()
67 {
68 return DEADLETTER_QUEUE_NAME;
69 }
70
71
72 public Message receive(Session session, MessageConsumer consumer) throws JMSException
73 {
74
75 Message message = consumer.receive(getTimeout());
76 assertNotNull(message);
77
78 Object obj = null;
79
80 if (message instanceof BytesMessage)
81 {
82 byte[] messageBytes = new byte[(int) ((BytesMessage) message).getBodyLength()];
83 ((BytesMessage) message).readBytes(messageBytes);
84 obj = SerializationUtils.deserialize(messageBytes);
85 }
86
87 else if (message instanceof ObjectMessage)
88 {
89 obj = ((ObjectMessage) message).getObject();
90 }
91 else
92 {
93 fail("Message is an unexpected type: " + message.getClass().getName());
94 }
95 assertTrue(obj instanceof ExceptionMessage);
96
97
98
99
100 Object payload = ((ExceptionMessage) obj).getPayload();
101
102 if (payload instanceof TextMessage)
103 {
104 assertEquals(DEFAULT_INPUT_MESSAGE, ((TextMessage) payload).getText());
105 }
106
107
108 else if (payload instanceof String)
109 {
110 assertEquals(DEFAULT_INPUT_MESSAGE, payload);
111 }
112 else
113 {
114 fail("Payload is an unexpected type: " + payload.getClass().getName());
115 }
116
117 String dest = message.getStringProperty(MuleProperties.MULE_ENDPOINT_PROPERTY);
118
119 if (dest != null)
120 {
121 assertEquals("jms://" + DEADLETTER_QUEUE_NAME, dest);
122 }
123
124 applyTransaction(session);
125 return message;
126 }
127 }
128
129 Scenario scenarioDeadLetterRollback = new ScenarioDeadLetterRollback();
130
131 class ScenarioDeadLetterRollback extends ScenarioDeadLetter
132 {
133
134 protected void applyTransaction(Session session) throws JMSException
135 {
136 session.rollback();
137 }
138 }
139
140 Scenario scenarioDeadLetterNotReceive = new ScenarioDeadLetterNotReceive();
141
142 class ScenarioDeadLetterNotReceive extends ScenarioDeadLetter
143 {
144
145 public Message receive(Session session, MessageConsumer consumer) throws JMSException
146 {
147 Message message = consumer.receive(getSmallTimeout());
148 assertNull(message);
149 return message;
150 }
151 }
152 }