1   /*
2    * $Id: ActiveMQJmsXaTransactionFunctionalTestCase.java 8583 2007-09-25 17:00:14Z akuzmin $
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.test.integration.providers.jms.activemq;
12  
13  import org.mule.MuleManager;
14  import org.mule.providers.jms.JmsConnector;
15  import org.mule.providers.jms.JmsConstants;
16  import org.mule.providers.jms.activemq.ActiveMqJmsConnector;
17  import org.mule.providers.jms.xa.SessionInvocationHandler;
18  import org.mule.transaction.XaTransactionFactory;
19  import org.mule.umo.UMOTransactionFactory;
20  
21  import java.lang.reflect.InvocationHandler;
22  import java.lang.reflect.Proxy;
23  
24  import javax.jms.Connection;
25  import javax.jms.ConnectionFactory;
26  import javax.jms.Session;
27  import javax.transaction.TransactionManager;
28  
29  import org.apache.activemq.ActiveMQConnectionFactory;
30  import org.apache.activemq.ActiveMQXAConnectionFactory;
31  import org.objectweb.jotm.Current;
32  import org.objectweb.jotm.Jotm;
33  
34  public class ActiveMQJmsXaTransactionFunctionalTestCase extends ActiveMQJmsTransactionFunctionalTestCase
35  {
36      private TransactionManager txManager;
37  
38      public ConnectionFactory getConnectionFactory() throws Exception
39      {
40          if (factory == null)
41          {
42              factory = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false&broker.useJmx=false");
43          }
44          return factory;
45      }
46  
47      public Connection getSenderConnection() throws Exception
48      {
49          factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.useJmx=false");
50          return factory.createConnection();
51      }
52  
53      protected void doSetUp() throws Exception
54      {
55          // check for already active JOTM instance
56          txManager = Current.getCurrent();
57          // if none found, create new local JOTM instance
58          if (txManager == null)
59          {
60              new Jotm(true, false);
61              txManager = Current.getCurrent();
62          }
63          txManager.setTransactionTimeout(15000);
64          super.doSetUp();
65          MuleManager.getInstance().setTransactionManager(txManager);
66      }
67  
68      public JmsConnector createConnector() throws Exception
69      {
70          ActiveMqJmsConnector connector = new ActiveMqJmsConnector();
71          connector.setName(CONNECTOR_NAME);
72          connector.setSpecification(JmsConstants.JMS_SPECIFICATION_11);
73          connector.setConnectionFactoryJndiName("XAJmsQueueConnectionFactory");
74          return connector;
75      }
76  
77      public UMOTransactionFactory getTransactionFactory()
78      {
79          return new XaTransactionFactory();
80      }
81  
82      public void afterInitialise() throws Exception
83      {
84          Thread.sleep(5000);
85      }
86  
87      public void testSendNotTransacted() throws Exception
88      {
89          // Cannot send non transacted messages when the connection is an
90          // XAConnection
91      }
92  
93      public void testSendTransactedIfPossibleWithoutTransaction() throws Exception
94      {
95          // there will always be a transaction available if using an Xa connector
96          // so this will always fail
97      }
98  
99      public void testIsSameMethod() throws Throwable
100     {
101         MuleManager.getInstance().start();
102 
103         assertNotNull(connector);
104         assertNotNull(connector.getConnection());
105         assertTrue(Proxy.isProxyClass(connector.getConnection().getClass()));
106         Connection connection = connector.getConnection();
107         Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
108         assertTrue(Proxy.isProxyClass(session.getClass()));
109         InvocationHandler invocationHandler = Proxy.getInvocationHandler(session);
110         assertEquals(invocationHandler.getClass(), org.mule.providers.jms.xa.SessionInvocationHandler.class);
111         SessionInvocationHandler sessionInvocationHandler = (SessionInvocationHandler) invocationHandler;
112         assertTrue(sessionInvocationHandler.getXAResource().isSameRM(sessionInvocationHandler.getXAResource()));
113         assertTrue(sessionInvocationHandler.getXAResource().isSameRM(sessionInvocationHandler.getTargetObject().getXAResource()));
114 //        assertTrue(sessionInvocationHandler.getTargetObject().getXAResource().isSameRM(sessionInvocationHandler.getXAResource()));
115 
116     }
117 
118 
119 }