1   /*
2    * $Id: JmsExceptionStrategyTestCase.java 10803 2008-02-14 13:31:25Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.jms.integration;
11  
12  import org.mule.api.config.MuleProperties;
13  import org.mule.message.ExceptionMessage;
14  
15  import javax.jms.JMSException;
16  import javax.jms.Message;
17  import javax.jms.MessageConsumer;
18  import javax.jms.ObjectMessage;
19  import javax.jms.Session;
20  
21  /**
22   * Tests a transactional exception strategy.
23   */
24  public class JmsExceptionStrategyTestCase extends AbstractJmsFunctionalTestCase
25  {
26      public static final String DEADLETTER_QUEUE_NAME = "dead.letter";
27      
28      protected String getConfigResources()
29      {
30          return "providers/activemq/jms-exception-strategy.xml";
31      }
32  
33      public void testTransactedRedeliveryToDLDestination() throws Exception
34      {
35          send(scenarioDeadLetter);
36          // Verify outbound message did _not_ get delivered.
37          receive(scenarioNotReceive);
38          // Verify message got sent to dead letter queue instead.
39          receive(scenarioDeadLetter);
40      }
41  
42      public void testTransactedRedeliveryToDLDestinationRollback() throws Exception
43      {
44          send(scenarioDeadLetter);
45          // Receive message but roll back transaction.
46          receive(scenarioDeadLetterRollback);
47          // Receive message again and commit transaction.
48          receive(scenarioDeadLetter);
49          // Verify there is no more message to receive.
50          receive(scenarioDeadLetterNotReceive);
51      }
52      
53      Scenario scenarioDeadLetter = new ScenarioDeadLetter();
54      class ScenarioDeadLetter extends ScenarioCommit
55      {
56          // @Override
57          public String getOutputQueue()
58          {
59              return DEADLETTER_QUEUE_NAME;
60          }
61  
62          // @Override
63          public Message receive(Session session, MessageConsumer consumer) throws JMSException
64          {
65              // Verify message got sent to dead letter queue.
66              Message message = consumer.receive(TIMEOUT);
67              assertNotNull(message);
68              assertTrue("Message should be ObjectMessage but is " + message.getClass(), message instanceof ObjectMessage);
69              Object obj = ((ObjectMessage) message).getObject();
70              assertTrue(obj instanceof ExceptionMessage);
71              // The payload should be the original message, not the reply message since the FTC threw an exception.
72              assertEquals(DEFAULT_INPUT_MESSAGE, ((ExceptionMessage) obj).getPayload());
73  
74              String dest = message.getStringProperty(MuleProperties.MULE_ENDPOINT_PROPERTY);
75              assertNotNull(dest);
76              assertEquals("jms://" + DEADLETTER_QUEUE_NAME, dest);
77  
78              applyTransaction(session);
79              return message;
80          }
81      }
82      
83      Scenario scenarioDeadLetterRollback = new ScenarioDeadLetterRollback();
84      class ScenarioDeadLetterRollback extends ScenarioDeadLetter
85      {
86          // @Override
87          protected void applyTransaction(Session session) throws JMSException
88          {
89              session.rollback();
90          }
91      }
92  
93      Scenario scenarioDeadLetterNotReceive = new ScenarioDeadLetterNotReceive();
94      class ScenarioDeadLetterNotReceive extends ScenarioDeadLetter
95      {
96          // @Override
97          public Message receive(Session session, MessageConsumer consumer) throws JMSException
98          {
99              Message message = consumer.receive(SMALL_TIMEOUT);
100             assertNull(message);
101             return message;
102         }
103     }
104 }