View Javadoc

1   /*
2    * $Id: ActiveMQXaJmsConnectorTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.vendors;
12  
13  import org.mule.tck.FunctionalTestCase;
14  import org.mule.transport.jms.JmsConnector;
15  import org.mule.transport.jms.xa.ConnectionFactoryWrapper;
16  import org.mule.transport.jms.xa.TargetInvocationHandler;
17  
18  import java.lang.reflect.Method;
19  import java.lang.reflect.Proxy;
20  
21  import javax.jms.Connection;
22  import javax.jms.ConnectionFactory;
23  
24  import org.apache.activemq.ActiveMQXAConnectionFactory;
25  
26  public class ActiveMQXaJmsConnectorTestCase extends FunctionalTestCase
27  {
28      protected String getConfigResources()
29      {
30          return "activemq-xa.xml";
31      }
32  
33      public void testReflectiveXaCleanup() throws Exception
34      {
35          JmsConnector c = (JmsConnector)muleContext.getRegistry().lookupConnector("jmsConnector");
36          assertNotNull(c);
37          
38          ConnectionFactory cf = c.getConnectionFactory();
39          assertTrue(cf instanceof ActiveMQXAConnectionFactory);
40  
41          ConnectionFactoryWrapper wrapper = new ConnectionFactoryWrapper(cf);
42          // can be a proxy
43          Connection connection = wrapper.createConnection();
44          assertNotNull(connection);
45          assertTrue(Proxy.isProxyClass(connection.getClass()));
46  
47          try
48          {
49              final Class clazz = connection.getClass();
50              Method cleanupMethod;
51              if (Proxy.isProxyClass(clazz))
52              {
53                  TargetInvocationHandler handler =
54                          (TargetInvocationHandler) Proxy.getInvocationHandler(connection);
55                  // this is really an XA connection
56                  connection = (Connection) handler.getTargetObject();
57                  Class realConnectionClass = connection.getClass();
58                  cleanupMethod = realConnectionClass.getMethod("cleanup", (Class[])null);
59              }
60              else
61              {
62                  cleanupMethod = clazz.getMethod("cleanup", (Class[])null);
63              }
64  
65  
66              if (cleanupMethod != null)
67              {
68                  cleanupMethod.invoke(connection, (Object[])null);
69              }
70          }
71          finally
72          {
73              connection.close();
74          }
75  
76          // there should be no errors
77      }
78  }