1   /*
2    * $Id: JmsSingleTransactionRecieveAndSendTestCase.java 12181 2008-06-26 20:05:55Z dirk.olmes $
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  
11  package org.mule.transport.jms.integration;
12  
13  import org.mule.tck.FunctionalTestCase;
14  
15  import javax.jms.Connection;
16  import javax.jms.ConnectionFactory;
17  import javax.jms.Message;
18  import javax.jms.MessageConsumer;
19  import javax.jms.MessageProducer;
20  import javax.jms.Session;
21  import javax.jms.TextMessage;
22  
23  import org.apache.activemq.ActiveMQConnectionFactory;
24  import org.apache.activemq.command.ActiveMQQueue;
25  
26  public class JmsSingleTransactionRecieveAndSendTestCase extends FunctionalTestCase
27  {
28      private Connection connection = null;
29      private Session session = null;
30  
31      protected String getConfigResources()
32      {
33          return "providers/activemq/jms-single-tx-recieve-send-in-one-tx.xml";
34      }
35  
36      public void testSingleTransactionBeginOrJoinAndAlwaysBegin() throws Exception
37      {
38          try
39          {
40              ConnectionFactory factory = new ActiveMQConnectionFactory(AbstractJmsFunctionalTestCase.DEFAULT_BROKER_URL);
41              connection = factory.createConnection();
42              connection.start();
43  
44              try
45              {
46                  session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
47                  ActiveMQQueue destination = new ActiveMQQueue(AbstractJmsFunctionalTestCase.DEFAULT_INPUT_MQ_QUEUE_NAME);
48                  MessageProducer producer = null;
49                  try
50                  {
51                      producer = session.createProducer(destination);
52                      producer.send(session.createTextMessage(AbstractJmsFunctionalTestCase.DEFAULT_INPUT_MESSAGE));
53                      session.commit();
54                  }
55                  finally
56                  {
57                      if (producer != null)
58                      {
59                          producer.close();
60                      }
61                  }
62  
63                  destination = new ActiveMQQueue(AbstractJmsFunctionalTestCase.DEFAULT_OUTPUT_MQ_QUEUE_NAME);
64                  MessageConsumer consumer = null;
65                  try
66                  {
67                      consumer = session.createConsumer(destination);
68                      Message message = consumer.receive(AbstractJmsFunctionalTestCase.TIMEOUT);
69                      assertNotNull(message);
70                      assertTrue(TextMessage.class.isAssignableFrom(message.getClass()));
71                      assertEquals(((TextMessage)message).getText(),
72                          AbstractJmsFunctionalTestCase.DEFAULT_OUTPUT_MESSAGE);
73                      session.commit();
74  
75                  }
76                  finally
77                  {
78                      if (consumer != null)
79                      {
80                          consumer.close();
81                      }
82                  }
83              }
84              finally
85              {
86                  if (session != null)
87                  {
88                      session.close();
89                  }
90              }
91          }
92          finally
93          {
94              if (connection != null)
95              {
96                  connection.close();
97              }
98          }
99      }
100 
101 }