1 18 package org.apache.activemq.ra; 19 20 import java.io.Serializable ; 21 import java.util.HashSet ; 22 import java.util.Timer ; 23 24 import javax.jms.Connection ; 25 import javax.jms.ConnectionFactory ; 26 import javax.jms.JMSException ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.TopicConnectionFactory ; 29 import javax.resource.Referenceable ; 30 import javax.resource.ResourceException ; 31 import javax.resource.spi.BootstrapContext ; 32 import javax.resource.spi.ConnectionRequestInfo ; 33 import javax.resource.spi.ManagedConnection ; 34 import javax.resource.spi.ManagedConnectionFactory ; 35 import javax.resource.spi.UnavailableException ; 36 import javax.resource.spi.XATerminator ; 37 import javax.resource.spi.work.WorkManager ; 38 39 import junit.framework.TestCase; 40 41 import org.apache.activemq.ActiveMQConnectionFactory; 42 import org.apache.activemq.ra.ActiveMQConnectionRequestInfo; 43 import org.apache.activemq.ra.ActiveMQManagedConnectionFactory; 44 import org.apache.activemq.ra.ActiveMQResourceAdapter; 45 import org.apache.activemq.ra.ManagedConnectionProxy; 46 47 48 51 public class ManagedConnectionFactoryTest extends TestCase { 52 53 private static final String DEFAULT_HOST = "vm://localhost?broker.persistent=false"; 54 private static final String REMOTE_HOST = "vm://remotehost?broker.persistent=false"; 55 private ActiveMQManagedConnectionFactory managedConnectionFactory; 56 57 60 protected void setUp() throws Exception { 61 62 ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter(); 63 adapter.setServerUrl(DEFAULT_HOST); 64 adapter.setUserName(ActiveMQConnectionFactory.DEFAULT_USER); 65 adapter.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD); 66 adapter.start(new BootstrapContext (){ 67 public WorkManager getWorkManager() { 68 return null; 69 } 70 public XATerminator getXATerminator() { 71 return null; 72 } 73 74 public Timer createTimer() throws UnavailableException { 75 return null; 76 } 77 }); 78 79 managedConnectionFactory = new ActiveMQManagedConnectionFactory(); 80 managedConnectionFactory.setResourceAdapter(adapter); 81 82 } 83 84 public void testConnectionFactoryAllocation() throws ResourceException , JMSException { 85 86 final boolean allocateRequested[] = new boolean[]{false}; 89 Object cf = managedConnectionFactory.createConnectionFactory( 90 new ConnectionManagerAdapter() { 91 private static final long serialVersionUID = 1699499816530099939L; 92 93 public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info) 94 throws ResourceException { 95 allocateRequested[0]=true; 96 return super.allocateConnection(connectionFactory, info); 97 } 98 } 99 ); 100 101 assertTrue( cf instanceof ConnectionFactory ); 103 ConnectionFactory connectionFactory = (ConnectionFactory )cf; 104 105 Connection connection = connectionFactory.createConnection(); 107 assertTrue(allocateRequested[0]); 108 109 assertTrue( connection!=null ); 111 assertTrue( connection instanceof ManagedConnectionProxy ); 112 113 } 114 115 116 public void testConnectionFactoryConnectionMatching() throws ResourceException , JMSException { 117 118 ActiveMQConnectionRequestInfo ri1 = new ActiveMQConnectionRequestInfo(); 119 ri1.setServerUrl(DEFAULT_HOST); 120 ri1.setUserName(ActiveMQConnectionFactory.DEFAULT_USER); 121 ri1.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD); 122 123 ActiveMQConnectionRequestInfo ri2 = new ActiveMQConnectionRequestInfo(); 124 ri2.setServerUrl(REMOTE_HOST); 125 ri2.setUserName(ActiveMQConnectionFactory.DEFAULT_USER); 126 ri2.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD); 127 assertNotSame(ri1, ri2); 128 129 ManagedConnection connection1 = managedConnectionFactory.createManagedConnection(null, ri1); 130 ManagedConnection connection2 = managedConnectionFactory.createManagedConnection(null, ri2); 131 assertTrue(connection1!=connection2); 132 133 HashSet set = new HashSet (); 134 set.add(connection1); 135 set.add(connection2); 136 137 ActiveMQConnectionRequestInfo ri3 = ri1.copy(); 139 assertTrue( ri1!=ri3 && ri1.equals(ri3) ); 140 ManagedConnection test = managedConnectionFactory.matchManagedConnections(set,null, ri3); 141 assertTrue( connection1==test ); 142 143 ri3 = ri2.copy(); 145 assertTrue( ri2!=ri3 && ri2.equals(ri3) ); 146 test = managedConnectionFactory.matchManagedConnections(set,null, ri2); 147 assertTrue( connection2==test ); 148 149 } 150 151 public void testConnectionFactoryIsSerializableAndReferenceable() throws ResourceException , JMSException { 152 Object cf = managedConnectionFactory.createConnectionFactory(new ConnectionManagerAdapter()); 153 assertTrue( cf!=null ); 154 assertTrue( cf instanceof Serializable ); 155 assertTrue( cf instanceof Referenceable ); 156 } 157 158 public void testImplementsQueueAndTopicConnectionFactory() throws Exception { 159 Object cf = managedConnectionFactory.createConnectionFactory(new ConnectionManagerAdapter()); 160 assertTrue( cf instanceof QueueConnectionFactory ); 161 assertTrue( cf instanceof TopicConnectionFactory ); 162 } 163 164 } 165 | Popular Tags |