1   /*
2    * $Id: ActiveMqJmsConnectorTestCase.java 7976 2007-08-21 14:26:13Z 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.providers.jms.activemq;
12  
13  import org.mule.providers.jms.DefaultJmsTopicResolver;
14  import org.mule.providers.jms.JmsTopicResolver;
15  import org.mule.providers.jms.xa.ConnectionFactoryWrapper;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.testmodels.mule.TestTransactionManagerFactory;
18  
19  import java.lang.reflect.Method;
20  import java.lang.reflect.Proxy;
21  
22  import javax.jms.Connection;
23  
24  import org.apache.activemq.ActiveMQXAConnectionFactory;
25  
26  public class ActiveMqJmsConnectorTestCase extends AbstractMuleTestCase
27  {
28      public void testConfigurationDefaults()
29      {
30          ActiveMqJmsConnector c = new ActiveMqJmsConnector();
31          assertFalse(c.isEagerConsumer());
32          JmsTopicResolver resolver = c.getTopicResolver();
33          assertNotNull("Topic resolver must not be null.", resolver);
34          assertTrue("Wrong topic resolver configured on the connector.",
35                     resolver instanceof DefaultJmsTopicResolver);
36      }
37  
38      public void testReflectiveXaCleanup() throws Exception
39      {
40          ActiveMQXAConnectionFactory factory = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false&broker.useJmx=false");
41  
42          ConnectionFactoryWrapper wrapper = new ConnectionFactoryWrapper(factory, new TestTransactionManagerFactory().create());
43          // can be a proxy
44          Connection connection = wrapper.createConnection();
45          assertNotNull(connection);
46          assertTrue(Proxy.isProxyClass(connection.getClass()));
47  
48          try
49          {
50              final Class clazz = connection.getClass();
51              Method cleanupMethod;
52              if (Proxy.isProxyClass(clazz))
53              {
54                  ConnectionFactoryWrapper.ConnectionInvocationHandler handler =
55                          (ConnectionFactoryWrapper.ConnectionInvocationHandler) Proxy.getInvocationHandler(connection);
56                  // this is really an XA connection
57                  connection = (Connection) handler.getTargetObject();
58                  Class realConnectionClass = connection.getClass();
59                  cleanupMethod = realConnectionClass.getMethod("cleanup", null);
60              }
61              else
62              {
63                  cleanupMethod = clazz.getMethod("cleanup", null);
64              }
65  
66  
67              if (cleanupMethod != null)
68              {
69                  cleanupMethod.invoke(connection, null);
70              }
71          }
72          finally
73          {
74              connection.close();
75          }
76  
77          // there should be no errors
78      }
79  }