View Javadoc

1   /*
2    * $Id: ActiveMQXaJmsConnectorTestCase.java 22431 2011-07-18 07:40:35Z dirk.olmes $
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.junit4.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  import org.junit.Test;
26  
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  public class ActiveMQXaJmsConnectorTestCase extends FunctionalTestCase
31  {
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "activemq-xa.xml";
37      }
38  
39      @Test
40      public void testReflectiveXaCleanup() throws Exception
41      {
42          JmsConnector c = (JmsConnector)muleContext.getRegistry().lookupConnector("jmsConnector");
43          assertNotNull(c);
44          
45          ConnectionFactory cf = c.getConnectionFactory();
46          assertTrue(cf instanceof ActiveMQXAConnectionFactory);
47  
48          ConnectionFactoryWrapper wrapper = new ConnectionFactoryWrapper(cf);
49          // can be a proxy
50          Connection connection = wrapper.createConnection();
51          assertNotNull(connection);
52          assertTrue(Proxy.isProxyClass(connection.getClass()));
53  
54          try
55          {
56              final Class clazz = connection.getClass();
57              Method cleanupMethod;
58              if (Proxy.isProxyClass(clazz))
59              {
60                  TargetInvocationHandler handler =
61                          (TargetInvocationHandler) Proxy.getInvocationHandler(connection);
62                  // this is really an XA connection
63                  connection = (Connection) handler.getTargetObject();
64                  Class realConnectionClass = connection.getClass();
65                  cleanupMethod = realConnectionClass.getMethod("cleanup", (Class[])null);
66              }
67              else
68              {
69                  cleanupMethod = clazz.getMethod("cleanup", (Class[])null);
70              }
71  
72  
73              if (cleanupMethod != null)
74              {
75                  cleanupMethod.invoke(connection, (Object[])null);
76              }
77          }
78          finally
79          {
80              connection.close();
81          }
82  
83          // there should be no errors
84      }
85  }