View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jms.vendors;
8   
9   import org.mule.tck.junit4.FunctionalTestCase;
10  import org.mule.transport.jms.JmsConnector;
11  import org.mule.transport.jms.xa.ConnectionFactoryWrapper;
12  import org.mule.transport.jms.xa.TargetInvocationHandler;
13  
14  import java.lang.reflect.Method;
15  import java.lang.reflect.Proxy;
16  
17  import javax.jms.Connection;
18  import javax.jms.ConnectionFactory;
19  
20  import org.apache.activemq.ActiveMQXAConnectionFactory;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  public class ActiveMQXaJmsConnectorTestCase extends FunctionalTestCase
27  {
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "activemq-xa.xml";
33      }
34  
35      @Test
36      public void testReflectiveXaCleanup() throws Exception
37      {
38          JmsConnector c = (JmsConnector)muleContext.getRegistry().lookupConnector("jmsConnector");
39          assertNotNull(c);
40          
41          ConnectionFactory cf = c.getConnectionFactory();
42          assertTrue(cf instanceof ActiveMQXAConnectionFactory);
43  
44          ConnectionFactoryWrapper wrapper = new ConnectionFactoryWrapper(cf);
45          // can be a proxy
46          Connection connection = wrapper.createConnection();
47          assertNotNull(connection);
48          assertTrue(Proxy.isProxyClass(connection.getClass()));
49  
50          try
51          {
52              final Class clazz = connection.getClass();
53              Method cleanupMethod;
54              if (Proxy.isProxyClass(clazz))
55              {
56                  TargetInvocationHandler handler =
57                          (TargetInvocationHandler) Proxy.getInvocationHandler(connection);
58                  // this is really an XA connection
59                  connection = (Connection) handler.getTargetObject();
60                  Class realConnectionClass = connection.getClass();
61                  cleanupMethod = realConnectionClass.getMethod("cleanup", (Class[])null);
62              }
63              else
64              {
65                  cleanupMethod = clazz.getMethod("cleanup", (Class[])null);
66              }
67  
68  
69              if (cleanupMethod != null)
70              {
71                  cleanupMethod.invoke(connection, (Object[])null);
72              }
73          }
74          finally
75          {
76              connection.close();
77          }
78  
79          // there should be no errors
80      }
81  }