1 18 package org.apache.activemq.ra; 19 20 import java.util.Timer ; 21 22 import javax.jms.ConnectionFactory ; 23 import javax.jms.JMSException ; 24 import javax.jms.MessageProducer ; 25 import javax.jms.Queue ; 26 import javax.jms.QueueConnection ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.Session ; 29 import javax.jms.TopicConnection ; 30 import javax.jms.TopicConnectionFactory ; 31 import javax.resource.ResourceException ; 32 import javax.resource.spi.BootstrapContext ; 33 import javax.resource.spi.ConnectionEvent ; 34 import javax.resource.spi.UnavailableException ; 35 import javax.resource.spi.XATerminator ; 36 import javax.resource.spi.work.WorkManager ; 37 38 import junit.framework.TestCase; 39 40 import org.apache.activemq.ActiveMQConnectionFactory; 41 import org.apache.activemq.ra.ActiveMQManagedConnection; 42 import org.apache.activemq.ra.ActiveMQManagedConnectionFactory; 43 import org.apache.activemq.ra.ActiveMQResourceAdapter; 44 import org.apache.activemq.ra.ManagedConnectionProxy; 45 46 47 50 public class ManagedConnectionTest extends TestCase { 51 52 53 private static final String DEFAULT_HOST = "vm://localhost"; 54 55 private ConnectionManagerAdapter connectionManager = new ConnectionManagerAdapter(); 56 private ActiveMQManagedConnectionFactory managedConnectionFactory; 57 private ConnectionFactory connectionFactory; 58 private ManagedConnectionProxy connection; 59 private ActiveMQManagedConnection managedConnection; 60 61 64 protected void setUp() throws Exception { 65 66 ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter(); 67 adapter.setServerUrl(DEFAULT_HOST); 68 adapter.setUserName(ActiveMQConnectionFactory.DEFAULT_USER); 69 adapter.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD); 70 adapter.start(new BootstrapContext (){ 71 public WorkManager getWorkManager() { 72 return null; 73 } 74 public XATerminator getXATerminator() { 75 return null; 76 } 77 78 public Timer createTimer() throws UnavailableException { 79 return null; 80 } 81 }); 82 83 managedConnectionFactory = new ActiveMQManagedConnectionFactory(); 84 managedConnectionFactory.setResourceAdapter(adapter); 85 86 connectionFactory = (ConnectionFactory ) managedConnectionFactory.createConnectionFactory(connectionManager); 87 connection = (ManagedConnectionProxy) connectionFactory.createConnection(); 88 managedConnection = connection.getManagedConnection(); 89 90 } 91 92 public void testConnectionCloseEvent() throws ResourceException , JMSException { 93 94 final boolean test[] = new boolean[]{false}; 95 connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() { 96 public void connectionClosed(ConnectionEvent arg0) { 97 test[0]=true; 98 } 99 }); 100 connection.close(); 101 assertTrue( test[0] ); 102 } 103 104 public void testLocalTransactionCommittedEvent() throws ResourceException , JMSException { 105 106 final boolean test[] = new boolean[]{false}; 107 connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() { 108 public void localTransactionCommitted(ConnectionEvent arg0) { 109 test[0]=true; 110 } 111 }); 112 113 managedConnection.getLocalTransaction().begin(); 114 Session session = connection.createSession(true,0); 115 116 doWork(session); 117 session.commit(); 118 119 assertTrue( test[0] ); 120 121 } 122 123 public void testLocalTransactionRollbackEvent() throws ResourceException , JMSException { 124 125 final boolean test[] = new boolean[]{false}; 126 connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() { 127 public void localTransactionRolledback(ConnectionEvent arg0) { 128 test[0]=true; 129 } 130 }); 131 managedConnection.getLocalTransaction().begin(); 132 Session session = connection.createSession(true,0); 133 doWork(session); 134 session.rollback(); 135 136 assertTrue( test[0] ); 137 } 138 139 public void testLocalTransactionStartedEvent() throws ResourceException , JMSException { 140 141 final boolean test[] = new boolean[]{false}; 142 connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() { 143 public void localTransactionStarted(ConnectionEvent arg0) { 144 test[0]=true; 145 } 146 }); 147 148 managedConnection.getLocalTransaction().begin(); 150 Session session = connection.createSession(true,0); 151 doWork(session); 152 153 assertTrue( test[0] ); 154 } 155 156 160 public void testCleanup() throws ResourceException , JMSException { 161 162 Session session = connection.createSession(true,0); 164 doWork(session); 165 connection.close(); 166 try { 167 doWork(session); 169 fail("Using a session after the connection is closed should throw exception."); 170 } catch ( JMSException e) { 171 } 172 } 173 174 public void testSessionCloseIndependance() throws ResourceException , JMSException { 175 176 Session session1 = connection.createSession(true,0); 177 Session session2 = connection.createSession(true,0); 178 assertTrue( session1!=session2 ); 179 180 doWork(session1); 181 session1.close(); 182 try { 183 doWork(session1); 185 fail("Using a session after the connection is closed should throw exception."); 186 } catch ( JMSException e) { 187 } 188 189 doWork(session2); 191 session2.close(); 192 try { 193 doWork(session2); 195 fail("Using a session after the connection is closed should throw exception."); 196 } catch ( JMSException e) { 197 } 198 } 199 200 204 public void doWork(Session session) throws JMSException { 205 Queue t = session.createQueue("TEST"); 206 MessageProducer producer = session.createProducer(t); 207 producer.send(session.createTextMessage("test message.")); 208 } 209 210 public void testImplementsQueueAndTopicConnection() throws Exception { 211 QueueConnection qc = ((QueueConnectionFactory )connectionFactory).createQueueConnection(); 212 assertNotNull(qc); 213 TopicConnection tc = ((TopicConnectionFactory )connectionFactory).createTopicConnection(); 214 assertNotNull(tc); 215 } 216 217 public void testSelfEquality() { 218 assertEquality(managedConnection, managedConnection); 219 } 220 221 public void testSamePropertiesButNotEqual() throws Exception { 222 ManagedConnectionProxy newConnection = (ManagedConnectionProxy) connectionFactory.createConnection(); 223 assertNonEquality(managedConnection, newConnection.getManagedConnection()); 224 } 225 226 private void assertEquality(ActiveMQManagedConnection leftCon, ActiveMQManagedConnection rightCon) { 227 assertTrue("ActiveMQManagedConnection are not equal", leftCon.equals(rightCon)); 228 assertTrue("ActiveMQManagedConnection are not equal", rightCon.equals(leftCon)); 229 assertTrue("HashCodes are not equal", leftCon.hashCode() == rightCon.hashCode()); 230 } 231 232 private void assertNonEquality(ActiveMQManagedConnection leftCon, ActiveMQManagedConnection rightCon) { 233 assertFalse("ActiveMQManagedConnection are equal", leftCon.equals(rightCon)); 234 assertFalse("ActiveMQManagedConnection are equal", rightCon.equals(leftCon)); 235 assertFalse("HashCodes are equal", leftCon.hashCode() == rightCon.hashCode()); 236 } 237 238 } 239 | Popular Tags |